コガネブログ

平日更新を目標に Unity や C#、Visual Studio、ReSharper などのゲーム開発アレコレを書いていきます

【Unity】UIElements で Scene ビューにツールバーを追加するエディタ拡張のサンプル

ソースコード

using UnityEditor;
using UnityEngine;
using UnityEngine.UIElements;

[InitializeOnLoad]
public static class Example
{
    static Example()
    {
        foreach ( var sceneView in Resources.FindObjectsOfTypeAll<SceneView>() )
        {
            var toolbar           = new VisualElement();
            var style             = toolbar.style;
            var backgroundColor   = new Color32( 203, 203, 203, 255 );
            var rootVisualElement = sceneView.rootVisualElement;

            style.flexDirection   = FlexDirection.Row;
            style.top             = 20;
            style.backgroundColor = new StyleColor( backgroundColor );
            style.height          = 20;

            toolbar.Add( new Label { text = "ピカチュウ" } );
            toolbar.Add( new Button( () => Debug.Log( "ピカチュウ" ) ) { text = "ピカチュウ" } );
            toolbar.BringToFront();

            rootVisualElement.Clear();
            rootVisualElement.Add( toolbar );
        }
    }
}

使用例

適用前

f:id:baba_s:20191023163227p:plain

適用後

f:id:baba_s:20191023163235p:plain