Last active
February 13, 2024 05:55
-
-
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)
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 ) ); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
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.