Skip to content

Instantly share code, notes, and snippets.

@jcguarinpenaranda
Created August 8, 2015 22:51
Show Gist options
  • Save jcguarinpenaranda/55ca7a48220a6fdc88eb to your computer and use it in GitHub Desktop.
Save jcguarinpenaranda/55ca7a48220a6fdc88eb to your computer and use it in GitHub Desktop.
Load a level asynchronously in Unity3d
//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