使用 IShortcutContext 通过定义快捷方式变为活动或不活动时的条件来创建您自己的快捷方式上下文。这些条件可以基于各种因素或任何其他相关的上下文信息。
using UnityEditor; using UnityEditor.ShortcutManagement; using UnityEngine; public class ShortcutContextSample : EditorWindow { public class CustomShortcutContext : IShortcutContext { public bool active { get { if (!(focusedWindow is ShortcutContextSample view)) return false; return view.toggleValue; } } } [Shortcut("Custom Shortcut Context/Sample Shortcut", typeof(CustomShortcutContext), KeyCode.Mouse1)] static void SampleShortcut(ShortcutArguments args) { Debug.Log("The sample shortcut was called."); } bool m_ToggleValue = false; public bool toggleValue => m_ToggleValue; CustomShortcutContext m_ShortcutContext = new CustomShortcutContext(); [MenuItem("Window/Custom Editor Window")] public static void ShowWindow() { ShortcutContextSample wnd = GetWindow<ShortcutContextSample>(); wnd.titleContent = new GUIContent("Custom Editor Window"); } void OnGUI() { var content = new GUIContent("Toggle", "Toggle to activate the shortcut context."); m_ToggleValue = EditorGUILayout.Toggle(content, m_ToggleValue); } private void OnEnable() { ShortcutManager.RegisterContext(m_ShortcutContext); } private void OnDisable() { ShortcutManager.UnregisterContext(m_ShortcutContext); } }
active | 表示实现 IShortcutContext 的自定义快捷方式上下文是否处于活动状态。 |