#Earliest executable code possible ?

1 messages · Page 1 of 1 (latest)

earnest prawn
#

Hello everyone. Quick question, I think...

From the point you launch the executable of a Unity game, when's the earliest possible moment you can execute a line of code?

Is it simply the Awake of a MonoBehaviour within the first scene?

Is it possible to do so before the first scene is loaded?

#

Context doesn't matter. I'm asking this purely as a theoretical question.

sharp walrus
#

maybe in a static constructor

#

although I think that only gets invoked the first time you interact with the class

#

"It is called automatically before the first instance is created or any static members are referenced"

#

unclear

earnest prawn
#

hmmm. Thanks for the suggestion! Will test it.

scarlet crater
earnest prawn
#

@scarlet crater Yeah, I saw that... What confused me about it is in the documentation for RuntimeInitializeOnLoadMethodAttribute, it says:

This is after the Awake method has been invoked.
https://docs.unity3d.com/ScriptReference/RuntimeInitializeOnLoadMethodAttribute.html

For an Awake method to be invoked, I assume there would need to be a MonoBehaviour, which implies that there would need to be a scene already loaded. I'm not sure. I find the verbiage used a little confusing.

scarlet crater
earnest prawn
#

Yeah, I saw that too. You'd think that before the scene or the splash screen is loaded, none of the Awakes would be invoked, right? But then it explicitly mentions this in the documentation:

Methods marked [RuntimeInitializeOnLoadMethod] are invoked after the game has been loaded. This is after the Awake method has been invoked.

Does that imply that Awake is called on MonoBehaviours before the scene they are contained within is even loaded?

#

@scarlet crater

scarlet crater
earnest prawn
#

Ahhhh, of course. Sorry for the dumb line of questioning.

scarlet crater
#

The best way you know is to test it.

earnest prawn
#

@scarlet crater
If you're curious, here are the results using System.Diagnostics.Stopwatch.
So basically, static constructor seems to be the first point at which you can start executing code.

Stopwatch started in static constructor
SubsystemRegistration completed in 00:00:00.00
AfterAssembliesLoaded completed in 00:00:00.00
BeforeSplashScreen completed in 00:00:00.06
BeforeSceneLoaded completed in 00:00:05.07
First scene Awake completed in 00:00:00.06
First scene Start completed in 00:00:00.52