using System; using System.Linq.Expressions; using System.Reflection; namespace UnityEditor.Rendering { /// /// Contains a set of method to be able to manage Menu Items for the editor /// static class MenuManager { #region Add Menu Item static Action> s_AddMenuItem = GetAddMenuItemMethod(); static Action> GetAddMenuItemMethod() { MethodInfo addMenuItemMethodInfo = typeof(Menu).GetMethod("AddMenuItem", BindingFlags.Static | BindingFlags.NonPublic); var nameParam = Expression.Parameter(typeof(string), "name"); var shortcutParam = Expression.Parameter(typeof(string), "shortcut"); var checkedParam = Expression.Parameter(typeof(bool), "checked"); var priorityParam = Expression.Parameter(typeof(int), "priority"); var executeParam = Expression.Parameter(typeof(Action), "execute"); var validateParam = Expression.Parameter(typeof(Func), "validate"); var addMenuItemExpressionCall = Expression.Call(null, addMenuItemMethodInfo, nameParam, shortcutParam, checkedParam, priorityParam, executeParam, validateParam); return Expression.Lambda>>( addMenuItemExpressionCall, nameParam, shortcutParam, checkedParam, priorityParam, executeParam, validateParam).Compile(); } /// /// Adds a menu Item to the editor /// /// The path to the menu item /// The shortcut of the menu item /// If the item can have an state, pressed or not /// The priority of the menu item /// The action that will be called once the menu item is pressed /// The action that will be called to know if the menu itme is enabled public static void AddMenuItem(string path, string shortcut, bool @checked, int priority, System.Action execute, System.Func validate) { s_AddMenuItem(path, shortcut, @checked, priority, execute, validate); } #endregion #region Remove Menu Item static Action s_RemoveMenuItem = GetRemoveMenuItemMethod(); static Action GetRemoveMenuItemMethod() { MethodInfo removeMenuItemMethodInfo = typeof(Menu).GetMethod("RemoveMenuItem", BindingFlags.Static | BindingFlags.NonPublic); var nameParam = Expression.Parameter(typeof(string), "name"); return Expression.Lambda>( Expression.Call(null, removeMenuItemMethodInfo, nameParam), nameParam).Compile(); } #endregion /// /// Removes a Menu item from the editor, if the path is not found it does nothing /// /// The path of the menu item to be removed public static void RemoveMenuItem(string path) { s_RemoveMenuItem(path); } } }