ISAP/Assets/Scripts/Tools/LoadingScripts.cs

51 lines
2.0 KiB
C#
Raw Blame History

This file contains ambiguous Unicode characters!

This file contains ambiguous Unicode characters that may be confused with others in your current locale. If your use case is intentional and legitimate, you can safely ignore this warning. Use the Escape button to highlight these characters.

using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.SceneManagement;
using UnityEngine.UI;
public class LoadingScripts : MonoBehaviour
{
private Slider slider; //滑动条
int currentProgress; //当前进度
int targetProgress; //目标进度
// Start is called before the first frame update
void Start()
{
currentProgress = 0;
targetProgress = 0;
slider = GameObject.Find("Slider").GetComponent<Slider>();
StartCoroutine(LoadingScene()); //开启协成
}
private IEnumerator LoadingScene()
{
AsyncOperation asyncOperation = SceneManager.LoadSceneAsync(DataManager.Instance.NextScene); //异步加载1号场景
asyncOperation.allowSceneActivation = false; //不允许场景立即激活//异步进度在 allowSceneActivation= false时会卡在0.89999的一个值这里乘以100转整形
while (asyncOperation.progress < 0.9f) //当异步加载小于0.9f的时候
{
targetProgress = (int)(asyncOperation.progress * 100); //异步进度在 allowSceneActivation= false时会卡在0.89999的一个值这里乘以100转整形
yield return LoadProgress();
}
targetProgress = 100; //循环后当前进度已经为90了所以需要设置目标进度到100继续循环
yield return LoadProgress();
asyncOperation.allowSceneActivation = true; //加载完毕,这里激活场景 —— 跳转场景成功
}
private IEnumerator<WaitForEndOfFrame> LoadProgress()
{
while (currentProgress < targetProgress) //当前进度 < 目标进度时
{
++currentProgress; //当前进度不断累加 Chinar温馨提示如果场景很小可以调整这里的值 例如:+=10 +=20来调节加载速度
slider.value = (float)currentProgress / 100; //给UI进度条赋值
yield return new WaitForEndOfFrame(); //等一帧
}
}
// Update is called once per frame
void Update()
{
}
}