Unity: Insert Parent Script

A script for grouping a bunch of objects together with a new partent GameObject




Here's a unity script I modified from the Unify Wiki. it groups selected game objects together. My modification does a few extra things:

1. The newly created parent GO's position is set to the position of the first selected GO. This makes the parent's transform controls appear closer to the group than if it was at 0,0,0. It also got rid of a bug I was seeing where grouped objects would flicker like mad as you approached them if it's parent transform was a zillion miles away from them.

2. The new parent GO is renamed to match the first object in the selection. This should make it easier to find in the Hierarchy, since it won't jump around as much.

3. The new parent GO is set as the active selection. Again, to make it easier to find.

I didn't add it back to the Wiki because, well, I wasn't 1005 sure I wrote all this code correctly. Let me know if you find any problems.

using UnityEngine;
using UnityEditor;
using System.Collections;
// HL: Mar11, 2009: I took this from the Unity Wiki. I made some changes so that
// 1. The newly created parent GO's position is set to the position of the first selected GO. this should solve the flicker bug I ran into.
// 2. The new parent GO is renamed to match the first object in the selection. This should make it easier to find in the Hierarchy.
// 3. The new parent GO is set as the active selection. 

public class InsertParent : ScriptableObject
{
    [MenuItem ("GameObject/Insert Parent %g")]
    static void MenuInsertParent()
    {
		Debug.Log("trigger!"); 
		Transform[] transforms = Selection.GetTransforms(SelectionMode.TopLevel |
            SelectionMode.OnlyUserModifiable);

        GameObject newParent = new GameObject("_Parent");
        Transform newParentTransform = newParent.transform;
		
		// HL:  experimental code, positions game object at location of first selected item:
		newParentTransform.position = transforms[0].position;  
		newParentTransform.name = transforms[0].name + " set"; // rename it so it ends up in roughly same location in the list.  
		//--AA-- End HL code. 
        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;
        }
    
	Selection.activeObject = newParent; 	 /// HL: sets the new parent GO to be the actively selected item in the UI.
	}
}




Feedback - 3 responses

Displayed newest to oldest. Leave a comment.
cheap auto insurance dallas texas wrote:   
I can not put it off to visit your types of the way your handwriting has got changed! We are enclosed in the many sorts that will give writing will take. I love acquiring aged post cards only to be aware of the crafting.
where can i find cheap auto insurance online wrote:   
Wonderful content! I have never been aware of telemedicine before. It is actually intriguing how it operates in addition to impression people. Very often find pertaining to academics writing agency in addition to generate content with regards to different types of medication. This page will help me a bunch.
Lucas Meijer wrote:   
Nice. btw: no need to make the class a ScriptableObject.

Leave a comment

Name:
Website:
Comment:

Email:
Captcha:
Choose the Saturn image from below:

           

           

 

This site is mostly about

Video Game Design

User Interface Design

Creative & fun stuff

 

Your Host

I'm Hanford Lemoore. My parking skills are unparalleled.

I make things. From consumer electronics, to video games, to theme park attractions. Perhaps I can make things for you! Check out my portfolio.

When I'm not making things for other people, I'm usually experimenting.

 

Contact

Follow me on Twitter.

Message me on Facebook.

Email me using my contact form.

 

RSS 2.0

 

maquettegame.com

tikiroom.com

junkyardclubhouse.com

monolux.com

 

   


Copyright 2012 Hanford Lemoore | Blog | About | Portfolio | Contact
Powered by Olark