Created
August 8, 2015 22:51
-
-
Save jcguarinpenaranda/55ca7a48220a6fdc88eb to your computer and use it in GitHub Desktop.
Load a level asynchronously in Unity3d
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
//From: http://unity3d.com/learn/tutorials/modules/beginner/live-training-archive/creating-a-scene-menu | |
using UnityEngine; | |
using System.Collections; | |
using UnityEngine.UI; | |
public class ClickToLoadAsync : MonoBehaviour { | |
//The slider that is going to be affected | |
public Slider loadingBar; | |
//In the example, this was a full sized black raw Image | |
//with the slider and a loading text | |
public GameObject loadingImage; | |
private AsyncOperation async; | |
//Call this from a ui button or other script | |
public void ClickAsync(int level) | |
{ | |
loadingImage.SetActive(true); | |
StartCoroutine(LoadLevelWithBar(level)); | |
} | |
//This will change the scene automatically after | |
//having shown the loading screen with progress | |
IEnumerator LoadLevelWithBar (int level) | |
{ | |
async = Application.LoadLevelAsync(level); | |
while (!async.isDone) | |
{ | |
loadingBar.value = async.progress; | |
yield return null; | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment