Fast / Unity

Rapid Development Tips (2): Editing Multiple Objects Efficiently

In the middle and late stages of development, scenes and UI hierarchies accumulate a lot of objects. If you need to change button events, hierarchy structure, component settings, or added components across many objects, doing it manually is miserable and error-prone. Editor scripts can apply those changes consistently.

Environment

  • Windows 7
  • Unity 5.2.4

How to get objects in Scene?

  1. Use Selection to get selected objects.
  2. Use GameObject.FindXXX methods to search objects.
  3. Start from a specific object, then traverse its children and apply filtering rules.

Implement it

Requirement, simplified for the example:

  1. Find the child object named Player.
  2. Set the Player’s Collider enabled value to false.
  3. Add a child object under Player.
  4. Name the new child object Player Child.

Start with this hierarchy:

Demand_01

Implement an editor script.

Method A: use Selection

ModifyBySelect.cs

using UnityEngine;
using UnityEditor;

public class ModifyBySelect
{
    [MenuItem("Modify/ModifyBySelect")]
    public static void BySelect()
    {
        if (Selection.activeGameObject != null)
            Modify(Selection.activeGameObject.transform);
    }

    static void Modify(Transform parent)
    {
        Transform trans = null;

        for (int i = 0; i < parent.childCount; i++)
        {
            trans = parent.GetChild(i);

            if (trans.childCount > 0)
                Modify(trans);


            if (trans.name == "Player")
            {
                trans.GetComponent<BoxCollider>().enabled = false;

                GameObject go = new GameObject("Player Child");
                go.transform.SetParent(trans);

                Debug.Log(trans);
            }
        }
    }
}

Line 6: Add a Unity menu item.

Line 10: Get the selected object in the current Scene.

Line 19: Iterate child objects.

Lines 21-22: If the child has children, recurse.

Line 25: Check the GameObject name.

Line 27: Disable BoxCollider.

Lines 29-30: Create the child object and set its parent.

Method B: use GameObject.FindXXX

ModifyByFind.cs

using UnityEngine;
using UnityEditor;

public class ModifyByFind
{
    [MenuItem("Modify/ModifyByFind")]
    public static void ByFind()
    {
        Modify(GameObject.FindObjectsOfType<Transform>());
    }

    static void Modify(Transform[] trans)
    {
        for (int i = 0; i < trans.Length; i++)
        {
            if (trans[i].name == "Player")
            {
                trans[i].GetComponent<BoxCollider>().enabled = false;

                GameObject go = new GameObject("Player Child");
                go.transform.SetParent(trans[i]);

                Debug.Log(trans[i]);
            }
        }

    }
}

Line 6: Add a Unity menu item.

Line 9: Find all Transform objects in the Scene.

Line 16: Check the GameObject name.

Line 18: Disable BoxCollider.

Lines 20-21: Create the child object and set its parent.

Run the menu item.

Demand_03

Result:

Demand_02

Postscript

Editor scripts let us modify many objects without hand-selecting and hand-editing each one. For repeated project-specific work, this can grow into dedicated EditorWindow tools.

References

Attribution

Please credit ARKAI Studio and link back to this article when quoting or reposting.