异步加载场景主要用于加载时的等待界面。
创建3个场景A、B、C。B场景为等待界面。
A场景代码,绑在摄像机上就好。
using UnityEngine;
using System.Collections;
public class A : MonoBehaviour {
// Use this for initialization
void Start () {
}
void OnGUI()
{
if(GUILayout.Button("Click",GUILayout.Height(50)))
{
Globe.SceneName = "C";
Application.LoadLevel("B");
}
}
// Update is called once per frame
void Update () {
}
}
B场景代码绑在摄像机上。
using UnityEngine;
using System.Collections;
public class B : MonoBehaviour {
AsyncOperation async;
// Use this for initialization
void Start ()
{
StartCoroutine(loadScene());
}
IEnumerator loadScene()
{
async = Application.LoadLevelAsync(Globe.SceneName);
yield return async;
}
// Update is called once per frame
void Update () {
}
}
C场景代码同样绑在摄像机上。这里我们创建很多物体,模拟创建数据的创建。
using UnityEngine;
using System.Collections;
public class C : MonoBehaviour
{
public GameObject prefab;
// Use this for initialization
void Start ()
{
loadObject();
}
void loadObject()
{
for(int i =0;i < 10000;i++)
{
Instantiate(prefab);
}
}
// Update is called once per frame
void Update () {
}
}