Unity 2021 - issue showing EditorWindow with [InitializeOnLoadMethod]

Hi,

I have an asset which needs to show a custom EditorWindow when the user first imports the asset. This has been working fine in 2018.4 - 2020 with the InitializeOnLoadMethod attribute but throws an exception in v2021.

    public class MyWindow : EditorWindow
    {
        [InitializeOnLoadMethod]
        public static void ShowMyWindow()
        {
            ShowWindow();
        }       
        static public void ShowWindow()
        {
            var window = GetWindow(typeof(MyWindow), true, "My Window");
            window.Show();
        }
    }

i’ve tried creating the window via " ScriptableObject.CreateInstance(typeof(MyWindow)) " but get the same issue.

When searching I can’t find anything that indicates why this issue is introduced in v2021, nor any way of solving it. It seems the window is not “ready” to be created when InitializeOnLoad fires.

The full error I get in Unity’s console is attached. Any ideas appreciated.

Many thanks,
shwhjw.

7552378–933760–error.txt (6 KB)

Can you isolate it in a tiny project and prove it? Because if so, that’s an awesome bug to report through the usual mechanism in Unity, which unfortunately sends your ENTIRE PROJECT to Unity, which is why you want to make it a tiny empty project.

Tested, it works prior to 2021 and in 2022.1 as well. So since I had my repro test project I also submitted the bug report.

Case #1371215.

1 Like

Turns out, the fix is already under way, I couldn’t find it for some reason, QA answered.

2 Likes