unity异步加载场景与过度场景进度条

unity异步加载场景与过度场景进度条

有A,B两个场景 还有一个Load过度场景
A场景里面有一个Button B场景也有一个Button 点击A场景的Button先出现过度场景进度条 然后进入到B场景 点击B场景的Button出现过度场景进度条 然后进入到A场景
首先新建一个过度场景 过度场景很简单 里面只需要有一个进度条 也就是UGUI的Slider 还需要一个Text 用来看加载进度的百分比


过度场景的代码
using System.Collections;
using UnityEngine;
using UnityEngine.SceneManagement;
using UnityEngine.UI;

public class Load
{
public static string SceneName;
}
public class Loading : MonoBehaviour {
public Slider loadingSlider;//进度条
public Text loadingText;//加载进度
private AsyncOperation asyncOperation ;//异步对象
public float nowProcess;
void Start ()
{
loadingSlider.value = 0;
StartCoroutine(loadAsync());
}
void Update()
{
if (asyncOperation == null)
{
return;
}
int toProcess;
if (asyncOperation .progress < 0.9f)
{
toProcess = (int)asyncOperation .progress * 100;
}
else
{
toProcess = 100;
}
if (nowProcess < toProcess)
{
nowProcess++;
}
loadingSlider.value = nowProcess / 100f;
loadingText.text = ((int)(loadingSlider.value * 100)).ToString() + “%”;
// 设置为true的时候,如果场景数据加载完毕,就可以自动跳转场景
if (nowProcess == 100)
{
asyncOperation .allowSceneActivation = true;
}
}
IEnumerator loadAsync()
{
asyncOperation = SceneManager.LoadSceneAsync(Load.SceneName);
asyncOperation .allowSceneActivation = false;
yield return asyncOperation ;
}
}


然后给A场景的Button绑定方法
public void aButtonClick()
{
Load.SceneName = “XXXXX”;//B场景的名字
SceneManager.LoadScene(“Loading”);
}
给B场景的Button绑定方法
public void bButtonClick()
{
Load.SceneName = “XXXXX”;//A场景的名字
SceneManager.LoadScene(“Loading”);
}


大功告成 记得把3个场景都添加到Scenes In Bulid里面
End

你可能感兴趣的:(unity)