说明: ??这里通过 sceneIndex= PlayerPrefs.GetInt("SceneIndex"); 来确定要加载的场景索引。 根据需求自己设定PlayerPrefs.SetInt("SceneIndex"); 即可
??直接赋值将对应组件拖拽上即可 脚本如下:
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.SceneManagement;
using UnityEngine.UI;
using DG.Tweening;
using QFramework;
public class LoadSceneAsyncController : MonoBehaviour
{
[Header("场景加载进度条:")]
public Slider scene_Slider;
[Header("进度条显示:")]
public Text process_Txt;
[Header("进度条提示信息:")]
public Text tips_Txt;
AsyncOperation _async;
int sceneIndex=0;
private bool isCanIntoScene = false;
void Start()
{
scene_Slider.value = 0;
tips_Txt.text = "资源加载中...";
isCanIntoScene = false;
}
void OnEnable()
{
StartCoroutine(LoadAsync_IE(sceneIndex));
}
void Update()
{
if (Input.anyKeyDown&&isCanIntoScene)
{
_async.allowSceneActivation = true;
isCanIntoScene = false;
}
}
IEnumerator LoadAsync_IE(int sceneIndex)
{
sceneIndex= PlayerPrefs.GetInt("SceneIndex");
Debug.Log("当前场景索引为:" + sceneIndex);
yield return new WaitForSeconds(0.1f);
Debug.Log("当前的场景索引为:" + sceneIndex);
_async = SceneManager.LoadSceneAsync(sceneIndex);
float nowProgress = 0;
float endProgress = 0;
_async.allowSceneActivation = false;
while (_async.progress < 0.9f)
{
endProgress = _async.progress * 100f;
while (nowProgress < endProgress)
{
++nowProgress;
scene_Slider.value = nowProgress / 100;
process_Txt.text = (int)(nowProgress) + "%";
yield return new WaitForEndOfFrame();
}
}
endProgress = 100;
while (nowProgress < endProgress)
{
++nowProgress;
scene_Slider.value = nowProgress / 100;
process_Txt.text = (int)(nowProgress) + "%";
yield return new WaitForEndOfFrame();
}
isCanIntoScene = true;
tips_Txt.text = "按下任意键继续...";
tips_Txt.DOFade(1, 0.5f);
yield return new WaitForSeconds(0.5f);
tips_Txt.DOFade(0, 0.5f);
yield return new WaitForSeconds(0.5f);
tips_Txt.DOFade(1, 0.5f);
yield return new WaitForSeconds(0.5f);
tips_Txt.DOFade(0, 0.5f);
yield return new WaitForSeconds(0.5f);
tips_Txt.DOFade(1, 0.5f);
yield return new WaitForSeconds(0.5f);
tips_Txt.DOFade(0, 0.5f);
yield return new WaitForSeconds(0.5f);
tips_Txt.DOFade(1, 0.5f);
}
}
加载进度条:
|