Menu

Unity3D – Add Loading Screen to your Games | Free Script | Free Demo Project Files

0 Comments


First thing to keep in mind is that loading screen is for the betterment of the game, and should not be a burden. If its a burden on the game, then there’s literally no need to add it in your game, the users of your game will like it without the loading screen anyway.

Normal execution timeline of a game

Normal execution timeline of a game
How can loading screen be a burden:

Many beginners of the game and software industry use it as an essential part and add it even if there’s no need of adding it at all.
Lets say they were encountering 1.5 seconds for some task to be completed (resource to be loaded into the memory). So they decided to add a loading bar/screen before it. They usually end up doing (lets say) 2 seconds of loading screen + 1.5 seconds of actual resource loading, which equals to 3.5 seconds.

The loading screen here is just a wastage of time and resources

The loading screen here is just a wastage of time and resources
How to avoid this:

The loading screens are added to the games to provide a curtain for some background activity of the game which is taking some time. So, we add a loading screen, to entertain our users with interesting graphics (loading screen) while the background task is being completed.

The loading screen is in the right place and time
The loading screen is in the right place and time

 

Here, i’ll let you know how to add a loading screen in our games. And that’ll be an actual loading screen instead of a fake one which most beginners use.

Setup:

There are three scenes in our game. One of them is Start Scene, the other is Target Scene and the last one is Loading Scene (i assume, you also have some setup like this. If not, don’t worry, this method works in almost every scenario).

It doesn’t matter how many scenes you have in your game, all you need is to add an extra scene into your game which will be your Loading Scene (to show the loading progress)

We’ll switch from Start Scene to Target Scene and while the Target Scene is being loaded, we’ll simultaneously load the Loading Scene along with the Target Scene. As a result, a loading screen will be shown to the users while loading the Target Scene.

So lets start by creating a c# script, and name it as LoadingScreenManager.

// LoadingScreenManager
// --------------------------------
// built by Martin Nerurkar (http://www.martin.nerurkar.de)
// for Nowhere Prophet (http://www.noprophet.com)
//
// Licensed under GNU General Public License v3.0
// http://www.gnu.org/licenses/gpl-3.0.txt

using UnityEngine;
using UnityEngine.UI;
using System.Collections;
using UnityEngine.SceneManagement;

public class LoadingScreenManager : MonoBehaviour {

	[Header("Loading Visuals")]
	public Image loadingIcon;
	public Image loadingDoneIcon;
	public Text loadingText;
	public Image progressBar;
	public Image fadeOverlay;

	[Header("Timing Settings")]
	public float waitOnLoadEnd = 0.25f;
	public float fadeDuration = 0.25f;

	[Header("Loading Settings")]
	public LoadSceneMode loadSceneMode = LoadSceneMode.Single;
	public ThreadPriority loadThreadPriority;

	[Header("Other")]
	// If loading additive, link to the cameras audio listener, to avoid multiple active audio listeners
	public AudioListener audioListener;

	AsyncOperation operation;
	Scene currentScene;

	public static int sceneToLoad = -1;
	// IMPORTANT! This is the build index of your loading scene. You need to change this to match your actual scene index
	static int loadingSceneIndex = 2;

	public static void LoadScene(int levelNum) {				
		Application.backgroundLoadingPriority = ThreadPriority.High;
		sceneToLoad = levelNum;
		SceneManager.LoadScene(loadingSceneIndex);
	}

	void Start() {
		if (sceneToLoad < 0)
			return;

		fadeOverlay.gameObject.SetActive(true); // Making sure it's on so that we can crossfade Alpha
		currentScene = SceneManager.GetActiveScene();
		StartCoroutine(LoadAsync(sceneToLoad));
	}

	private IEnumerator LoadAsync(int levelNum) {
		ShowLoadingVisuals();

		yield return null; 

		FadeIn();
		StartOperation(levelNum);

		float lastProgress = 0f;

		// operation does not auto-activate scene, so it's stuck at 0.9
		while (DoneLoading() == false) {
			yield return null;

			if (Mathf.Approximately(operation.progress, lastProgress) == false) {
				progressBar.fillAmount = operation.progress;
				lastProgress = operation.progress;
			}
		}

		if (loadSceneMode == LoadSceneMode.Additive)
			audioListener.enabled = false;

		ShowCompletionVisuals();

		yield return new WaitForSeconds(waitOnLoadEnd);

		FadeOut();

		yield return new WaitForSeconds(fadeDuration);

		if (loadSceneMode == LoadSceneMode.Additive)
			SceneManager.UnloadScene(currentScene.name);
		else
			operation.allowSceneActivation = true;
	}

	private void StartOperation(int levelNum) {
		Application.backgroundLoadingPriority = loadThreadPriority;
		operation = SceneManager.LoadSceneAsync(levelNum, loadSceneMode);


		if (loadSceneMode == LoadSceneMode.Single)
			operation.allowSceneActivation = false;
	}

	private bool DoneLoading() {
		return (loadSceneMode == LoadSceneMode.Additive && operation.isDone) || (loadSceneMode == LoadSceneMode.Single && operation.progress >= 0.9f); 
	}

	void FadeIn() {
		fadeOverlay.CrossFadeAlpha(0, fadeDuration, true);
	}

	void FadeOut() {
		fadeOverlay.CrossFadeAlpha(1, fadeDuration, true);
	}

	void ShowLoadingVisuals() {
		loadingIcon.gameObject.SetActive(true);
		loadingDoneIcon.gameObject.SetActive(false);

		progressBar.fillAmount = 0f;
		loadingText.text = "LOADING...";
	}

	void ShowCompletionVisuals() {
		loadingIcon.gameObject.SetActive(false);
		loadingDoneIcon.gameObject.SetActive(true);

		progressBar.fillAmount = 1f;
		loadingText.text = "LOADING DONE";
	}

}

You can also download this C# Script by clicking HERE.

Once you complete downloading and importing the C# script, follow these easy steps.

Steps:

  • Create a new scene named LoadingScene
  • Add LoadingScreenManager.cs C# script to one of the gameobjects in this scene
  • You’ll need to create the following items for this C# script to work,
    • Loading icon
    • Loading Complete icon
    • Screen fade-in panel
    • An image to show the progress of the loading bar
    • A text to show the percentage of loading
  • Once you’ve created these items, add these to the inspector settings of the C# script
  • That’s all for the Loading Scene
  • Now, go to the Scene that you want to trigger the Loading Scene from (in our case, its Start Scene)
  • Create a C# Script and name it Btn_SceneLoader.cs
using System.Collections;
using System.Collections.Generic;
using UnityEngine;

public class Btn_SceneLoader : MonoBehaviour {
	public void Btn_LoadScene (int Index) {
		LoadingScreenManager.LoadScene (Index);
	}
}
  • Download the Script HERE.
  • Now add a Button in Start Scene and call this function with that Button and pass the Build Index of the Target Scene as its parameter in the inspector panel.
  • Note: The loadingSceneIndex in Line 40 of LoadingScreenManager.cs Script should be the same as the Build Index of the Loading Scene.
static int loadingSceneIndex = 2;
  • That’s pretty much all. Now go-to the Start Scene and give it a try.

To download the complete demo project click HERE.

Watch this tutorial step by step on YoutTube HERE.

Tags: , , , , , , , , ,

Leave a Reply

avatar
  Subscribe  
Notify of
F