游戏场景加载:
同步加载:SceneManager.LoadScene(index);
异步加载:SceneManager.LoadSceneAsync(index);
这里主要讲的是异步加载,进入命名空间using UnityEngine.SceneManagement;
创建异步加载对象:private AsyncOperation asy;//异步加载对象
使用协程游戏开启加载游戏场景,在根据特定的条件去加载场景。
这里是通过加载进度条的方式去进行加载限制,这里需要注意的是asy.progress的值它只有0.9,不能超过0.9。
public Slider slider;//进度条
public Text tap;//显示时间文本形式
private AsyncOperation asy;//异步加载对象
private float toProgress, nowProgress;//toProgress目标时间,nowProgress现在时间
// Start is called before the first frame update
void Start()
{
StartCoroutine("Loda", 0);//下标传入,选择要进入场景
}
// Update is called once per frame
IEnumerator Loda(int i)
{
//异步加载
asy=SceneManager.LoadSceneAsync(i);
//异步加载完之后不让它立刻调整
asy.allowSceneActivation = false;
//返回异步跳转对象
yield return asy;
}
void Update()
{
if (asy.progress<0.9f)//asy.progress异步加载时间
{
//让其显示为90%
nowProgress = asy.progress * 100.0f;
}
else//asy.progress>0.9的情况
{
//当其加载为90的时候直接让他变成100
nowProgress = 100;
}
//现在的时间<要到达的时间
if (toProgress<nowProgress)
{
//累加,一直到100为止
toProgress++;
}
//进度条显示的值
slider.value = (int)(toProgress /100.0f);
//进度条显示的文本值
tap.text = (slider.value*100).ToString()+"%";
//当进度条满时,开启异步加载,进入下一个场景
if (toProgress==100)
{
asy.allowSceneActivation = true;
}
}