Home > Game and CG, Programming, Unity > Create Simple Loading Screen

Create Simple Loading Screen

Just try to create simple Loading screen during the idle time …

  1. Create Folders Scripts, Scenes, and Textures in Assets Folder
    • Scenes
      • Loading.unity (Set Background in Main Camera to Black)
      • Scene01.unity
      • Scene02.unity
    • Scripts
      • Base
        • PersistentObject.cs
      • Game
        • Scene01Controller.cs
        • Scene02Controller.cs
      • Loading
        • LoadingManager.cs
        • LoadingScript.cs
        • Fading.cs
    • Textures
      • fadeout_texture.png (black image 2×2 pixels) imported as texture
  2. Write Script
    • PersistentObject.cs
      using UnityEngine;
      using System.Collections;
      
      public abstract class PersistentObject<T> : 
      MonoBehaviour where T : Component {
          private static T instance;
          
          void Awake() {
              if (instance == null) {
                  instance = GetComponent<T>();
                  DontDestroyOnLoad(gameObject);
                  OnAwake();
              } else {
                  Destroy(gameObject);
              }
          }
          
          protected virtual void OnAwake() {
              
          }
      }
    • Fading.cs
      using UnityEngine;
      using System.Collections;
      
      public class Fading : MonoBehaviour 
      {
          public Texture2D fadeOutTexture;
          public float fadeSpeed = 0.8f;
      
          private int drawDepth = -1000;
          private float alpha = 1.0f;
          private int fadeDir = -1;
      
          void OnGUI()
          {
            alpha += fadeDir * fadeSpeed * Time.deltaTime;
            alpha = Mathf.Clamp01(alpha);
      
            GUI.color = new Color(GUI.color.r,GUI.color.g,GUI.color.b,alpha);
            GUI.depth = drawDepth;
            GUI.DrawTexture(new Rect(0,0, Screen.width, Screen.height), 
                            fadeOutTexture);
      
          }
      
          public float BeginFade(int direction)
          {
              fadeDir = direction;
              return fadeSpeed;
          }
      
          void OnLevelWasLoaded()
          {
              BeginFade(-1);
          }
      }
    • LoadingManager.cs
      using UnityEngine;
      using System.Collections;
      
      public class LoadingManager : PersistentObject<LoadingManager
      {
          private string _nextScene = "Scene01";
          public string NextScene { get { return _nextScene;} }
          public void Load (string name) {
              _nextScene = name;
              Application.LoadLevel("Loading");
          }
          IEnumerator WaitAndLoad() { 
              float fadeTime = Component.FindObjectOfType<Fading>().BeginFade(1);
              yield return new WaitForSeconds(fadeTime);
              Application.LoadLevel("Loading");
          }
       }
    • LoadingScript.cs
      using UnityEngine;
      using System.Collections;
      
      public class LoadingScript : MonoBehaviour 
      {
          [SerializeFieldprivate GUIText textLbl;
          private float progress;
          AsyncOperation op;
          
          IEnumerator Start() {
              yield return new WaitForSeconds(0.2f);
              LoadingManager loadingManager = 
              Component.FindObjectOfType<LoadingManager>();
              op = Application.LoadLevelAsync(loadingManager.NextScene);
              yield return op;
          }
          
         void Update() {
              if( (op!=null) && (!op.isDone) ) {
                  progress = op.progress * 100f;
                  textLbl.text = "Loading.. " + progress" %";
              }
          }
      }
      
    • Scene01Controller.cs
      using UnityEngine;
      using System.Collections;
      
      public class Scene01Controller : MonoBehaviour 
      {
          void OnGUI() {
              GUILayout.BeginArea (new Rect (10,10,100,100));
              if(GUILayout.Button("Scene02")) {
                  var loadingManager = 
                  Component.FindObjectOfType<LoadingManager>();
                  loadingManager.Load("Scene02");
              }
              GUILayout.EndArea ();
          }
      }
    • Scene02Controller.cs
      using UnityEngine;
      using System.Collections;
      
      public class Scene02Controller : MonoBehaviour 
      {
          void OnGUI() {
              GUILayout.BeginArea (new Rect (10,10,100,100));
              if(GUILayout.Button("Scene01")) {
                  var loadingManager = 
                  Component.FindObjectOfType<LoadingManager>();
                  loadingManager.Load("Scene01");
              }
              GUILayout.EndArea ();
          }
      }
  3. Attach Script
    • LoadingScript.cs & LoadingManager.cs
      Create 2 empty game objects (LoadingScript and LoadingManager) in Loading.unity and attach these script
    • Fading.cs
      Attach this script to LoadingManager game object in scene Loading.unity and assign fadeout_texture texture
    • Scene01Controller.cs
      Create 1  empty game object in Scene01.unity and attach this script
    • Scene02Controller.cs
      Create 1 empty game object in Scene02.unity and attach this script
  4. Attach GUIText Component
    • Attach GUIText Component to LoadingScript.cs empty game object (step3)
    • Set text to “Loading ..”
    • Set position to (0.5,0.5,0.0)
    • Set Anchor value to “middle center” and Alignment to “center”
    • Drag this GUIText Component to variable textLbl in LoadingScript.cs inspector
  5. Include Scenes in Build Settings
    • Loading.unity
    • Scene01.unity
    • Scene02.unity
  6. Test
Advertisements
Categories: Game and CG, Programming, Unity
  1. No comments yet.
  1. No trackbacks yet.

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s

%d bloggers like this: