AddChild

From Unify Community Wiki
(Difference between revisions)
Jump to: navigation, search
m (Reverted edits by Joseph05408 (Talk); changed back to last version by NCarter)
m (Text replace - "<csharp>" to "<syntaxhighlight lang="csharp">")
Line 15: Line 15:
 
== C# - AddChild.cs ==
 
== C# - AddChild.cs ==
  
<csharp>using UnityEngine;
+
<syntaxhighlight lang="csharp">using UnityEngine;
 
using UnityEditor;
 
using UnityEditor;
 
using System.Collections;
 
using System.Collections;

Revision as of 09:55, 10 January 2012

Author: Neil Carter (NCarter)

Description

Adds an empty GameObject as a child of each selected object. This may sometimes be preferable to the built-in GameObject→Create Empty command, as it saves you from having to hunt in the hierarchy for the newly created object.

Usage

You must place the script in a folder named Editor in your project's Assets folder for it to work properly.

Select some objects in the Scene view or Hierarchy window, then choose GameObject→Add Child from the menu (or press control N). Each selected object will have a new child object added to it.

The newly created child objects are named _Child to make them sort towards the top of the Hierarchy list.

C# - AddChild.cs

using UnityEngine;
using UnityEditor;
using System.Collections;
 
public class AddChild : ScriptableObject
{
    [MenuItem ("GameObject/Add Child ^n")]
    static void MenuAddChild()
    {
        Transform[] transforms = Selection.GetTransforms(SelectionMode.TopLevel | SelectionMode.OnlyUserModifiable);
 
        foreach(Transform transform in transforms)
        {
            GameObject newChild = new GameObject("_Child");
            newChild.transform.parent = transform;
        }
    }
}
</csharp>
 
[[Category:Editor Scripts]]
[[Category:ScriptableObject]]
[[Category:C Sharp]]
Personal tools
Namespaces

Variants
Actions
Navigation
Extras
Toolbox