Create A Loading Scene / Splash Screen - Unity

I was looking at my Unity inventory and found this script which I had used to add a loading / splash screen to one of my game.
I believe I found this code in the Unity Forum, which I had to do some modifications to get working. However, the modifications were very minimal.
I thought I would just share this with you guys. So here you go!

Loading Scene / Splash Screen - The Result



In the first scene or maybe the Main Menu scene of your game Create an Empty Gameobject. Call it whatever you like, for reference, I would call it as GO_LoadingScreen. Attach the script below to it:

using UnityEngine;
using System.Collections;
 
public class LoadingScreen : MonoBehaviour
{
        //The static loading loading screen texture to be assigned
        public Texture2D texture;
        //We make a static variable to our LoadingScreen instance
        static LoadingScreen instance;
 
        //When the object awakens, we assign the static variable if its a new instance and
        void Awake()
        {       
                //destroy the already existing instance, if any
                if (instance)
                {
                        Destroy(gameObject);
                        hide();                                         //call hide function to hide the 'loading texture'
                        return;
                }

                instance = this;                
                gameObject.AddComponent<GUITexture>().enabled = false;  //disable the texture on start of the scene
                guiTexture.texture = texture;                           //assign the texture
                transform.position = new Vector3(0.5f, 0.5f, 1f);       //position the texture to the center of the screen
                DontDestroyOnLoad(this);                                //make this object persistent between scenes
        }
        
        
        void Update()
        {       
                //hide the loading screen if the scene is loaded
                if(!Application.isLoadingLevel)
                        hide();
        }

        //function to enable the loading screen
        public static void show()
        {       
                //if instance does not exists return from this function
                if (!InstanceExists())
                {
                        return;
                }
                //enable the loading texture
                instance.guiTexture.enabled = true;
        }
        
        //function to hide the loading screen
        public static void hide()
        {
                if (!InstanceExists())
                {
                        return;
                }
                instance.guiTexture.enabled = false;
        }
 
        //function to check if the persistent instance exists
        static bool InstanceExists()
        {
                if (!instance)
                {
                        return false;
                }
                return true;
 
        }
 
}

I have commented the script for you guys and I don't think I should explain it in detail as it is quite simple.

But, I would brief the logic though:
- The script is basically a singleton class (A singleton is a class which only allows a single instance of itself to be created, and usually gives simple access to that instance.) which expects a Texture2D input (this Texture2D is the static image which you want to display when the screen is loading)
- We have made the GO_LoadingScreen persistent throughout our game, that is, after the scene where it is created, by attaching the script above (If you are wondering which line achieve this objective, well, the line DontDestroyOnLoad(this) of the Awake function does this)
- Functions show, hide and InstanceExists have been defined to display the texture, hide the texture and check if the instance of the singleton already exists, respectively.

Save the script and switch back to unity. You will see that the script attached to GO_LoadingScreen is expecting a Texture2D component. Set the texture which you want to use for the static loading screen of your game.

Once this is done you can use this script by calling the show function of the LoadingScreen singleton from another script where in you switch between the levels.

For instance, check out the method below:

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

See you around. 
Share on Google+

About Sujit Horakeri

Sujit Horakeri is a game freak just like any other next door guy you would come across. He is a Web Developer by Profession, Game Developer by Choice.
Connect with him on:
    Blogger
    Facebook

3 comments:

  1. It said that I need to add ; at the end somewhere I have no idea where though I put them on all the ends and I still get this message

    ReplyDelete
    Replies
    1. Ah, what message? And what to put at the end? Please be more clear so that we can help you..

      Delete
  2. i cannot see the texture that i put. it comes somehwere in the background and its barely visible.

    ReplyDelete