LoadScene場景異步加載
using UnityEngine;
using System.Collections;
using UnityEngine.SceneManagement;
using UnityEngine.UI;
public class LoadScene: MonoBehaviour
{
public Slider Slider;//定義滑動條
private AsyncOperation async;
private int number = 0;
private int num = 0;
private void Start()
{
//賦初始值
Slider.value = 0;
//開啟協程
StartCoroutine("start");
}
void Update()
{
//滑動條的移動
Slider.value = number / 100f;
}
//協程
private IEnumerator start()
{
//要加載的大場景Scene
async = SceneManager.LoadSceneAsync("Scene");
//初始加載場景不激活
async.allowSceneActivation = false;
//增加滑動平滑性
while (async.progress < 0.9f)
{
//加載進度
num = (int)async.progress * 100;
while (number < num)
{
++number;
//等待時長
yield return new WaitForSeconds(0);
}
}
num = 100;
//增加平滑效果
while (number < num)
{
++number;
//等待時長
yield return new WaitForSeconds(0);
}
//加載場景激活
async.allowSceneActivation = true;
}
}
注:加載的場景Scene需要要將場景添加到構建設置,請使用菜單File-> Build Settings ...之后才可使用.......