暂无描述
您最多选择25个主题 主题必须以字母或数字开头,可以包含连字符 (-),并且长度不得超过35个字符

MenuManager.cs 3.4KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879
  1. using System;
  2. using System.Linq.Expressions;
  3. using System.Reflection;
  4. namespace UnityEditor.Rendering
  5. {
  6. /// <summary>
  7. /// Contains a set of method to be able to manage Menu Items for the editor
  8. /// </summary>
  9. static class MenuManager
  10. {
  11. #region Add Menu Item
  12. static Action<string, string, bool, int, Action, Func<bool>> s_AddMenuItem = GetAddMenuItemMethod();
  13. static Action<string, string, bool, int, Action, Func<bool>> GetAddMenuItemMethod()
  14. {
  15. MethodInfo addMenuItemMethodInfo = typeof(Menu).GetMethod("AddMenuItem", BindingFlags.Static | BindingFlags.NonPublic);
  16. var nameParam = Expression.Parameter(typeof(string), "name");
  17. var shortcutParam = Expression.Parameter(typeof(string), "shortcut");
  18. var checkedParam = Expression.Parameter(typeof(bool), "checked");
  19. var priorityParam = Expression.Parameter(typeof(int), "priority");
  20. var executeParam = Expression.Parameter(typeof(Action), "execute");
  21. var validateParam = Expression.Parameter(typeof(Func<bool>), "validate");
  22. var addMenuItemExpressionCall = Expression.Call(null, addMenuItemMethodInfo,
  23. nameParam,
  24. shortcutParam,
  25. checkedParam,
  26. priorityParam,
  27. executeParam,
  28. validateParam);
  29. return Expression.Lambda<Action<string, string, bool, int, Action, Func<bool>>>(
  30. addMenuItemExpressionCall,
  31. nameParam,
  32. shortcutParam,
  33. checkedParam,
  34. priorityParam,
  35. executeParam,
  36. validateParam).Compile();
  37. }
  38. /// <summary>
  39. /// Adds a menu Item to the editor
  40. /// </summary>
  41. /// <param name="path">The path to the menu item</param>
  42. /// <param name="shortcut">The shortcut of the menu item</param>
  43. /// <param name="checked">If the item can have an state, pressed or not</param>
  44. /// <param name="priority">The priority of the menu item</param>
  45. /// <param name="execute">The action that will be called once the menu item is pressed</param>
  46. /// <param name="validate">The action that will be called to know if the menu itme is enabled</param>
  47. public static void AddMenuItem(string path, string shortcut, bool @checked, int priority, System.Action execute, System.Func<bool> validate)
  48. {
  49. s_AddMenuItem(path, shortcut, @checked, priority, execute, validate);
  50. }
  51. #endregion
  52. #region Remove Menu Item
  53. static Action<string> s_RemoveMenuItem = GetRemoveMenuItemMethod();
  54. static Action<string> GetRemoveMenuItemMethod()
  55. {
  56. MethodInfo removeMenuItemMethodInfo = typeof(Menu).GetMethod("RemoveMenuItem", BindingFlags.Static | BindingFlags.NonPublic);
  57. var nameParam = Expression.Parameter(typeof(string), "name");
  58. return Expression.Lambda<Action<string>>(
  59. Expression.Call(null, removeMenuItemMethodInfo, nameParam),
  60. nameParam).Compile();
  61. }
  62. #endregion
  63. /// <summary>
  64. /// Removes a Menu item from the editor, if the path is not found it does nothing
  65. /// </summary>
  66. /// <param name="path">The path of the menu item to be removed</param>
  67. public static void RemoveMenuItem(string path)
  68. {
  69. s_RemoveMenuItem(path);
  70. }
  71. }
  72. }