Last active
March 21, 2020 00:27
-
-
Save rutcreate/90032396371d256713be to your computer and use it in GitHub Desktop.
Unity3D: Scene Manager (Loading asynchronously)
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
using UnityEngine; | |
public class SceneManager : MonoBehaviour { | |
private const string LOADING_LEVEL_KEY = "Loading Level"; | |
private static AsyncOperation operation; | |
public float delayBefore = 0.5f; | |
public float delayAfter = 0.5f; | |
private float currentProgress = 0f; | |
private bool finishing = false; | |
private void Start() { | |
Invoke("StartLoadScene", delayBefore); | |
} | |
private void StartLoadScene() { | |
StartCoroutine(LoadScene()); | |
} | |
private IEnumerator LoadScene() { | |
// Finish load scene if there is no level index assigned. | |
if (!PlayerPrefs.HasKey(LOADING_LEVEL_KEY)) yield break; | |
// Get level index from player preferences. | |
int levelIndex = PlayerPrefs.GetInt(LOADING_LEVEL_KEY); | |
// Remove key. We don't need it anymore. | |
PlayerPrefs.DeleteKey(LOADING_LEVEL_KEY); | |
// Start load scene. | |
operation = Application.LoadLevelAsync(levelIndex); | |
// Mark that scene should not be activated by itself. We will handle it. | |
operation.allowSceneActivation = false; | |
// Update current progress for GUI display. | |
currentProgress = operation.progress; | |
yield return operation; | |
} | |
private void Update() { | |
if (operation == null) return; | |
if (finishing) return; | |
if (operation.progress >= 0.9f) { | |
currentProgress = 1.0f; | |
Invoke("ActivateScene", delayAfter); | |
finishing = true; | |
} | |
} | |
private void ActivateScene() { | |
operation.allowSceneActivation = true; | |
} | |
public static void LoadLevel(int index) { | |
PlayerPrefs.SetInt(LOADING_LEVEL_INDEX, index); | |
// Load scene name "Loading" which contains of object with SceneManager componenet. | |
// SceneManager.Start() will triggered. | |
Application.LoadLevel("Loading"); | |
} | |
// For debug purpose. | |
// OnGUI() could make an error. Please kindly remove this method if it doesn't work. | |
// In Unity 5.x, this method is deprecated. | |
private void OnGUI() { | |
if (operation == null) return; | |
Rect position = new Rect(10f, 10f, Screen.width - 20f, 100f); | |
GUI.Label(position, "Progress: " + currentProgress); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment