进度条的制作unity

不说了直接上代码:

LoadingPanel:

using UnityEngine;
using System.Collections;
using UnityEngine.UI;
using UnityEngine.SceneManagement;
public class LoadingPanel : MonoBehaviour
{
public Slider processBar;
public Image loadingBg;

int toProcess;
private AsyncOperation async;

private int nowProcess;

public void Start()
{
loadingBg.sprite = BackgroundShow.Instance.ShowBackground();
}
public void EnterGame(string sceneName)
{

StartCoroutine(loadScene(sceneName));
}
private IEnumerator loadScene(string sceneName)
{
int displayProgress = 0;
int toProgress = 0;
AsyncOperation op = SceneManager.LoadSceneAsync(sceneName);
op.allowSceneActivation = false;
while (op.progress < 0.9f)
{
toProgress = (int)op.progress * 100;
while (displayProgress < toProgress)
{
++displayProgress;
SetLoadingPercentage(displayProgress);
yield return new WaitForEndOfFrame();
}
}

toProgress = 100;
while (displayProgress < toProgress)
{
++displayProgress;
SetLoadingPercentage(displayProgress);
yield return new WaitForEndOfFrame();
}
op.allowSceneActivation = true;
}
public void SetLoadingPercentage(int value)
{
processBar.value = value / 100f;
}

}

到时候直接调用LoadingPanel的EnterGame方法就行

 

posted @ 2018-09-12 18:14  mc宇少  阅读(221)  评论(0编辑  收藏  举报