在unity开发中有时需要快速展开某个选定物体

如果每次都要展开都要点一下小三角或者点一下物体再通过快捷键展开有些麻烦

不如直接通过代码直接展开物体,这样也方便扩展,比如在频繁切换编辑scene时自动展开所有物体,避免还要再点击一下展开,从而减少操作步骤

最终在github上找到了这么一个脚本

https://github.com/sandolkakos/unity-utilities

原理就是通过反射获取到hierarchy窗口,再调用其中展开物体的方法

using System.Collections.Generic;
using System.Reflection;
using UnityEditor;
using UnityEngine;

namespace SandolkakosDigital.EditorUtils
{
    /// <summary>
    /// Editor functionalities from internal SceneHierarchyWindow and SceneHierarchy classes. 
    /// For that we are using reflection.
    /// </summary>
    public static class SceneHierarchyUtility
    {
        /// <summary>
        /// Check if the target GameObject is expanded (aka unfolded) in the Hierarchy view.
        /// </summary>
        public static bool IsExpanded(GameObject go)
        {
            return GetExpandedGameObjects().Contains(go);
        }

        /// <summary>
        /// Get a list of all GameObjects which are expanded (aka unfolded) in the Hierarchy view.
        /// </summary>
        public static List<GameObject> GetExpandedGameObjects()
        {
            object sceneHierarchy = GetSceneHierarchy();

            MethodInfo methodInfo = sceneHierarchy
                .GetType()
                .GetMethod("GetExpandedGameObjects");

            object result = methodInfo.Invoke(sceneHierarchy, new object[0]);

            return (List<GameObject>)result;
        }

        /// <summary>
        /// Set the target GameObject as expanded (aka unfolded) in the Hierarchy view.
        /// </summary>
        public static void SetExpanded(GameObject go, bool expand)
        {
            object sceneHierarchy = GetSceneHierarchy();

            MethodInfo methodInfo = sceneHierarchy
                .GetType()
                .GetMethod("ExpandTreeViewItem", BindingFlags.NonPublic | BindingFlags.Instance);

            methodInfo.Invoke(sceneHierarchy, new object[] { go.GetInstanceID(), expand });
        }

        /// <summary>
        /// Set the target GameObject and all children as expanded (aka unfolded) in the Hierarchy view.
        /// </summary>
        public static void SetExpandedRecursive(GameObject go, bool expand)
        {
            object sceneHierarchy = GetSceneHierarchy();

            MethodInfo methodInfo = sceneHierarchy
                .GetType()
                .GetMethod("SetExpandedRecursive", BindingFlags.Public | BindingFlags.Instance);

            methodInfo.Invoke(sceneHierarchy, new object[] { go.GetInstanceID(), expand });
        }

        private static object GetSceneHierarchy()
        {
            EditorWindow window = GetHierarchyWindow();

            object sceneHierarchy = typeof(EditorWindow).Assembly
                .GetType("UnityEditor.SceneHierarchyWindow")
                .GetProperty("sceneHierarchy")
                .GetValue(window);

            return sceneHierarchy;
        }

        private static EditorWindow GetHierarchyWindow()
        {
            // For it to open, so that it the current focused window.
            EditorApplication.ExecuteMenuItem("Window/General/Hierarchy");
            return EditorWindow.focusedWindow;
        }
    }
}

使用例

展开或折叠自身及所有子物体

void Expend(Transform transform,bool expand)
{
    SceneHierarchyUtility.SetExpanded(transform.gameObject,expand);
    for (int j = 0; j < transform.childCount; j++)
    {
        SceneHierarchyUtility.SetExpanded(transform.GetChild(j).gameObject,expand);
        Expend(transform.GetChild(j),expand);
    }
}
Last modification:September 3rd, 2021 at 06:56 pm