Uninomicon

Documenting the dark corners of the Unity Engine.

User Tools

Site Tools


runtimeinitializeonload

This is an old revision of the document!


RuntimeInitializeOnLoadMethod

The documentation is not correct about this function running after awake. It does by default, but not if you specify a load type:

The order of callbacks is: 1)

  • SubsystemRegistration
  • AfterAssembliesLoad
  • BeforeSplashScreen2)
  • BeforeSceneLoad
  • Unity MonoBehaviour.Awake() runs here
  • AfterSceneLoad, Default

Providing no load type defaults to RuntimeInitializeLoadType.AfterSceneLoad, running the function after Awake.

Other versions that have been verified to also use this exact order:

  • 2021.1.12f1 (editor only)

Methods with RuntimeInitializeLoadType.AfterSceneLoad, or RuntimeInitializeLoadType.BeforeSceneLoad will only be called for the first scene in a run of the application, not every scene. 3)


Other Nuanced Info

If you create a GameObject and attach a MonoBehaviour to it in “BeforeSceneLoad”, or another early LoadType, the Awake function of that MonoBehavior WILL be called at the point of creating the GameObject. In other words, the Awake will be called earlier than the Awakes of GameObjects in Loaded4).

Example:

[RuntimeInitializeOnLoadMethod(RuntimeInitializeLoadType.BeforeSceneLoad)]
static void AutoRun() {
	GameObject obj = new GameObject("New Object", typeof(SomeMonoClass)); // Awake of SomeMonoClass will be called here
}

SceneManager.sceneLoaded event subscribers will be called before AfterSceneLoad. By the name of the event, you might assume that the event is called after the scene was loaded, but it appears to be called just before. 5)

1)
tested in the editor, 2020.2.1f1 and 2020.1.17f1. Also tested in editor and in build in 2019.4.12f1
2)
On 2019, this call didn't seem to run in the editor, I think this was fixed with the addition of the previous two load types.
3)
Tested in Unity 2019.4.19f1, in editor and in Android build
4) , 5)
tested in 2021.1.15f1
runtimeinitializeonload.1630421767.txt.gz · Last modified: 2021/08/31 14:56 by 73.95.178.156