www加载进度条实现:
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.UI;
using UnityEngine.SceneManagement;
public class TestLoadingProcess : MonoBehaviour {
public Text showProcess;
public Scrollbar bar;
void Start()
{
StartCoroutine(getWWW());
}
IEnumerator getWWW()
{
WWW www = new WWW(@"file:///E:/AssetBundleProject/01.mp4");
string progress;
while (!www.isDone)
{
progress = (((int)(www.progress * 100)) % 100) + "%";
bar.value = www.progress;
showProcess.text = progress;
//Debug.Log(progress);
yield return 1;
}
if (www.error != null)
{
Debug.Log("loading error:" + www.url);
}
else
{
progress = "100%";
SceneManager.LoadScene("GSS_4");
//Debug.Log(progress);
//enter complete code
}
}
}
异步加载场景进度条01:
private IEnumerator StartLoading_4()
{
int displayProgress = 0;
int toProgress = 0;
AsyncOperation op = Application.LoadLevelAsync(1);
op.allowSceneActivation = false;
while (op.progress < 0.9f)
{
toProgress = (int)op.progress * 100;
while (displayProgress < toProgress)
{
++displayProgress;
loadingText.text = displayProgress.ToString() + "%";
loadingSlider.value = displayProgress * 0.01f;
//SetLoadingPercentage(displayProgress);
yield return new WaitForEndOfFrame();
}
}
toProgress = 100;
while (displayProgress < toProgress)
{
++displayProgress;
loadingText.text = displayProgress.ToString() + "%";
//Debug.Log(float.Parse((displayProgress/100).ToString()));
//Debug.Log(displayProgress * 0.01);
loadingSlider.value = displayProgress * 0.01f;
//SetLoadingPercentage(displayProgress);
yield return new WaitForEndOfFrame();
}
op.allowSceneActivation = true;
}
异步加载场景进度条02:
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.SceneManagement;
using UnityEngine.UI;
public class TestLoadingScene : MonoBehaviour {
public Slider loadingSlider;
public Text loadingText;
private float loadingSpeed = 0.1f;
private float targetValue;
private AsyncOperation operation;
void Start()
{
//启动协程
StartCoroutine(AsyncLoading());
}
IEnumerator AsyncLoading()
{
operation = SceneManager.LoadSceneAsync("GSS_4");
targetValue = operation.progress;
//阻止当加载完成自动切换
//operation.allowSceneActivation = false;
yield return operation;
if (operation.progress >= 0.9f)
{
//operation.progress的值最大为0.9
targetValue = 1.0f;
}
if (targetValue != loadingSlider.value)
{
//插值运算
loadingSlider.value = Mathf.Lerp(loadingSlider.value, targetValue, Time.deltaTime * loadingSpeed);
if (Mathf.Abs(loadingSlider.value - targetValue) < 0.01f)
{
loadingSlider.value = targetValue;
}
}
loadingText.text = ((int)(loadingSlider.value * 100)).ToString() + "%";
if ((int)(loadingSlider.value * 100) == 100)
{
//允许异步加载完毕后自动切换场景
operation.allowSceneActivation = true;
}
}
}