创建自定义的右键菜单

1.当前的控件的Rect

2.当前事件是鼠标按下事件,并且是按下的鼠标右键

3.右键点击位置在Rect内

 1 void MyContexMenu(Rect rect)
 2 {
 3     if(Event.current.type == EventType.MouseDown &&
 4         Event.current.button == 1 &&
 5         rect.Contains(Event.current.mousePosition))
 6     {
 7         GenericMenu menu = new GenericMenu();
 8         menu.AddItem(new GUIContent("Menu 1"), false, callback, null);
 9         menu.AddItem(new GUIContent("Menu 2/Sub Menu 1"), false, callback2);
10         menu.ShowAsContext();
11         Event.current.Use();
12     }
13 }

public void AddItem(GUIContent content, bool on, MenuFunction func);
public void AddItem(GUIContent content, bool on, MenuFunction2 func, object userData);

参数1:菜单的标题,支持路径

参数2:如果为true,则菜单名称前会打勾

参数3:回调方法

参数4:传递给回调方法的参数


**注意**

当EventType用ContexClick时,不起作用;

有时不起作用可能是点击事件没能传递下去,被上层的组件使用了当前的鼠标事件。

猜你喜欢

转载自www.cnblogs.com/stan-si/p/10623496.html