Link to the third episode: Unity Editor Extension - Episode 3 - Adding a button to the component menu and resetting the component
1. Objectives + effect display of this program
1. Reselect the name of a single object
2. Reselect the names of all objects
3. Select all objects, but only change the name of the first layer of objects
4. Come to a useful one (all selected objects, named in order)
2. Overview
There is a class Selection that allows you to get different objects
Selection.activeGameObject //选择单个物体
//如果就非要多选,它认多选时选的第一个
Selection.gameObjects //你选的所有物体都算
Selection.transforms //你选的所有物体的辈分最大那个物体
3. Case
1. Reselect the name of a single object
using UnityEditor;
public class Tools
{
[MenuItem("第四集/菌菌兹")]
static void ChangeName() {
Selection.activeGameObject.name = "菌菌兹";
}
}
2. Reselect the names of all objects
using UnityEditor;
using UnityEngine;
public class Tools
{
[MenuItem("第四集/菌菌兹")]
static void ChangeName() {
foreach (GameObject obj in Selection.gameObjects)
{
obj.name = "菌菌兹";
}
}
}
3. Select all objects, but only change the name of the first layer of objects
using UnityEditor;
using UnityEngine;
public class Tools
{
[MenuItem("第四集/菌菌兹")]
static void ChangeName() {
foreach (Transform obj in Selection.transforms)
{
obj.name = "菌菌兹";
}
}
}
4. All the selected objects are named in order
using UnityEditor;
using UnityEngine;
public class Tools
{
[MenuItem("第四集/菌菌兹")]
static void ChangeName() {
foreach (GameObject obj in Selection.gameObjects)
{
//GetSiblingIndex()是获取父亲的第几个孩子
obj.name = obj.transform.GetSiblingIndex().ToString();
}
}
}