コガネブログ

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

【Unity】Unity プロジェクトを開いた瞬間に終了するエディタ拡張

概要

using System.Reflection;
using UnityEditor;
using UnityEngine.Events;

public static class EditorApplicationUtility
{
    private const BindingFlags BINDING_ATTR = BindingFlags.Static | BindingFlags.Instance | BindingFlags.NonPublic;

    private static readonly FieldInfo m_info = typeof( EditorApplication ).GetField( "projectWasLoaded", BINDING_ATTR );

    public static UnityAction projectWasLoaded
    {
        get
        {
            return m_info.GetValue( null ) as UnityAction;
        }
        set
        {
            var functions = m_info.GetValue( null ) as UnityAction;
            functions += value;
            m_info.SetValue( null, functions );
        }
    }
}

public static class Example
{
    [InitializeOnLoadMethod]
    private static void Hoge()
    {
        EditorApplicationUtility.projectWasLoaded += () =>
        {
            EditorApplication.Exit( 0 );
        };
    }
}