Skip to content

Instantly share code, notes, and snippets.

@baba-s
Created March 22, 2024 11:27
Show Gist options
  • Save baba-s/f19f200be9c0936a2994755768e60ea3 to your computer and use it in GitHub Desktop.
Save baba-s/f19f200be9c0936a2994755768e60ea3 to your computer and use it in GitHub Desktop.
using UnityEditor;
using UnityEditor.ShortcutManagement;
using UnityEngine;
using UnityEngine.UIElements;
internal sealed class ExampleEditorWindow : EditorWindow
{
private readonly struct ShortcutContext : IShortcutContext
{
// この ShortcutContext は ExampleEditorWindow がフォーカスされている時だけ有効になる
bool IShortcutContext.active => focusedWindow is ExampleEditorWindow;
}
// ShortcutContext のインスタンス
private readonly ShortcutContext m_shortcutContext = new();
private Label m_label;
[MenuItem( "Tools/Hoge" )]
private static void Open()
{
GetWindow<ExampleEditorWindow>();
}
private void CreateGUI()
{
m_label = new()
{
text = "ピカチュウ"
};
rootVisualElement.Add( m_label );
}
// ExampleEditorWindow が開いたら ShortcutContext のインスタンスを
// ShortcutManager に登録する
private void OnEnable()
{
ShortcutManager.RegisterContext( m_shortcutContext );
}
// ExampleEditorWindow が閉じたら ShortcutContext のインスタンスを
// ShortcutManager から解除する
private void OnDisable()
{
ShortcutManager.UnregisterContext( m_shortcutContext );
}
// ShortcutContext に紐づくショートカット用の関数
// ここではスペースキーが押されたら呼び出されるように設定している
[Shortcut
(
id: "Example Editor Window/Shortcut Context",
context: typeof( ShortcutContext ),
defaultKeyCode: KeyCode.Space
)]
private static void SampleShortcut( ShortcutArguments args )
{
Debug.Log( args.context );
Debug.Log( args.stage );
// ExampleEditorWindow のラベルのテキストを変更してみる
var exampleEditorWindow = ( ExampleEditorWindow )focusedWindow;
exampleEditorWindow.m_label.text = "ライチュウ";
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment