1
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

More than 3 years have passed since last update.

【Unity】エディター拡張の右クリック

Posted at

概要

エディター拡張時に右クリックのメニューをやってみました。
新しいUIElementsとGUI システム (IMGUI)の両方のパターンがあります。

UIElementsの場合

// ラベルを右クリックした時の動作
VisualElement label = new Label("Hello World! From C#");
label.RegisterCallback<MouseDownEvent>((evt) =>
{
  if (Event.current.button == 0)
    {
        // 左クリック
    }
    else if (Event.current.button == 1)
    {
        GenericMenu menu = new GenericMenu();
        menu.AddItem(new GUIContent("右クリックメニュー"), false, () => Debug.Log("右クリックメニュー"));
        menu.ShowAsContext();
    }
});

image.png

UI システム (IMGUI)の場合

if(GUILayout.Button("ボタン"))
{
    if (Event.current.button == 0)
    {
        // 左クリック
    }
    else if (Event.current.button == 1)
    {
        // 右クリック
        GenericMenu menu = new GenericMenu();
        menu.AddItem(new GUIContent("右クリックメニュー"), false, () => Debug.Log("右クリックメニュー"));
        menu.ShowAsContext();
    }
}

image.png

終わりに

どちらにしても判定の後にEvent.current.buttonで分けるみたい。

1
0
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
1
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?