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?

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 ^^