- Home /
Appropriate way to force the loading of a particular scene at startup
I'm trying to force the application to load a particular Scene "loading" whenever the player is started.
For this I use a static class like so:
[InitializeOnLoad]
public class Autorun
{
static Autorun()
{
String levelName = "loading";
Debug.Log("Autorun will load " + levelName);
Application.LoadLevel(levelName);
}
}
Unfortunately the Application.LoadLevel(levelName); seems to be completely ignored.
If this is to be expected I would like to know how I can achieve the Load at startup.
edit: The static constructor is executed at startup. I suspect Application is not ready when I call the LoadLevel
which would explain this behaviour. How should I proceed to run my code (save and preferences loading, network queries ad initial scene display) as soon as Application has finished initialising its dependencies?
Thanks.
I added an answer that totally misread the static constructor and took it as a static method call without a return type. :) This was plainly wrong - I've deleted the answer again. Sorry about the inconvenience. :P
As a guess: Perhaps the call to Application.LoadLevel fails because the Unity runtime isn't in a state where it is ready to receive and handle Application.LoadLevel at the time the .Net runtime executes static constructors?
@CHPedersen Indeed. I too suspect the Application is still initialising or maybe has not even started yet. Do you know if there is some sort of callback system to get notified when the Application is ready?
I think I'd try to stick to executing code in the $$anonymous$$onoBehavior callbacks. It seems Unity's design expects us to use these as entry-points for user-code rather than static constructors.
There is a callback, OnLevelWasLoaded which is called whenever a level is loaded, and the application thus presumably ready. But again, this requires you to construct some scene with a gameobject to host the script that handles this callback, in which case you might as well stick your code in that script's Awake-method. This, I believe, is the very first thing that gets called when an object is first initialized.
You could create a scene specifically for loading and then use PlayerPrefs to store which scene to load on startup. It could just be a quad with 'Loading' on it along with the loader script and a camera pointed at it.
In case it's not obvious or already known to you, Unity already has a way to define which scene gets loaded when a build of your app is executed - it's the first scene listed in the "Build Settings" dialog.
It doesn't affect what happens when you press "Play" in the editor - that always launches the currently-loaded scene. But you can fix that if you want by having an object in your current scene which checks whether the "boot" scene was already loaded, and if not, loads it straight away.
Answer by Xtro · Jan 29, 2014 at 03:32 PM
You don't need to write code for that. Just add the Loading scene as the first scene in the build window.
Your answer
Follow this Question
Related Questions
Preventing first instance of object from starting on screen 1 Answer
Android game does not load fully sometimes 1 Answer
how to seperate two visually distinct game modes 1 Answer
Loading Sene problem 2 Answers
Load different Scene on first load 4 Answers