- Home /
Check if level has been loaded?
Hi, how can i get a script to check if a level has been loaded?
Thanks.
In what way? What's wrong with using the
OnLevelWasLoaded(levelNumber)
callback? Are you talking about asynchronous level loading here? The other kind only ever happens within one frame- it tends to manifest as a massive lag spike as it loads all the resources in.
ok, what i'm trying to do is check if the first level has been loaded, and if it has been, then on the main menu, create a button that leads to the level.
Define 'loaded'. Do you mean 'has been loaded in the past' or do you mean 'is currently loaded'? You can't load a level in the background and then keep it 'on hold' so to speak until you want to enter it- the level appears as soon as it gets loaded. Of course, there are ways of faking this. What exactly are you trying to do?
Well that's easy then. Do you have any persistent objects? Do you want it to remember this between sessions?
Answer by syclamoth · Nov 13, 2011 at 02:25 PM
Well, I'm going to go straight ahead and answer what I think your question is (which, if I'm wrong, should teach you not to ask one-line questions).
You should use PlayerPrefs for this stuff- it'll remember between sessions, as well as between scenes without needing any persistant objects.
// when you load the level for the first time
if(PlayerPrefs.GetInt("HighestLevelLoaded") < Application.loadedLevel)
{
PlayerPrefs.SetInt("HighestLevelLoaded", Application.loadedLevel);
}
// In your menu script-
int jumpToThis = 0;
while(PlayerPrefs.GetInt("HighestLevelLoaded") <= jumpToThis))
{
if(GUILayout.Button("Load Level: " + jumpToThis))
{
Application.LoadLevel(jumpToThis);
}
jumpToThis++;
}
This will give you a list of numbered buttons for levels up to the highest one yet loaded. You can do whatever you like here- this is just an example.