- Home /
The question is answered, right answer was accepted
How to make a progress bar for loading next scene
i have 2 scenes in my game
i wish to make a progress bar in first scene according to waiting time to load second scene.
How can i do it with progrss bar.
Is anty other method with image to do it?
Answer by TonyLi · May 21, 2013 at 06:14 PM
Calling LoadLevelAsync like TheDarkVoid suggested will start the loading process. You could put it in a coroutine so other parts of the game can keep running. It returns an AsyncOperation. You can check 'isDone' to see if it's done, or 'progress' to get the progress (0-1). Note that progress doesn't work well in the editor, but is more accurate in builds.
Then you need to display the progress. Using Unity GUI, you can draw two textures: one for the empty bar background, and one for the filled progress.
public Texture2D emptyProgressBar; // Set this in inspector.
public Texture2D fullProgressBar; // Set this in inspector.
private AsyncOperation async = null; // When assigned, load is in progress.
private IEnumerator LoadALevel(string levelName) {
async = Application.LoadLevelAsync(levelName);
yield return async;
}
void OnGUI() {
if (async != null) {
GUI.DrawTexture(Rect(0, 0, 100, 50), emptyProgressBar);
GUI.DrawTexture(Rect(0, 0, 100 * async.progress, 50), fullProgressBar);
}
}
Note 1: Untested code; may contain syntax errors.
Note 2: The example code draws the progress bar at (0,0)-(100,50). Change this to wherever you want to put the bar.
void OnGUI() {
if (async != null) {
GUI.DrawTexture(Rect(0, 0, 100, 50), emptyProgressBar);
GUI.DrawTexture(Rect(0, 0, 100 * async.progress, 50), fullProgressBar);
GUI.skin.label.alignment = TextAnchor.$$anonymous$$iddleCenter;
GUI.Label(Rect(0, 0, 100, 50), string.Format("{0:N0}%", async.progress * 100f));
}
}
Please read: http://docs.unity3d.com/Documentation/Components/GUIScriptingGuide.html
When I tried this, the IEnumerator had to be a generic function outside the call to loading:
private void SyncLoadLevel(string levelName) {
async = Application.LoadLevelAsync (levelName);
Load ();
}
IEnumerator Load ()
{
yield return async;
}
Answer by TheDarkVoid · May 16, 2013 at 10:27 AM
Take a look at LoadLevelAsync, it allows you to load a level and still have other stuff going on like a loading screen. It requires unity pro though, as far as i'm aware there's no way to archive this without pro.