[转]重写Hierarchy的右键菜单

Hierarchy视图中选择一个游戏对象以后通过右键可以打开一个unity默认菜单,一般情况下都可以满足我们,但是我想真对某些特殊的游戏对象而展开特殊的菜单。如下图所示,比如这样:

[转]重写Hierarchy的右键菜单_第1张图片
Unity3D研究院编辑器之重写Hierarchy的右键菜单(二十二) - 雨松MOMO程序研究院 - 1

代码:

using UnityEngine;
using UnityEditor;
using System.Collections;
 
public class MyHierarchyMenu
{
    [MenuItem("Window/Test/yusong")]
    static void Test()
    {
    }
 
    [MenuItem("Window/Test/momo")]
    static void Test1()
    {
    }
    [MenuItem("Window/Test/雨松/MOMO")]
    static void Test2()
    {
    }
 
 
    [InitializeOnLoadMethod]
     static void StartInitializeOnLoadMethod()
     {
         EditorApplication.hierarchyWindowItemOnGUI += OnHierarchyGUI;
     }
 
     static void OnHierarchyGUI(int instanceID, Rect selectionRect)
     {
         if (Event.current != null && selectionRect.Contains(Event.current.mousePosition)
             && Event.current.button == 1 && Event.current.type <= EventType.mouseUp)
         {
             GameObject selectedGameObject = EditorUtility.InstanceIDToObject(instanceID) as GameObject;
             //这里可以判断selectedGameObject的条件
             if (selectedGameObject)
             {
                Vector2 mousePosition = Event.current.mousePosition;
 
                EditorUtility.DisplayPopupMenu(new Rect(mousePosition.x, mousePosition.y, 0, 0), "Window/Test",null);
                Event.current.Use();
             }          
         }
     }
 
}

你可能感兴趣的:([转]重写Hierarchy的右键菜单)