Skip to content

Instantly share code, notes, and snippets.

@Democide
Last active March 28, 2024 22:25
Show Gist options
  • Save Democide/beba5fd2603b268a8f72 to your computer and use it in GitHub Desktop.
Save Democide/beba5fd2603b268a8f72 to your computer and use it in GitHub Desktop.
// 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";
}
}
@johnsolm
Copy link

thx. going to give this a try.

@cardex107
Copy link

Thanks!!!

@CaminhoneiroHell
Copy link

Going to try, thanks

@SanketRJDave
Copy link

Thanks 👍

@MrAzureAngel
Copy link

Brilliant thank you, just what I needed. Will credit you in my University project.

@wj127
Copy link

wj127 commented Oct 16, 2016

Keep going with such tutorials man! They're really helpful :)

@oliversitan
Copy link

oliversitan commented Oct 19, 2016

Thanks man.
I recommend changing the line 40 for this
static string loadingSceneIndex = "NameOfLoadinScene";
because it not change if you change scene number, useful to many scene projects

@Frankjaamy
Copy link

ThankYou!

@EverythingMGP
Copy link

have u got a downloadable script for the 2nd one as well

@JamScoBal
Copy link

I used this but it gets stuck on loading screen and won't fade in. Any suggestions?

@onepiece92
Copy link

thanks

@MarlonCopeland
Copy link

Unity recommends using UnloadSceneAsync now. Use that instead of UnloadScene

@siva-saripilli
Copy link

I tried adding .gif and converted it to a sprite. Then assigned it to the LoadingIcon but the animation of the gif doesn't seem to animate it when rendering the game! Any ideas?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment