[英]Collapsing selected Game Objects in the hierarchy
I am collapsing a selected parent gameObject with this editor script.我正在使用此编辑器脚本折叠选定的父游戏对象。 It all works well but it also collapses other expanded gameObjects which should not happen.这一切都很好,但它也会折叠其他不应该发生的扩展游戏对象。 For example under two different parents, if I collapse one parent gameObject, even the other parent gameObject gets collapsed.例如,在两个不同的父级下,如果我折叠一个父级游戏对象,甚至另一个父级游戏对象也会被折叠。 How do I not let this happen?我如何不让这种情况发生?
I feel the problem is at rootGameObjects.AddRange (SceneManager.GetSceneAt (i).GetRootGameObjects ());
我觉得问题出在rootGameObjects.AddRange (SceneManager.GetSceneAt (i).GetRootGameObjects ());
where it takes the root gameObject in a scene but not the selected gameObject in the hierarchy.它采用场景中的根游戏对象,而不是层次结构中的选定游戏对象。
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);
}
}
}
When it comes to Unity, nothing beats reflection:谈到 Unity,没有什么比反射更好的了:
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>();
}
}
Check SceneHierarchyWindow
class for other things you could hook into.检查SceneHierarchyWindow
class 以了解您可以挂钩的其他内容。
Now you have another problem, when you have multiple hierarchy windows open, I'll let you solve that one:)现在你有另一个问题,当你有多个层次 windows 打开时,我会让你解决那个:)
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.