Advertisement
Guest User

Unity Hierarchy Shortcuts

a guest
Feb 24th, 2016
783
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 5.48 KB | None | 0 0
  1. using UnityEngine;
  2. using UnityEditor;
  3. using System.Linq;
  4. using System.Collections.Generic;
  5.  
  6. /// <summary>
  7. /// A collection of useful shortcuts for manipulating the hierarchy in Unity.
  8. /// Some of this was written by me (@adamgryu), some of it was collected from random Unity forums and StackOverflow.
  9. /// Feel free to use any of this code freely, without any restrictions or references.
  10. /// Tested with Unity 5.3.3.
  11. /// </summary>
  12. public class CustomHierarchyMenuItems {
  13.  
  14.     [MenuItem("GameObject/Hierarchy/Collapse All  [Alt+Q] &q", false, 0)]
  15.     private static void CollapseAll() {
  16.         foreach (GameObject obj in SceneRoots()) {
  17.             SetExpandedRecursive(obj, false);
  18.         }
  19.     }
  20.  
  21.     [MenuItem("GameObject/Hierarchy/Un-Parent And Collapse All  [Ctrl+Q] %q", false, 0)]
  22.     private static void UnparentAndCollapse() {
  23.         foreach (GameObject obj in Selection.gameObjects) {
  24.             obj.transform.parent = null;
  25.         }
  26.         CollapseAll();
  27.     }
  28.  
  29.     [MenuItem("GameObject/Hierarchy/Place Selection In New Container  [Shift+Ctrl+Q] #%q", false, 0)]
  30.     private static void PlaceInNewContainer() {
  31.         if (Selection.gameObjects.Length <= 0) {
  32.             return;
  33.         }
  34.  
  35.         GameObject container = new GameObject("GameObject");
  36.  
  37.         foreach (GameObject obj in Selection.gameObjects) {
  38.             Transform nextParent = obj.transform.parent;
  39.             bool dontMove = false;
  40.             while (nextParent != null) {
  41.                 if (Selection.gameObjects.Any(o => o.gameObject == nextParent.gameObject)) {
  42.                     dontMove = true;
  43.                     break;
  44.                 }
  45.                 nextParent = nextParent.transform.parent;
  46.             }
  47.  
  48.             if (!dontMove) {
  49.                 obj.transform.parent = container.transform;
  50.             }
  51.         }
  52.  
  53.         Selection.objects = new Object[] { };
  54.         CollapseAll();
  55.     }
  56.  
  57.     #region CodeFromWeb
  58.  
  59.     [MenuItem("GameObject/Hierarchy/Sort Children By Name  [Alt+Shift+Ctrl+Q] &#%q", false, -1)]
  60.     public static void SortGameObjectsByName(MenuCommand menuCommand) {
  61.         if (menuCommand.context == null || menuCommand.context.GetType() != typeof(GameObject)) {
  62.             EditorUtility.DisplayDialog("Error", "You must select an item to sort in the frame", "Okay");
  63.             return;
  64.         }
  65.  
  66.         GameObject parentObject = (GameObject)menuCommand.context;
  67.  
  68.         // Build a list of all the Transforms in this player's hierarchy
  69.         Transform[] objectTransforms = new Transform[parentObject.transform.childCount];
  70.         for (int i = 0; i < objectTransforms.Length; i++)
  71.             objectTransforms[i] = parentObject.transform.GetChild(i);
  72.  
  73.         int sortTime = System.Environment.TickCount;
  74.  
  75.         bool sorted = false;
  76.         // Perform a bubble sort on the objects
  77.         while (sorted == false) {
  78.             sorted = true;
  79.             for (int i = 0; i < objectTransforms.Length - 1; i++) {
  80.                 // Compare the two strings to see which is sooner
  81.                 int comparison = objectTransforms[i].name.CompareTo(objectTransforms[i + 1].name);
  82.  
  83.                 if (comparison > 0) // 1 means that the current value is larger than the last value
  84.                 {
  85.                     objectTransforms[i].transform.SetSiblingIndex(objectTransforms[i + 1].GetSiblingIndex());
  86.                     sorted = false;
  87.                 }
  88.             }
  89.  
  90.             // resort the list to get the new layout
  91.             for (int i = 0; i < objectTransforms.Length; i++)
  92.                 objectTransforms[i] = parentObject.transform.GetChild(i);
  93.         }
  94.  
  95.         Debug.Log("Sort took " + (System.Environment.TickCount - sortTime) + " milliseconds");
  96.     }
  97.  
  98.     public static IEnumerable<GameObject> SceneRoots() {
  99.         var prop = new HierarchyProperty(HierarchyType.GameObjects);
  100.         var expanded = new int[0];
  101.         while (prop.Next(expanded)) {
  102.             yield return prop.pptrValue as GameObject;
  103.         }
  104.     }
  105.  
  106.     public static void Collapse(GameObject gameObject, bool collapse = true) {
  107.         // Bail out immediately if the go doesn't have children.
  108.         if (gameObject.transform.childCount == 0) return;
  109.         // Get a reference to the hierarchy window.
  110.         var hierarchy = GetFocusedWindow("Hierarchy");
  111.         // Select our GameObject.
  112.         SelectObject(gameObject);
  113.         // Create a new key event (RightArrow for collapsing, LeftArrow for folding)
  114.         var key = new Event { keyCode = collapse ? KeyCode.RightArrow : KeyCode.LeftArrow, type = EventType.keyDown };
  115.         // Finally, send the window the event.
  116.         hierarchy.SendEvent(key);
  117.     }
  118.  
  119.     public static void SetExpandedRecursive(GameObject go, bool expand) {
  120.         var type = typeof(EditorWindow).Assembly.GetType("UnityEditor.SceneHierarchyWindow");
  121.         var methodInfo = type.GetMethod("SetExpandedRecursive");
  122.  
  123.         EditorApplication.ExecuteMenuItem("Window/Hierarchy");
  124.         var window = EditorWindow.focusedWindow;
  125.  
  126.         methodInfo.Invoke(window, new object[] { go.GetInstanceID(), expand });
  127.     }
  128.  
  129.     public static void SelectObject(Object obj) {
  130.         Selection.activeObject = obj;
  131.     }
  132.  
  133.     public static EditorWindow GetFocusedWindow(string window) {
  134.         FocusOnWindow(window);
  135.         return EditorWindow.focusedWindow;
  136.     }
  137.  
  138.     public static void FocusOnWindow(string window) {
  139.         EditorApplication.ExecuteMenuItem("Window/" + window);
  140.     }
  141.  
  142.     #endregion
  143. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement