-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathMenuManager.cs
More file actions
85 lines (78 loc) · 3.86 KB
/
MenuManager.cs
File metadata and controls
85 lines (78 loc) · 3.86 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
using System;
using System.Linq.Expressions;
using System.Reflection;
using UnityEditor;
namespace PKGE.Editor
{
/// <summary>
/// Contains a set of method to be able to manage Menu Items for the editor
/// </summary>
public static class MenuManager
{
//https://github.com/Unity-Technologies/Graphics/blob/504e639c4e07492f74716f36acf7aad0294af16e/Packages/com.unity.render-pipelines.core/Editor/MenuManager.cs
#region UnityEditor.Rendering
#region Add Menu Item
static Action<string, string, bool, int, Action, Func<bool>> _addMenuItem;
static Action<string, string, bool, int, Action, Func<bool>> AddMenuItemMethod => _addMenuItem ??= GetAddMenuItemMethod();
static Action<string, string, bool, int, Action, Func<bool>> 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<bool>), "validate");
var addMenuItemExpressionCall = Expression.Call(null, addMenuItemMethodInfo,
nameParam,
shortcutParam,
checkedParam,
priorityParam,
executeParam,
validateParam);
return Expression.Lambda<Action<string, string, bool, int, Action, Func<bool>>>(
addMenuItemExpressionCall,
nameParam,
shortcutParam,
checkedParam,
priorityParam,
executeParam,
validateParam).Compile();
}
/// <summary>
/// Adds a menu Item to the editor
/// </summary>
/// <param name="path">The path to the menu item</param>
/// <param name="shortcut">The shortcut of the menu item</param>
/// <param name="checked">If the item can have a state, pressed or not</param>
/// <param name="priority">The priority of the menu item</param>
/// <param name="execute">The action that will be called once the menu item is pressed</param>
/// <param name="validate">The action that will be called to know if the menu item is enabled</param>
public static void AddMenuItem(string path, string shortcut, bool @checked, int priority, System.Action execute, System.Func<bool> validate)
{
AddMenuItemMethod(path, shortcut, @checked, priority, execute, validate);
}
#endregion
#region Remove Menu Item
static Action<string> _removeMenuItem;
static Action<string> RemoveMenuItemMethod => _removeMenuItem ??= GetRemoveMenuItemMethod();
static Action<string> GetRemoveMenuItemMethod()
{
MethodInfo removeMenuItemMethodInfo = typeof(Menu).GetMethod("RemoveMenuItem", BindingFlags.Static | BindingFlags.NonPublic);
var nameParam = Expression.Parameter(typeof(string), "name");
return Expression.Lambda<Action<string>>(
Expression.Call(null, removeMenuItemMethodInfo, nameParam),
nameParam).Compile();
}
#endregion
/// <summary>
/// Removes a Menu item from the editor, if the path is not found it does nothing
/// </summary>
/// <param name="path">The path of the menu item to be removed</param>
public static void RemoveMenuItem(string path)
{
RemoveMenuItemMethod(path);
}
#endregion // UnityEditor.Rendering
}
}