Hierarchy拓展
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEditor;
public class TestHierachy : MonoBehaviour
{
[MenuItem("GameObject/Function2",false,0)]
static void Function2()
{
Debug.Log("运行成功");
}
[InitializeOnLoadMethod]
static void IntiallizeOnLoad()
{
//Hierarchy面板右侧出现按键操作
EditorApplication.hierarchyWindowItemOnGUI += (instanceID, rect) =>
{
//GUI.Button(new Rect(100, 100, 100, 100), "Test1");
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);
}
}
};
}
}
重写菜单
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEditor;
//重写菜单
public class TestMenuOverride
{
//监听右键事件
[InitializeOnLoadMethod]
static void InitOnLoad()
{
EditorApplication.hierarchyWindowItemOnGUI += (instanceID,rect) =>
{
if(Event.current != null && Event.current.button == 1 && Event.current.type == EventType.MouseUp) {
Debug.Log("鼠标抬起");
//弹出自定义的菜单 "Assets"带标使用的该菜单,可修改为自定义的菜单
EditorUtility.DisplayPopupMenu(new Rect(Event.current.mousePosition.x, Event.current.mousePosition.y,0,0) ,"Assets", null);
//不让事件继续往下传递,不然会弹出原生的菜单
Event.current.Use();//把事件用掉就不会往下传递了
}
};
}
}