InsertParent

From Unify Community Wiki
(Difference between revisions)
Jump to: navigation, search
m (Pedantry)
m (Text replace - "</csharp>" to "</syntaxhighlight>")
 
(4 intermediate revisions by 2 users not shown)
Line 3: Line 3:
 
== Description ==
 
== Description ==
  
This script is an alternative to the built-in Make Parent command.  Whereas the Make Parent command makes the first object you select the parent of subsequently selected object, this script inserts a new GameObject which becomes the parent of all selected objects.
+
This script is an alternative to the built-in Make Parent command.  Whereas the Make Parent command makes the first object you select the parent of subsequently selected objects, this script inserts a new GameObject which becomes the parent of all selected objects.
  
 
== Usage ==
 
== Usage ==
Line 13: Line 13:
 
*If only one object is selected...
 
*If only one object is selected...
 
**...and it has no parent, a new GameObject will be added, and the selected object will become a child of it.
 
**...and it has no parent, a new GameObject will be added, and the selected object will become a child of it.
**...and it already has a parent, a new GameObject will be inserted between the object and its original parent.
+
**...and it already has a parent, a new GameObject will be ''inserted'' between the object and its original parent.
 
*If more than one object is selected, irrespective of whether those objects already have parents, all those objects will become children of the newly created GameObject.
 
*If more than one object is selected, irrespective of whether those objects already have parents, all those objects will become children of the newly created GameObject.
  
Line 20: Line 20:
 
== C# - InsertParent.cs ==
 
== C# - InsertParent.cs ==
  
<csharp>using UnityEngine;
+
<syntaxhighlight lang="csharp">using UnityEngine;
 
using UnityEditor;
 
using UnityEditor;
 
using System.Collections;
 
using System.Collections;
Line 48: Line 48:
 
         }
 
         }
 
     }
 
     }
}</csharp>
+
}</syntaxhighlight>
  
 
[[Category:Editor Scripts]]
 
[[Category:Editor Scripts]]
 
[[Category:ScriptableObject]]
 
[[Category:ScriptableObject]]
 
[[Category:C Sharp]]
 
[[Category:C Sharp]]

Latest revision as of 20:45, 10 January 2012

Author: Neil Carter (NCarter)

[edit] Description

This script is an alternative to the built-in Make Parent command. Whereas the Make Parent command makes the first object you select the parent of subsequently selected objects, this script inserts a new GameObject which becomes the parent of all selected objects.

[edit] 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→Insert Parent from the menu (or press control P). Depending on the nature of the selected objects, it will have one of the following effects:

  • If only one object is selected...
    • ...and it has no parent, a new GameObject will be added, and the selected object will become a child of it.
    • ...and it already has a parent, a new GameObject will be inserted between the object and its original parent.
  • If more than one object is selected, irrespective of whether those objects already have parents, all those objects will become children of the newly created GameObject.

The newly created parent object is named _Parent to make it sort towards the top of the Hierarchy list.

[edit] C# - InsertParent.cs

using UnityEngine;
using UnityEditor;
using System.Collections;
 
public class InsertParent : ScriptableObject
{
    [MenuItem ("GameObject/Insert Parent ^p")]
    static void MenuInsertParent()
    {
        Transform[] transforms = Selection.GetTransforms(SelectionMode.TopLevel |
            SelectionMode.OnlyUserModifiable);
 
        GameObject newParent = new GameObject("_Parent");
        Transform newParentTransform = newParent.transform;
 
        if(transforms.Length == 1)
        {
            Transform originalParent = transforms[0].parent;
            transforms[0].parent = newParentTransform;
            if(originalParent)
                newParentTransform.parent = originalParent;
        }
        else
        {
            foreach(Transform transform in transforms)
                transform.parent = newParentTransform;
        }
    }
}
Personal tools
Namespaces

Variants
Actions
Navigation
Extras
Toolbox