Skip to content

Instantly share code, notes, and snippets.

@5alamander
Created September 5, 2016 06:25
Show Gist options
  • Save 5alamander/2efc9ba42e8cc2d55a389f1437747f7e to your computer and use it in GitHub Desktop.
Save 5alamander/2efc9ba42e8cc2d55a389f1437747f7e to your computer and use it in GitHub Desktop.
Unity, Custom Menu, in Hierarchy
using UnityEngine;
using UnityEditor;
using System.Collections;
public class MyHierarchyMenu
{
[MenuItem("Window/Test/a")]
static void Test()
{
}
[MenuItem("Window/Test/b")]
static void Test1()
{
}
[MenuItem("Window/Test/c/a")]
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();
}
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment