Create A Loading Scene / Splash Screen - Unity 4.6

In the previous post of ours which was on How to Create a Loading Scene / Splash Screen in Unity, we learnt to create a Loading Scene with a static Texture2D image. We had requests asking how to achieve this using Sprites and the new Unity GUI. This post is as a result of those requests we got.

Loading Scene / Splash Screen - Unity 4.6 (Result)



Follow the simple steps below to achieve this effect in Unity 4.6:

1. In the first scene of your game create an Empty GameObject and name it whatever you like, for reference I am going to name it as Splash Screen Holder, or perhaps, you can instead add it to the Main Menu scene as well.

2. Add a Canvas component inside this empty gameobject i.e., create a Canvas component and make it the child of  Splash Screen Holder object.

3. Inside the Canvas component, add a Image component, name it as ImageLS or any name you wish to.
Once you are done with the steps above your Splash Screen Holder object in the hierarchy will look like something in the image below:

Splash Screen Hierarchy View
Loading Scene / Splash Screen Hierarchy View
4.  Select the Render Mode of the Canvas to Screen Space - Camera. Check the Pixel Perfect checkbox. Drag and drop the Main Camera in the Render Camera field. Set the Order in Layer to 1 so as to make the components of this Canvas appear on top of any other Canvas components, if any.

Canvas Component Properties
Canvas Component Properties
5. Drag and drop the Sprite which you want to display as the Loading Scene Image / Splash Screen Image in the Source Image field of the Image component.

Image Component properties
Image Component Properties
6. You will see the Static Image on your Game and Scene view, which based on it's size, occupies a part of the screen. Resize the Image to occupy the entire Canvas and also set the Anchor points to the edges to make it dynamic.

Static Image Before Resizing
Static Image Before Resizing 

Static Image After Resizing
Static Image After Resizing 
7. Create a new C# script named LoadingScreen46, attach it to the Splash Screen Holder object and add the below code to it

using UnityEngine;
using System.Collections;

public class LoadingScreen46 : MonoBehaviour
{
 //We make a static variable to our LoadingScreen instance
 static LoadingScreen46 instance;
 //reference to gameobject with the static image 
 GameObject loadingScreenImage;

 //function which executes on scene awake before the start function
 void Awake()
 {
  //find the ImageLS gameobject from the Hierarchy
  loadingScreenImage = GameObject.Find("ImageLS");
  //destroy the already existing instance, if any
  if (instance)
  {
   Destroy(gameObject);
   hide();     //call hide function to hide the 'loading Screen Sprite'
   return;
  }
  instance = this;    
  instance.loadingScreenImage.SetActive(false);
  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 image object 
  instance.loadingScreenImage.SetActive(true);
 }
 //function to hide the loading screen
 public static void hide()
 {
  if (!InstanceExists()) 
  {
   return;
  }
  instance.loadingScreenImage.SetActive(false);
 }
 //function to check if the persistent instance exists
 static bool InstanceExists()
 {
  if (!instance)
  {
   return false;
  }
  return true;
  
 }
 
}

Save the script and return to Unity.

8. You have now successfully added the loading screen / splash screen to your game using the Unity 4.6 GUI, however, you got to call the show function of the LoadingScreen46 script before calling the LoadLevel function to load any level. The below function demonstrates a simple usage of this Singleton script

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


The above function is a function from a script within which there would be a need to switch to a different scene during run time (Note that this is not the same script as LoadingScreen46)

See you around.

Also check out,
Unity 4.6 GUI - Create A Dynamic Menu With The New UI
Unity 4.6 GUI - Create A Health Bar HUD
Unity 4.6 GUI - Create A Level Select Scroll Menu
Unity 4.6 GUI - Create An Animated Menu
Unity 4.6 GUI - Create A Level Lock/Unlock System
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

10 comments:

  1. Thank you for the effort you put into the tutorials.
    I'd like to see more in future.

    Regards
    Dennis

    ReplyDelete
    Replies
    1. Thank you for letting us know that you found our site useful.. It is really hard to see people commenting to thank for the posts these days :)

      Delete
  2. Hello Sujit,

    This tutorial is AWESOME!
    Unfortunately I can't get it to work. :(
    You've managed to explain it very clear but because I'm very new to unity and coding, I might be doing something wrong.

    Again I've uploaded a Unity package with the basic stuff from your tutorial(2 Scenes, 2 scripts, 1 loading image) here:
    https://drive.google.com/file/d/0B2AAPx_1Kup5S1g5MTJiOW9qaEU/view?usp=sharing

    If you can point me to the right direction I would be very grateful!
    Thanks!

    ReplyDelete
  3. not working, I do not know why. Everything is done as a lesson, but for some reason does not display a sprite.

    ReplyDelete
  4. I'm sorry! it worked! but I added a line to your script LoadingScreen46.show (); and Render Mode of the Canvas to Screen Space Overlay.
    Thank you for the lessons! They are the best!

    ReplyDelete
  5. good job bro keep up the good work.. definetly u r the hope for many indie developers out there..

    ReplyDelete
  6. help me

    loadingscreen46 skript error ,,

    unity show me error ,,cannot open LoadingScreen46.cs script please see external blabla

    ReplyDelete
  7. i tried to run the script in android mobile the game was crashed and it closing automatically. I am using unity 5.2, Android Kitkat

    ReplyDelete
  8. I am arrived at the 8th step, and I don't understand what to do now, I created this load image in the same scene of my augmented reality app but it never disappear, what should I do? Where i should paste the function in the 8th step?

    ReplyDelete