If you want a loading screen as a separate scene, I created a static class which allows you to do this. It currently uses Strings to reference levels rather than their index, simply for testing purposes (you could easily change this to an int).
Anyway, here’s the class (.js) if you want it:
Note this is for Unity 5, as it uses the new SceneManager stuff.
#pragma strict
import UnityEngine.SceneManagement;
public class Load extends MonoBehaviour
{
static function Begin (name : String, gm : Object)
{
SceneManager.LoadScene (name);
DontDestroyOnLoad (gm);
}
public static var progress : String;
public static var isDone = true;
public static var noLvl = false;
static function LoadLevel (name : String, gm : Object)
{
var async = SceneManager.LoadSceneAsync (name);
if (async == null) {
noLvl = true;
} else {
noLvl = false;
while (!async.isDone) {
var prog : int = 0;
prog = async.progress * 100;
progress = prog.ToString() + "%";
isDone = false;
yield;
}
if (async.isDone) {
isDone = true;
}
}
}
}
You can access the progress variable, which shows the loading percentage.
The isDone variable tells you if the new scene is finished loading.
The noLvl variable returns true if the requested scene does not exist or hasn’t been added to the build settings.
This is a static class, so you don’t need to attach it to anything.
Here is an example of how you could use it (with GUI elements):
#pragma strict
var design : GUISkin;
var loadingScene : String;
var scene : String;
function LoadScene ()
{
Load.Begin (loadingScene, gameObject);
yield WaitForFixedUpdate ();
yield Load.LoadLevel (scene, gameObject);
}
function OnGUI ()
{
GUI.skin = design;
if (Load.isDone == true)
{
if (GUI.Button (Rect (10,10,50,50), "Load")) {
LoadScene ();
}
scene = GUI.TextField (Rect (70, 10, 200, 50), scene, 25);
if (Load.noLvl == true) {
GUI.skin.label.fontSize = 18;
GUI.Label (Rect (10, 60, 200, 50), "Level does not exist.");
}
}
else
{
GUI.skin.label.alignment = TextAnchor.MiddleRight;
GUI.skin.label.fontSize = 42;
GUI.Label (Rect (Screen.width - 210, Screen.height - 60, 200, 50), Load.progress);
}
}
LoadSceneAsync loads the requested level in the background of the current one.