6
5

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

Unity のメインツールバーをカスタマイズ(2024年版)

Last updated at Posted at 2024-08-12

@iixd_pog さんの投稿を参考に UI Toolkit 側によせる対応を行ったモノです。従来の IMGUI(EditorGUILayout系)も引き続き使えます。

動作確認: Unity 2021 LTS ~ Unity 6 Preview

Unity Editor Main Toolbar

UnityEditorMainToolbar

Left Center Right 3つのプロパティーを提供するだけのプリミティブなユーティリティーです。

UI Toolkit の利用が前提ですが IMGUIContainer を通して (Editor)GUILayout 系の機能も使えます。

// InitializeOnLoadMethod と組み合わせて使う場合は遅延実行が必要
EditorApplication.delayCall += () =>
{
    // 左右のエリアにラベルを追加
    UnityEditorMainToolbar.Left.Add(new Label("Left"));
    UnityEditorMainToolbar.Right.Add(new Label("Right"));

    // 中央エリアに追加
    UnityEditorMainToolbar.Center.Add(new Label("Center-Right"));
    UnityEditorMainToolbar.Center.Insert(0, new Label("Center-Left"));

    // GUILayout 系のメソッドを使う(IMGUIContainer)
    UnityEditorMainToolbar.Center.Add(new IMGUIContainer(() =>
    {
        if (GUILayout.Button("IMGUI Button",
                             EditorStyles.toolbarButton))  // 標準ボタンに見た目を揃える
        {
            EditorUtility.DisplayDialog("DEBUG", "IMGUI Works!!", "Close");
        }
    }));
};

とにかく余計なことは何もしない、Unity のメインツールバーへのアクセスを提供するだけクラスなので「良い感じのマージン」等は自前で設定する必要があります。

(冒頭のスクショではボタンの左右に GUILayout.Space(7) を配置)

また、一切の配慮がない VisualElement への直アクセスでなので、ツールバーの要素を全て消す等あらゆる操作が可能です。

// ボタンとか全部消す!
while (UnityEditorMainToolbar.Left.childCount > 0)
    UnityEditorMainToolbar.Left.RemoveAt(0);

while (UnityEditorMainToolbar.Center.childCount > 0)
    UnityEditorMainToolbar.Center.RemoveAt(0);

while (UnityEditorMainToolbar.Right.childCount > 0)
    UnityEditorMainToolbar.Right.RemoveAt(0);

// Unity を再起動するかアセンブリーを再コンパイルすれば元に戻る

ソースコード

テストコードの方が長い!

参考

いつもありがとうございます。

--

こういう細かいけど全プロジェクトにあったら嬉しいスクリプトは扱いに困りますね。

以上です。お疲れ様でした。

6
5
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
6
5

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?