GenericMenu 允许您创建自定义上下文菜单和下拉菜单。
下面的示例打开了一个带有按钮的编辑器窗口。单击该按钮可显示上下文菜单,您可以通过该菜单更改应用于窗口中 GUI 的颜色。将示例的内容复制到名为 GenericMenuExample.cs 的脚本中,然后将其置于您项目中的 Editor 文件夹内。
.
using UnityEngine; using UnityEditor;
public class GenericMenuExample : EditorWindow { // open the window from the menu item Example -> GUI Color [MenuItem("Example/GUI Color")] static void Init() { EditorWindow window = GetWindow<GenericMenuExample>(); window.position = new Rect(50f, 50f, 200f, 24f); window.Show(); }
// serialize field on window so its value will be saved when Unity recompiles [SerializeField] Color m_Color = Color.white;
void OnEnable() { titleContent = new GUIContent("GUI Color"); }
// a method to simplify adding menu items void AddMenuItemForColor(GenericMenu menu, string menuPath, Color color) { // the menu item is marked as selected if it matches the current value of m_Color menu.AddItem(new GUIContent(menuPath), m_Color.Equals(color), OnColorSelected, color); }
// the GenericMenu.MenuFunction2 event handler for when a menu item is selected void OnColorSelected(object color) { m_Color = (Color)color; }
void OnGUI() { // set the GUI to use the color stored in m_Color GUI.color = m_Color;
// display the GenericMenu when pressing a button if (GUILayout.Button("Select GUI Color")) { // create the menu and add items to it GenericMenu menu = new GenericMenu();
// forward slashes nest menu items under submenus AddMenuItemForColor(menu, "RGB/Red", Color.red); AddMenuItemForColor(menu, "RGB/Green", Color.green); AddMenuItemForColor(menu, "RGB/Blue", Color.blue);
// an empty string will create a separator at the top level menu.AddSeparator("");
AddMenuItemForColor(menu, "CMYK/Cyan", Color.cyan); AddMenuItemForColor(menu, "CMYK/Yellow", Color.yellow); AddMenuItemForColor(menu, "CMYK/Magenta", Color.magenta); // a trailing slash will nest a separator in a submenu menu.AddSeparator("CMYK/"); AddMenuItemForColor(menu, "CMYK/Black", Color.black);
menu.AddSeparator("");
AddMenuItemForColor(menu, "White", Color.white);
// display the menu menu.ShowAsContext(); } } }
allowDuplicateNames | 允许菜单具有多个同名的菜单项。 |
AddDisabledItem | 向菜单添加已禁用的项。 |
AddItem | 向菜单添加一个项。 |
AddSeparator | 向菜单添加一个分隔符项。 |
DropDown | 在给定屏幕矩形中显示菜单。 |
GetItemCount | 获取菜单中的项数。 |
ShowAsContext | 右键单击时在鼠标下显示菜单。 |
MenuFunction | 回调函数,菜单项选中时调用。 |
MenuFunction2 | 带有用户数据的回调函数,菜单项选中时调用。 |
Did you find this page useful? Please give it a rating:
Thanks for rating this page!
What kind of problem would you like to report?
Thanks for letting us know! This page has been marked for review based on your feedback.
If you have time, you can provide more information to help us fix the problem faster.
Provide more information
You've told us this page needs code samples. If you'd like to help us further, you could provide a code sample, or tell us about what kind of code sample you'd like to see:
You've told us there are code samples on this page which don't work. If you know how to fix it, or have something better we could use instead, please let us know:
You've told us there is information missing from this page. Please tell us more about what's missing:
You've told us there is incorrect information on this page. If you know what we should change to make it correct, please tell us:
You've told us this page has unclear or confusing information. Please tell us more about what you found unclear or confusing, or let us know how we could make it clearer:
You've told us there is a spelling or grammar error on this page. Please tell us what's wrong:
You've told us this page has a problem. Please tell us more about what's wrong:
Thank you for helping to make the Unity documentation better!
Your feedback has been submitted as a ticket for our documentation team to review.
We are not able to reply to every ticket submitted.