コガネブログ

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

【Unity】Unity 再生時に Game ビューをフォーカスするエディタ拡張

ソースコード

using System;
using UnityEditor;

namespace Kogane.Internal
{
    [InitializeOnLoad]
    internal static class AutoFocusGameViewOnPlayModeStateChanged
    {
        private static readonly Type GAME_VIEW_TYPE;

        static AutoFocusGameViewOnPlayModeStateChanged()
        {
            GAME_VIEW_TYPE = typeof( EditorWindow ).Assembly.GetType( "UnityEditor.GameView" );

            EditorApplication.playModeStateChanged -= OnPlayModeStateChanged;
            EditorApplication.playModeStateChanged += OnPlayModeStateChanged;
        }

        private static void OnPlayModeStateChanged( PlayModeStateChange change )
        {
            if ( change != PlayModeStateChange.EnteredPlayMode ) return;
            EditorWindow.FocusWindowIfItsOpen( GAME_VIEW_TYPE );
        }
    }
}