Advertisement
KpoKec

Custom contex menu in Unity editor over any element

Sep 22nd, 2018
180
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 1.19 KB | None | 0 0
  1. // Пример использования - проверяет ПКМ па последнему отрисованному элементу
  2. CheckContextMenu(ShowMenu);
  3.  
  4. // Метод вызова меню
  5.     private bool CheckContextMenu(Action menuMethod)
  6.     {
  7.         var w = EditorWindow.mouseOverWindow;
  8.         if (w == null) return false;
  9.         Event current = Event.current;
  10.         if (current.isMouse && current.button == 1)
  11.             if (GUILayoutUtility.GetLastRect().Contains(current.mousePosition))
  12.             {
  13.                 menuMethod.Invoke();
  14.                 return true;
  15.             }
  16.         return false;
  17.     }
  18.  
  19. // Пример меню
  20.     void ShowMenu()
  21.     {
  22.         GenericMenu menu = new GenericMenu();
  23.         menu.AddDisabledItem(new GUIContent("Actor action menu"));
  24.         menu.AddItem(new GUIContent("Add"), false, AddActorAction);
  25.         menu.AddItem(new GUIContent("Clone"), false, CloneActorAction);
  26.         menu.AddItem(new GUIContent("Delete"), false, DeleteActorAction);
  27.         menu.ShowAsContext();
  28.     }
  29.  
  30. // Пункты меню
  31.     void AddActorAction()
  32.     {}
  33.     void CloneActorAction()
  34.     {}
  35.     void DeleteActorAction()
  36.     {}
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement