Estoy colapsando un gameObject principal seleccionado con este script de editor. Todo funciona bien, pero también colapsa otros gameObjects expandidos, lo que no debería suceder. Por ejemplo, bajo dos padres diferentes, si colapso un gameObject principal, incluso el otro gameObject principal se colapsa. ¿Cómo no dejo que esto suceda?
Siento que el problema está en rootGameObjects.AddRange (SceneManager.GetSceneAt (i).GetRootGameObjects ()); donde toma el gameObject raíz en una escena pero no el gameObject seleccionado en la jerarquía.
using System.Collections.Generic; using System.IO; using UnityEditor; using UnityEngine; using UnityEngine.SceneManagement; public static class CollapseTools { private static int wait = 0; private static int undoIndex; public static void CollapseGameObjects () { EditorApplication.update -= CollapseGameObjects; CollapseGameObjects (new MenuCommand (null)); } [MenuItem ("GameObject/Collapse GameObjects", priority = 40)] private static void CollapseGameObjects (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 if (command.context) { EditorApplication.update -= CollapseGameObjects; EditorApplication.update += CollapseGameObjects; return; } List<GameObject> rootGameObjects = new List<GameObject> (); #if UNITY_2018_3_OR_NEWER // Check if a prefab stage is currently open var prefabStage = UnityEditor.Experimental.SceneManagement.PrefabStageUtility.GetCurrentPrefabStage (); if (prefabStage != null && prefabStage.stageHandle.IsValid ()) rootGameObjects.Add (prefabStage.prefabContentsRoot); else #endif { int sceneCount = SceneManager.sceneCount; for (int i = 0; i < sceneCount; i++) rootGameObjects.AddRange (SceneManager.GetSceneAt (i).GetRootGameObjects ()); } if (rootGameObjects.Count > 0) { Undo.IncrementCurrentGroup (); Selection.objects = Selection.objects; undoIndex = Undo.GetCurrentGroup (); Selection.objects = rootGameObjects.ToArray (); EditorApplication.update -= CollapseHelper; EditorApplication.update += CollapseHelper; } } private static void CollapseHelper () { if (wait < 1) // Increase the number if script doesn't always work wait++; else { EditorApplication.update -= CollapseHelper; wait = 0; EditorWindow focusedWindow = EditorWindow.focusedWindow; if (focusedWindow != null) focusedWindow.SendEvent (new Event { keyCode = KeyCode.LeftArrow, type = EventType.KeyDown, alt = true }); Undo.RevertAllDownToGroup (undoIndex); } } }Cuando se trata de Unity, nada supera la reflexión:
using UnityEditor; using UnityEngine; internal class MyClass : EditorWindow { private void OnGUI() { using (var scope = new EditorGUI.DisabledGroupScope(Selection.activeGameObject == null)) { if (GUILayout.Button("Expand Recursive")) { var type = typeof(EditorWindow).Assembly.GetType("UnityEditor.SceneHierarchyWindow"); var window = GetWindow(type); var exprec = type.GetMethod("SetExpandedRecursive"); exprec!.Invoke(window, new object[] {Selection.activeGameObject.GetInstanceID(), true}); } } } [MenuItem("TEST/TEST")] private static void Init() { GetWindow<MyClass>(); } } Consulte la clase SceneHierarchyWindow para ver otras cosas a las que podría conectarse.
Ahora tienes otro problema, cuando tienes varias ventanas de jerarquía abiertas, te dejaré resolver esa :)