You can not select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
54 lines
1.5 KiB
54 lines
1.5 KiB
1 year ago
|
using System.Collections;
|
||
|
using UniRx;
|
||
|
using UnityEngine;
|
||
|
using UnityEngine.SceneManagement;
|
||
|
using UnityEngine.UI;
|
||
|
|
||
|
public class SceneLoader : MonoBehaviour
|
||
|
{
|
||
|
public string SceneName ="MainScene";
|
||
|
public Text ProgressText;
|
||
|
public Slider ProgressSlider;
|
||
|
|
||
|
private bool isloading;
|
||
|
private AsyncOperation async;
|
||
|
private int curProgressVaule = 0;
|
||
|
|
||
|
// Start is called before the first frame update
|
||
|
void Start()
|
||
|
{
|
||
|
StartCoroutine(AsyncLoading());
|
||
|
|
||
|
Observable.EveryUpdate()
|
||
|
.Where(_ => isloading)
|
||
|
.Subscribe(_ =>
|
||
|
{
|
||
|
int progressVaule = 0;
|
||
|
if (async.progress < 0.9f)
|
||
|
progressVaule = (int)async.progress * 100;
|
||
|
else
|
||
|
progressVaule = 100;
|
||
|
if (curProgressVaule < progressVaule)
|
||
|
curProgressVaule++;
|
||
|
}).AddTo(gameObject);
|
||
|
|
||
|
Observable.EveryLateUpdate()
|
||
|
.Subscribe(_ =>
|
||
|
{
|
||
|
ProgressText.text = curProgressVaule + "%";//实时更新进度百分比的文本显示
|
||
|
ProgressSlider.value = curProgressVaule;
|
||
|
if (curProgressVaule == 100)
|
||
|
async.allowSceneActivation = true;
|
||
|
|
||
|
}).AddTo(gameObject);
|
||
|
}
|
||
|
|
||
|
IEnumerator AsyncLoading()
|
||
|
{
|
||
|
isloading = true;
|
||
|
async = SceneManager.LoadSceneAsync(SceneName);
|
||
|
async.allowSceneActivation = false;
|
||
|
yield return async;
|
||
|
}
|
||
|
}
|