- Home /
Got a problem with Async!
Hey. I made a custom loadingscreen for my game but i got a problem:
List item
the loadingscreen last for 0.5 second or something like that so i cant see the "Facts" that i put on it and i want to make a "Press any key" button to load the new level after it is loaded 100%.
void Update ()
{ StartCoroutine(DisplayLoadingScreen(levelToLoad)); }
IEnumerator DisplayLoadingScreen(string level)
{
progress.enabled = true;
fact.enabled = true;
progressbar.enabled = true;
know.enabled = true;
background.enabled = true;
progressbar.transform.localScale = new Vector3 (loadProgress,progressbar.transform.localScale.y,progressbar.transform.localScale.z);
progress.text = "Loading Progress : " + loadProgress + "%";
AsyncOperation async = Application.LoadLevelAsync (level);
while(!async.isDone )
{
loadProgress = (int)(async.progress * 100);
progress.text = "Loading Progress : " + loadProgress + "%";
progressbar.transform.localScale = new Vector3 (async.progress, progressbar.transform.localScale.y, progressbar.transform.localScale.z);
yield return null;
}
}
}
Ok so what i want to ask is: I have to put an if sentence to async or something like that If(async==is.Done && input.GetKeyDown("x")) Application.LoadLevel(level)
or something like that but it wont work because AsyncOperation async = Application.LoadLevelAsync (level); is gona load the level automatically when is 100%.
How should i do it?
Answer by Bunny83 · May 11, 2015 at 12:30 PM
You have to prevent Unity from switching to the new loaded scene by setting allowSceneActivation to false. Something like that:
AsyncOperation async = Application.LoadLevelAsync (level);
async.allowSceneActivation = false;
while(!async.isDone || !Input.GetKeyDown("x"))
{
loadProgress = (int)(async.progress * 100);
progress.text = "Loading Progress : " + loadProgress + "%";
progressbar.transform.localScale = new Vector3 (async.progress, progressbar.transform.localScale.y, progressbar.transform.localScale.z);
yield return null;
}
async.allowSceneActivation = true;
You might want to replace this ugly condition:
while(!async.isDone || !Input.GetKeyDown("x"))
with
while( !(async.isDone && Input.GetKeyDown("x")) )
by applying simply boolean logic ^^
Tnx for help but i manage to do it without allowSceneActivation. Now i dont know how to count how much %of the lvl load , it is always at 100%.
@Spoukey:
How you "managed" it without it? Also how large are your levels? If they aren't that heavy it probably loads too fast...