Custom EditorWindow, CreateGUI called before Awake/OnEnable when opening project

Just wanted to check if this is the expected behaviour. Unity Version: 2021.1.0f1 / 2021.1.1f1

I made a custom EditorWindow and noticed that when opening my project in Unity, when my custom window is already open from the start, the execution order is always:

CreateGUI()
Awake()
OnEnable()

If you compare this to manually opening the custom window, the order is:

Awake()
OnEnable()
CreateGUI()

or after Reload Script Assemblies:

OnEnable()
CreateGUI()

So doing initialization stuff, that you need for building the UI, in OnEnable() or Awake() will cause problems when opening the project with your custom window already open, which is the reason I found this unexpected behaviour.

Going by their descriptions in the documentation, I thought Awake() and OnEnable() will always execute before CreateGUI(). Seems a little weird that the rootVisualElement of my window is ready( CreateGUI ) before my window is even open ( Awake ).

It also makes Awake() and OnEnable() not safe for initializing stuff and instead I have to put that into CreateGUI().

So my question is, if this behaviour is correct and if I should do all my initialization in CreateGUI() to make sure it always works?

Thanks

Hello! CreateGUI is a new pattern we created recently to ensure Editor Window initialization is only carried out after the assets finished loading because using the OnEnable pattern (which is what we suggested previously) caused some issues of trying to access a Style Sheet, for example, before it was completely loaded, causing it to be null (other asset types had the same issue, not just UI Toolkit ones).

So, in a nutshell, if you’re using CreateGUI you should not need to use the others, but you’re right that this is unexpected. Can you file a bug with a simple project showing this (like logging a little something on each of the calls) so that we can track it down and fix it? Thanks in advance!

2 Likes

Hi, thanks for the quick response and explanation.

I will move my initialization stuff into CreateGUI then.

I also created an issue and attached a sample project, which logs the order of execution.
I will post the issue id when I receive it.

1 Like

The Unity Team was able to reproduce the issue.
The issue can be tracked here:

1 Like