tag | 上下文字符串标识符。 |
e | 上下文枚举标识符。 |
将标签注册为自定义上下文,用于在确定窗口上下文后筛选快捷方式。
使用此方法定义跨编辑器窗口的上下文,或包含某些窗口状态和 GUI 控件。
using UnityEditor; using UnityEditor.ShortcutManagement; using UnityEngine;
public class MyWindow : EditorWindow { const KeyCode shortcutKey = KeyCode.A;
public enum MyTool { EmptyTool, ShortcutTool }
MyTool tool; MyTool Tool { get => tool; set { if (tool == value) return;
ShortcutManager.RegisterTag(tool = value); Debug.Log($"{tool} tool registered"); } }
bool customTag; bool CustomTag { get => customTag; set { if (customTag == value) return;
customTag = value; var tag = nameof(customTag);
if (customTag) { ShortcutManager.RegisterTag(tag); Debug.Log($"{tag} enabled"); } else { ShortcutManager.UnregisterTag(tag); Debug.Log($"{tag} disabled"); } } }
[Shortcut("Tags/No Tag", typeof(MyWindow), shortcutKey)] static void NoTagShortcut() { Debug.Log($"Shortcut for MyWindow without tag context executed"); }
[Shortcut("Tags/Shortcut Tool", typeof(MyWindow), "MyTool.ShortcutTool", shortcutKey)] static void ShortcutToolShortcut() { Debug.Log($"Shortcut for MyWindow with 'MyTool.ShortcutTool' tag context executed"); }
[Shortcut("Tags/Custom Tag", typeof(MyWindow), nameof(customTag), shortcutKey)] static void CustomTagShortcut() { Debug.Log($"Shortcut for MyWindow with {nameof(customTag)} tag context executed"); }
[MenuItem("Window/My Window")] static void OpenWindow() => EditorWindow.GetWindow(typeof(MyWindow)).Show();
void OnGUI() { EditorGUILayout.LabelField("Tools"); Tool = (MyTool)EditorGUILayout.EnumPopup("Custom Tool", Tool); CustomTag = EditorGUILayout.Toggle("Custom Tag", CustomTag);
EditorGUILayout.Space(); EditorGUILayout.LabelField($"The default binding for window shortcuts is {shortcutKey}"); } }
使用这些标签时,请注意不要留下已过时的标签注册。这样做会导致多个可用的快捷方式,从而打开快捷方式冲突窗口。使用 UnregisterTag 取消注册标签。
如果使用枚举重载,Unity 会自动清除该枚举先前值的标签。
Unity 还为某些枚举提供内置支持。例如,UnityEditor.Tool 和 UnityEditor.ViewTool。
其他资源:UnregisterTag。