美文网首页
Hierarchy面板增加激活复选框

Hierarchy面板增加激活复选框

作者: Boyce_Lig | 来源:发表于2022-03-11 16:35 被阅读0次

先上效果图

image.png

附上源码

using UnityEditor;
using UnityEngine;

/// <summary>
/// Hierarchy的编辑器扩展
/// </summary>
public class HierarchyEditor
{
    [InitializeOnLoadMethod]
    static void InitializeOnLoadMethod()
    {
        EditorApplication.hierarchyWindowItemOnGUI += HierarchyWindowOnGUI;
    }


    static void HierarchyWindowOnGUI(int instanceID, Rect selectionRect)
    {

        //Toggle控制Hierarchy上的对象显隐
        Rect rect = new Rect(selectionRect);
        rect.x += (selectionRect.width - 15f);
        rect.width = 15f;

        GameObject go = EditorUtility.InstanceIDToObject(instanceID) as GameObject;
        if (go == null) return;
        bool newActive = go.activeSelf;
        bool active = GUI.Toggle(rect, go.activeSelf, string.Empty);

        if (newActive != active)
        {
            newActive = active;
            go.SetActive(newActive);
            EditorUtility.SetDirty(go);
        }
    }
}

使用方法

  1. 这个脚本命名为HierarchyEditor
  2. 放入带有Editor路径的文件件内

相关文章

网友评论

      本文标题:Hierarchy面板增加激活复选框

      本文链接:https://www.haomeiwen.com/subject/mxthdrtx.html