Last active
May 7, 2019 11:00
-
-
Save drZool/91d7ba12714c88533723085c79c285fb to your computer and use it in GitHub Desktop.
Group selected scene objects. Place this script below a folder named "Editor"
This file contains hidden or 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 UnityEngine; | |
| using System.Collections; | |
| using UnityEditor; | |
| public class GroupSceneObjects | |
| { | |
| [MenuItem("Hello/GroupSelected %g")] | |
| static void GroupSelected(){ | |
| Transform topParent = null; | |
| var objects = Selection.gameObjects; | |
| int depth = 255; | |
| foreach (var obj in objects) { | |
| if (obj.transform.parent == null) { | |
| topParent = null; | |
| break; | |
| } | |
| int objdepth = GetDepth (obj.transform); | |
| if (objdepth < depth) { | |
| depth = objdepth; | |
| topParent = obj.transform.parent; | |
| } | |
| } | |
| var group = new GameObject ("Group").transform; | |
| group.SetParent (topParent, false); | |
| Undo.RegisterCreatedObjectUndo (group.gameObject, "Group"); | |
| foreach(var obj in objects){ | |
| Undo.SetTransformParent( obj.transform, group, "Group"); | |
| } | |
| Selection.activeGameObject = group.gameObject; | |
| } | |
| static int GetDepth (Transform transform, int depth = 0) | |
| { | |
| if (transform.parent == null) | |
| return depth; | |
| return GetDepth (transform.parent, depth + 1); | |
| } | |
| } |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment