コガネブログ

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

【Unity】Addressable Asset System の AddressableAssetsWindow を再描画するエディタ拡張

using System.Reflection;
using UnityEditor;
using UnityEngine;

public static class Example
{
    [MenuItem( "Tools/Hoge" )]
    public static void Repaint()
    {
        const BindingFlags attr                = BindingFlags.Instance | BindingFlags.Public | BindingFlags.NonPublic;
        const string       assemblyName        = "Unity.Addressables.Editor";
        const string       windowTypeName      = "UnityEditor.AddressableAssets.GUI.AddressableAssetsWindow";
        const string       groupEditorTypeName = "UnityEditor.AddressableAssets.GUI.AddressableAssetsSettingsGroupEditor";

        var assembly   = Assembly.Load( assemblyName );
        var windowType = assembly.GetType( windowTypeName );
        var windows    = Resources.FindObjectsOfTypeAll( windowType );
        var isOpen     = 1 <= windows.Length;

        // AddressableAssetsWindow が表示されていない場合は処理をスキップ
        if ( !isOpen ) return;

        var window           = windows[ 0 ] as EditorWindow;
        var groupEditorType  = assembly.GetType( groupEditorTypeName );
        var groupEditorField = windowType.GetField( "m_GroupEditor", attr );
        var method           = groupEditorType.GetMethod( "Reload", attr );
        var groupEditor      = groupEditorField.GetValue( window );

        method.Invoke( groupEditor, new object[0] );
        window.Repaint();
    }
}