如图,做加载ProgressBar
UI组件如下:
代码如下:
using System;
using System.Collections;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using UnityEngine;
class LoadingPanel:MonoBehaviour
{
private UIProgressBar PB;
private UILabel txtContent;
void Awake()
{
PB = Util.GetObjComponent<UIProgressBar>(this.transform,"ProgressBar");
txtContent = PB.GetComponentInChildren<UILabel>();
if (PB == null)
Logger.Log_Error("LoadingPanel ProgreessBar is null!!!");
if (txtContent == null)
Logger.Log_Error("LoadingPanel txtContent is null!!!");
PB.value = 0;
}
void Start()
{
LoadGame();
}
public void LoadGame()
{
//协同
StartCoroutine(StartLoading("Lobby"));
}
private IEnumerator StartLoading(String sceneName)
{
//当前展示的进度
int displayProgress = 0;
//实际变化的进度
int toProgress = 0;
AsyncOperation op = Application.LoadLevelAsync(sceneName);
//设置场景手动切换
op.allowSceneActivation = false;
//手动切换progress只能加载90%,对应的处理
while (op.progress < 0.9f)
{
//获得场景加载的进度
toProgress = (int)op.progress * 100;
//当展示的进度小于后台加载的进度时,对展示的数值进行改变
while (displayProgress < toProgress)
{
++displayProgress;
//百分比数值的跟进
txtContent.text = displayProgress + "%";
//progressBar的跟进
PB.value = displayProgress*0.01f;
yield return new WaitForEndOfFrame();
}
}
//对剩余10%做处理
toProgress = 100;
while (displayProgress < toProgress)
{
++displayProgress;
txtContent.text = displayProgress + "%";
PB.value = displayProgress * 0.01f;
yield return new WaitForEndOfFrame();
}
//开启场景
op.allowSceneActivation = true;
}
}
将脚本挂到Panel下,运行即可!