Unity编辑器拓展--Hierarchy拓展

Unity编辑器拓展--Hierarchy拓展

前言

Hierarchy视图打开的选项我们发现和GameObject打开的视图是一样的,所以我们拓展Hierachy视图也是对GameObject菜单栏这一选项进行拓展,前文已经介绍过了,具体可以参考Project拓展

Hierarchy拓展

这里我们新建一个脚本来控制Hierarchy拓展:
在这里插入图片描述
这个和我们之前做的Project拓展类似,只不过传参的时候,需要我们传一个实例Id进去而不是guid。
具体代码如下所示:

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

public class TestHierarchy : MonoBehaviour
{
    [MenuItem("GameObject/Hierarchy1", false, 0)]
    static void Test1()
    {
        Debug.Log("Hierarchy1");
    }
    [InitializeOnLoadMethod]
    static void InitializeOnload()
    {
        EditorApplication.hierarchyWindowItemOnGUI += (instanceID, rect) =>
        {

            if (Selection.activeObject != null && instanceID==Selection.activeObject.GetInstanceID())
            {
                rect.x = rect.width - 50;
                rect.width = 100;
                if (GUI.Button(rect, "delete"))
                {
                    //删除场景中的游戏物体
                    GameObject.DestroyImmediate(Selection.activeObject);
                }
            }
        };
    }
}

重写Hierarchy菜单

如果我们想完全弃用之前的Hierarchy菜单从而完全使用自己自定义的Hierarchy应该怎么实现呢?
这里我们首先创建一个脚本来控制Hierarchy的重写
在这里插入图片描述
我们跳出Hierarchy的菜单是在点击右键的时候出现的,所以这里我们需要通过监听右键的点击事件来进行重写。类似于这样:

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

public class TestHierarchyMenu : MonoBehaviour
{
    [InitializeOnLoadMethod]
    static void InitOnLoad() 
    {
        EditorApplication.hierarchyWindowItemOnGUI += (instanceID, rect) =>
        {
            //监听我们鼠标的右键抬起事件
            if (Event.current != null&&Event.current.button==1&&Event.current.type==EventType.MouseUp) 
            {
                Debug.Log("Hierarchy鼠标右键抬起");
            }
        };
    }
}

在这里插入图片描述
我们将这个事件阻断掉就可以实现重写了:

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

public class TestHierarchyMenu : MonoBehaviour
{
    [InitializeOnLoadMethod]
    static void InitOnLoad() 
    {
        EditorApplication.hierarchyWindowItemOnGUI += (instanceID, rect) =>
        {
            //监听我们鼠标的右键抬起事件
            if (Event.current != null&&Event.current.button==1&&Event.current.type==EventType.MouseUp) 
            {
                Debug.Log("Hierarchy鼠标右键抬起");
                //阻断原生的菜单
                Event.current.Use();
                //弹出自定义的菜单
                ///参数1.弹出的位置和大小,2.弹出的自定义的菜单 3.菜单命令
                EditorUtility.DisplayPopupMenu(new Rect(Event.current.mousePosition.x,Event.current.mousePosition.y,100,100),"Assets",null);
            }
        };
    }
}

我们来看一下效果:
发现弹出了Assets菜单:
在这里插入图片描述

  • 1
    点赞
  • 3
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论
QHierarchy is an editor extension that adds several often used functions to hierarchy window: - Displaying the icon of a GameObject - Showing / hiding a GameObject - Locking / unlocking a GameObject - Prevent selection of locked GameObject - Displaying tag and layer of a GameObject - Displaying color label for a GameObject - Displaying the icon of MonoBehaviour script attached to a GameObject - GameObject can be showed / hidden only for edit-time, and the visibility state will return during play-time - Displaying the number of children of a GameObject - Displaying the number of vertices and triangles of the GameObject (can display the number including all children) - Change the colors of icons and labels - Displaying custom icon for any layer - Displaying custom icon for any tag - Displaying prefab connection status - Displaying static flags icon of GameObject - Displaying the error icon (MonoBehaviour script missing / Reference property is null / String property is empty / Callback of event is missing) - Displaying icons of all scripts that attached to a GameObject - Showing / hiding wireframe objects - Enable / Disable MeshRenderer - Order of icons can be changed - Displaying the list of GameObjects in the form of a tree - Option to add right indent (useful if you use other plugins that add another icons to the hierarchy window) - Any feature can be disabled - Source Code Included. Website | Documentation | Contacts Compatible with Unity 4.6.0+ / 5.0.1+ / 2017.1+ / 2018.1+

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

IT学徒.

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值