コガネブログ

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

【Unity】UIElements で Console ウィンドウにボタンを追加するエディタ拡張のサンプル

ソースコード

using System.Linq;
using UnityEditor;
using UnityEngine;
using UnityEngine.UIElements;

[InitializeOnLoad]
public static class Example
{
    static Example()
    {
        EditorApplication.delayCall += () =>
        {
            var assembly      = typeof( EditorWindow ).Assembly;
            var type          = assembly.GetType( "UnityEditor.ConsoleWindow" );
            var consoleWindow = Resources.FindObjectsOfTypeAll( type ).FirstOrDefault() as EditorWindow;

            if ( consoleWindow == null ) return;

            var toolbar           = new VisualElement();
            var style             = toolbar.style;
            var rootVisualElement = consoleWindow.rootVisualElement;

            style.flexDirection = FlexDirection.Row;
            style.top           = 20;
            style.height        = 20;

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

            rootVisualElement.Add( toolbar );
        };
    }
}

関連記事