如何在 Unity Editor 中绘制自定义菜单
1个回答
展开全部
using UnityEngine;
using UnityEditor;
using System.Collections;
// This example shows how to create a context menu inside a custom EditorWindow.
/慎虚培/ context-click the green area to show the menu
public class GenericMenuExample : EditorWindow
{
[MenuItem("Example/Open Window")]
static void Init()
{
var window = GetWindow<GenericMenuExample>();
window.position = new Rect(50, 50, 250, 60);
window.Show();
}
void Callback(object obj)
{
Debug.Log("Selected: " + obj);
}
void OnGUI()
{
Event currentEvent = Event.current;
Rect contextRect = new Rect(10, 10, 100, 100);
EditorGUI.DrawRect(contextRect, Color.green);
if (currentEvent.type == EventType.ContextClick)
{
Vector2 mousePos = currentEvent.mousePosition;
if (contextRect.Contains(mousePos))
{
// Now create the menu, add items and show it
GenericMenu menu = new GenericMenu();
menu.AddItem(new GUIContent("MenuItem1"), false, Callback, "item 1");
menu.AddItem(new GUIContent("MenuItem2"), false, Callback, "item 2");
menu.AddSeparator(""宽唯);
menu.AddItem(new GUIContent("SubMenu/MenuItem3"), false, Callback, "item 3");
menu.ShowAsContext();
currentEvent.Use();
}
}
}
}
using UnityEngine;
using UnityEditor;
using System.Collections;
// This example shows how to create a context menu inside a custom EditorWindow.
// context-click the green area to show the menu
public class GenericMenuExample : EditorWindow
{
[MenuItem("Example/Open Window"誉数)]
static void Init()
{
var window = GetWindow<GenericMenuExample>();
window.position = new Rect(50, 50, 250, 60);
window.Show();
}
void Callback(object obj)
{
Debug.Log("Selected: " + obj);
}
void OnGUI()
{
Event currentEvent = Event.current;
RectcontextRect = new Rect(10, 10, 100, 100);
EditorGUI.DrawRect(contextRect, Color.green);
if (currentEvent.type == EventType.ContextClick)
{
Vector2mousePos = currentEvent.mousePosition;
if (contextRect.Contains(mousePos))
{
// Now create the menu, add items and show it
GenericMenumenu = new GenericMenu();
menu.AddItem(new GUIContent("MenuItem1"), false, Callback, "item 1");
menu.AddItem(new GUIContent("MenuItem2"), false, Callback, "item 2");
menu.AddSeparator("");
menu.AddItem(new GUIContent("SubMenu/MenuItem3"), false, Callback, "item 3");
menu.ShowAsContext();
currentEvent.Use();
}
}
}
}
using UnityEditor;
using System.Collections;
// This example shows how to create a context menu inside a custom EditorWindow.
/慎虚培/ context-click the green area to show the menu
public class GenericMenuExample : EditorWindow
{
[MenuItem("Example/Open Window")]
static void Init()
{
var window = GetWindow<GenericMenuExample>();
window.position = new Rect(50, 50, 250, 60);
window.Show();
}
void Callback(object obj)
{
Debug.Log("Selected: " + obj);
}
void OnGUI()
{
Event currentEvent = Event.current;
Rect contextRect = new Rect(10, 10, 100, 100);
EditorGUI.DrawRect(contextRect, Color.green);
if (currentEvent.type == EventType.ContextClick)
{
Vector2 mousePos = currentEvent.mousePosition;
if (contextRect.Contains(mousePos))
{
// Now create the menu, add items and show it
GenericMenu menu = new GenericMenu();
menu.AddItem(new GUIContent("MenuItem1"), false, Callback, "item 1");
menu.AddItem(new GUIContent("MenuItem2"), false, Callback, "item 2");
menu.AddSeparator(""宽唯);
menu.AddItem(new GUIContent("SubMenu/MenuItem3"), false, Callback, "item 3");
menu.ShowAsContext();
currentEvent.Use();
}
}
}
}
using UnityEngine;
using UnityEditor;
using System.Collections;
// This example shows how to create a context menu inside a custom EditorWindow.
// context-click the green area to show the menu
public class GenericMenuExample : EditorWindow
{
[MenuItem("Example/Open Window"誉数)]
static void Init()
{
var window = GetWindow<GenericMenuExample>();
window.position = new Rect(50, 50, 250, 60);
window.Show();
}
void Callback(object obj)
{
Debug.Log("Selected: " + obj);
}
void OnGUI()
{
Event currentEvent = Event.current;
RectcontextRect = new Rect(10, 10, 100, 100);
EditorGUI.DrawRect(contextRect, Color.green);
if (currentEvent.type == EventType.ContextClick)
{
Vector2mousePos = currentEvent.mousePosition;
if (contextRect.Contains(mousePos))
{
// Now create the menu, add items and show it
GenericMenumenu = new GenericMenu();
menu.AddItem(new GUIContent("MenuItem1"), false, Callback, "item 1");
menu.AddItem(new GUIContent("MenuItem2"), false, Callback, "item 2");
menu.AddSeparator("");
menu.AddItem(new GUIContent("SubMenu/MenuItem3"), false, Callback, "item 3");
menu.ShowAsContext();
currentEvent.Use();
}
}
}
}
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询