Uninomicon

Documenting the dark corners of the Unity Engine.

User Tools

Site Tools


runtimeinitializeonload

Differences

This shows you the differences between two versions of the page.

Link to this comparison view

Both sides previous revision Previous revision
Next revision
Previous revision
runtimeinitializeonload [2021/05/07 19:28]
bhanson Add footnote that this was also tested in 2020.1.17.f1
runtimeinitializeonload [2022/01/26 04:23] (current)
187.190.156.255
Line 10: Line 10:
   * __//Unity MonoBehaviour.Awake() runs here//__   * __//Unity MonoBehaviour.Awake() runs here//__
   * AfterSceneLoad, Default   * AfterSceneLoad, Default
-  * __//Unity MonoBehaviour.Start() runs here//__ 
  
 Providing no load type defaults to RuntimeInitializeLoadType.AfterSceneLoad, running the function after Awake. 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. ((Tested in Unity 2019.4.19f1, in editor and in Android build))
 +
 +Another important thing is that the method should be static!
 +----
 +
 +===== 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 Loaded((tested in 2021.1.15f1)).
 +
 +Example:
 +<code cs>
 +[RuntimeInitializeOnLoadMethod(RuntimeInitializeLoadType.BeforeSceneLoad)]
 +static void AutoRun() {
 + GameObject obj = new GameObject("New Object", typeof(SomeMonoClass)); // Awake of SomeMonoClass will be called here
 +}
 +</code>
 +----
 +
 +[[https://docs.unity3d.com/ScriptReference/SceneManagement.SceneManager-sceneLoaded.html|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. ((tested in 2021.1.15f1))
runtimeinitializeonload.1620415682.txt.gz ยท Last modified: 2021/05/07 19:28 by bhanson