版权声明:本文为博主原创文章,遵循 CC 4.0 BY-SA 版权协议,转载请附上原文出处链接和本声明。
加载游戏场景
using System;
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.UI;
using UnityEngine.SceneManagement;
public class LoadGame : MonoBehaviour {
public Slider processView;
void Start () {
LoadGameMethod();
}
void Update () {
}
public void LoadGameMethod()
{
StartCoroutine(StartLoading_4(2));
}
private IEnumerator StartLoading_4(int scene)
{
int displayProgress = 0;//当前进度
int toProgress = 0;//目标进度
AsyncOperation op = SceneManager.LoadSceneAsync(scene);
op.allowSceneActivation = false;
while (op.progress < 0.9f)
{
toProgress = (int)op.progress * 100;
while (displayProgress < toProgress)
{
++displayProgress;
SetLoadingPercentage(displayProgress);
yield return new WaitForEndOfFrame(); //等待这一帧所有渲染完成,才进行接下来的操作(+1 视图渲染完成)
}
}
toProgress = 100;
while (displayProgress < toProgress)
{
++displayProgress;
SetLoadingPercentage(displayProgress);
yield return new WaitForEndOfFrame();
}
op.allowSceneActivation = true;
}
private void SetLoadingPercentage(float v)
{
processView.value = v / 100;
}
}
另外一种更简单的场景异步加载博文:【Unity】场景异步加载的进度条制作