版本:Unity 6 (6000.0)
语言English
  • C#

ShortcutManager.RegisterTag

建议更改

成功!

感谢您帮助我们提高 Unity 文档的质量。虽然我们无法接受所有提交,但我们确实会阅读用户提出的每个建议更改,并在适用时进行更新。

关闭

提交失败

由于某些原因,您的建议更改无法提交。请<a>稍后再试</a>。感谢您抽出时间帮助我们提高 Unity 文档的质量。

关闭

取消

声明

public static void RegisterTag(string tag);

声明

public static void RegisterTag(Enum e);

参数

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。

© . This site is unofficial and not affiliated with Unity Technologies.