Loading Screen?

Hi,

I have a qeustion,

I want to make a loading screen, somthing that says "Loading". but i dont want to have to make a new scene per level. basicly, if a level is loading, can i make it spawn a GUI everytime its loading somthing.

i know this is very hard to explain but i need a little help.

Sure, here is a script I created.

It acts like a singleton, and you can easily set it up in your first scene. Just give it a texture and you are all set. You can use it like LoadingScreen.Load(levelIndex); and it will show the texture on screen.

using UnityEngine;

public class LoadingScreen : MonoBehaviour
{
    public Texture2D texture;
    static LoadingScreen instance;

    void Awake()
    {
        if (instance)
        {
            Destroy(gameObject);
            return;
        }
        instance = this;    
        gameObject.AddComponent<GUITexture>().enabled = false;
        guiTexture.texture = texture;
        transform.position = new Vector3(0.5f, 0.5f, 0.0f);
        DontDestroyOnLoad(this); 
    }

    public static void Load(int index)
    {
        if (NoInstance()) return;
        instance.guiTexture.enabled = true;
        Application.LoadLevel(index);
        instance.guiTexture.enabled = false;
    }

    public static void Load(string name)
    {
        if (NoInstance()) return;
        instance.guiTexture.enabled = true;
        Application.LoadLevel(name);
        instance.guiTexture.enabled = false;
    }

    static bool NoInstance()
    {
        if (!instance)
            Debug.LogError("Loading Screen is not existing in scene.");
        return instance;
    }
}

I found out to do loading screen is so easy after I implemented myself. Here is to share with everyone:

static var loading_on : boolean;
var Loading_Screen:Texture2D;

function OnGUI () {
	if(loading_on){
		GUI.matrix = Matrix4x4.TRS(Vector3.zero, Quaternion.identity, Vector3(1.0*Screen.width/GameData.X, 1.0*Screen.height/GameData.Y, 1.0));
		GUI.depth =-10;
		GUI.Box(new Rect(0,0,1024,768),Loading_Screen);
	}
	if(!Application.isLoadingLevel)
		loading_on=false;
}

Statement’s code didn’t work for me. Here is the modified version:

using UnityEngine;
using System.Collections;

public class LoadingScreen : MonoBehaviour
{
    public Texture2D texture;
    static LoadingScreen instance;
 
    void Awake()
    {
        if (instance)
        {
            Destroy(gameObject);
			hide();
            return;
        }
        instance = this;    
        gameObject.AddComponent<GUITexture>().enabled = false;
        guiTexture.texture = texture;
        transform.position = new Vector3(0.5f, 0.5f, 1f);
        DontDestroyOnLoad(this); 
    }
	
	public static void show()
	{
		if (!InstanceExists()) 
		{
			return;
		}
		instance.guiTexture.enabled = true;
	}
		
	public static void hide()
	{
		if (!InstanceExists()) 
		{
			return;
		}
		instance.guiTexture.enabled = false;
	}
	
	static bool InstanceExists()
	{
        if (!instance)
		{
			return false;
		}
		return true;
		
	}
 
}

I call i from my GameController class like this:

	public static void loadLevel(string sceneName)
	{
		LoadingScreen.show();
		Application.LoadLevel(sceneName);
	}

Don’t forget to put it on an empty object and set the texture.

I just put a post up about this at Fervent Interactive Blog

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.