Skip to content

Instantly share code, notes, and snippets.

@yasirkula
Last active February 13, 2024 05:55
Show Gist options
  • Star 5 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save yasirkula/b5b357b4abdce68f94a0e33194eee1a9 to your computer and use it in GitHub Desktop.
Save yasirkula/b5b357b4abdce68f94a0e33194eee1a9 to your computer and use it in GitHub Desktop.
Group the selected objects under a new parent object in Hierarchy (Unity 3D)
using System.Collections.Generic;
using UnityEditor;
using UnityEngine;
public static class EmptyParentCreator
{
[MenuItem( "GameObject/Create Empty Parent", priority = 0 )]
private static void CreateEmptyParent( MenuCommand command )
{
// This happens when this button is clicked via hierarchy's right click context menu
// and is called once for each object in the selection. We don't want that, we want
// the function to be called only once so that there aren't multiple empty parents
// generated in one call
if( command.context )
{
EditorApplication.update -= CallCreateEmptyParentOnce;
EditorApplication.update += CallCreateEmptyParentOnce;
return;
}
Transform[] selection = Selection.transforms;
if( selection.Length == 0 )
return;
List<Renderer> renderers = new List<Renderer>( 8 );
Bounds bounds = new Bounds();
bool boundsInitialized = false;
for( int i = 0; i < selection.Length; i++ )
{
if( AssetDatabase.Contains( selection[i].gameObject ) )
continue;
renderers.Clear();
selection[i].GetComponentsInChildren( renderers );
for( int j = renderers.Count - 1; j >= 0; j-- )
{
if( boundsInitialized )
bounds.Encapsulate( renderers[j].bounds );
else
{
bounds = renderers[j].bounds;
boundsInitialized = true;
}
}
}
Transform newParent = new GameObject().transform;
newParent.position = bounds.center;
//newParent.position -= new Vector3( 0f, bounds.extents.y, 0f ); // Move pivot to the bottom
Undo.RegisterCreatedObjectUndo( newParent.gameObject, "Parent Selected" );
for( int i = 0; i < selection.Length; i++ )
{
if( AssetDatabase.Contains( selection[i].gameObject ) )
continue;
Undo.SetTransformParent( selection[i], newParent, "Parent Selected" );
}
Selection.activeTransform = newParent;
}
private static void CallCreateEmptyParentOnce()
{
EditorApplication.update -= CallCreateEmptyParentOnce;
CreateEmptyParent( new MenuCommand( null ) );
}
}
@yasirkula
Copy link
Author

yasirkula commented Oct 16, 2019

How To

Simply create a folder called Editor inside your Project window and add this script inside it. Then, after making a selection, simply click the GameObject-Create Empty Parent button.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment