Home > Software design >  Is there a way to check if the game just started, or the scene just loaded
Is there a way to check if the game just started, or the scene just loaded

Time:10-31

I'm trying to make some logic that tells me if the actual Unity game just loaded, or if just SceneManager just loaded scene 0. Is there a way to check this? Thank you!

CodePudding user response:

If you are looking for something that should really happen only a single time when the app is started there are multiple options for this.

Among these e.g.

  • Have a static flag and do thins only once in Awake if the flag is not set
  • Use a dedicated setup scene that is loaded only once when the app starts, finishes your initialization and then goes to your actual fist scene and is never loaded again within that app lifecycle
  • Use [RuntimeInitializeOnLoadMethod] on a static method which is then called once when the first Awake routine has finished

For checking which scene was loaded you can attach a listener to SceneManager.sceneLoaded and check if the loaded scene has Scene.buildIndex == 0.

Note though that this might miss the very first scene load depending on how exactly you subscribe to the event.

CodePudding user response:

Maybe you can check with this unity lifecycle methods according to this link

https://docs.unity3d.com/ScriptReference/SceneManagement.SceneManager-sceneLoaded.html

For example you can write control logic inside the awake function for this.

  • Related