Skip to content

Instantly share code, notes, and snippets.

Show Gist options
  • Star 35 You must be signed in to star a gist
  • Fork 2 You must be signed in to fork a gist
  • Save hybridherbst/36ae70b6520981c8edc7b478423fae5e to your computer and use it in GitHub Desktop.
Save hybridherbst/36ae70b6520981c8edc7b478423fae5e to your computer and use it in GitHub Desktop.
[RuntimeInitializeOnLoad] Event Order
static Lifecycle() => Debug.Log(Prefix + "Static Constructor");
[RuntimeInitializeOnLoadMethod(RuntimeInitializeLoadType.SubsystemRegistration)] static void Subs() => Debug.Log(Prefix + "Subsystem Registration");
[RuntimeInitializeOnLoadMethod(RuntimeInitializeLoadType.AfterAssembliesLoaded)] static void AfterAsm() => Debug.Log(Prefix + "AfterAssembliesLoaded");
[RuntimeInitializeOnLoadMethod(RuntimeInitializeLoadType.BeforeSplashScreen)] static void BeforeSlash() => Debug.Log(Prefix + "Before Splash");
[RuntimeInitializeOnLoadMethod(RuntimeInitializeLoadType.BeforeSceneLoad)] static void BeforeScene() => Debug.Log(Prefix + "BeforeScene");
private void Awake() => Debug.Log(Prefix + "Awake");
private void OnEnable() => Debug.Log(Prefix + "OnEnable");
[RuntimeInitializeOnLoadMethod(RuntimeInitializeLoadType.AfterSceneLoad)] static void AfterScene() => Debug.Log(Prefix + "AfterSceneLoad");
[RuntimeInitializeOnLoadMethod] static void DefaultLog() => Debug.Log(Prefix + "RuntimeInit Default");
void Start() => Debug.Log("Start");
@hybridherbst
Copy link
Author

hybridherbst commented Mar 8, 2021

Just to clarify, the above line order is the actual order in which these events are called on Windows Editor / Windows Standalone / Android.
The Unity docs mention that the order might be undefined depending on platform, not sure what that means for actual usage, but I guess take the above with a grain of salt.

@cybergen
Copy link

cybergen commented Mar 9, 2021

Thanks for putting this together! How does a static constructor or subsystem registration run before assemblies have been loaded? Doesn't any code you write, even in the global namespace, still get built into a separate assembly?

@hybridherbst
Copy link
Author

@cybergen there's no "BeforeAssembliesLoaded" - I'd assume that AfterAssembliesLoaded is called after all static constructors etc. have been run, but I'd hope for clarity on that from the Unity docs :)

I think in C# static constructors are actually called on-demand, so it might just be that on the first occasion the runtime tries to run any code from this class (e.g. Unity calls the first RuntimeInitializeOnLoad), the static constructor is invoked before that.

@cybergen
Copy link

Oh gotcha. Appreciate the clarification!

@bklooster
Copy link

Thank you very much for compiling this, helped me out out a ton tracking down a bug where the static initialization was happening after Unity lifecycle events!

@SBMNet
Copy link

SBMNet commented Apr 26, 2023

This is one of the top google results for this stuff, so I just wanted to direct people to the uninomicon page for RuntimeInitializeOnLoad: https://uninomicon.com/runtimeinitializeonload

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment