forked from zxl/LaboratoryProtection
133 lines
4.6 KiB
C#
133 lines
4.6 KiB
C#
using Cysharp.Threading.Tasks;
|
||
using Cysharp.Threading.Tasks.Linq;
|
||
|
||
using PMaker.Await;
|
||
|
||
using System;
|
||
using System.Threading;
|
||
|
||
using TMPro;
|
||
|
||
using UniRx;
|
||
|
||
using UnityEngine;
|
||
using UnityEngine.AddressableAssets;
|
||
using UnityEngine.ResourceManagement.AsyncOperations;
|
||
using UnityEngine.SceneManagement;
|
||
using UnityEngine.UI;
|
||
|
||
public partial class UILoading : AwaitBehaviour
|
||
{
|
||
[SerializeField]
|
||
private Slider _slider;
|
||
[SerializeField]
|
||
private TextMeshProUGUI _text;
|
||
|
||
public float baseTimeSlice = 0.02f;
|
||
public AssetReference targetScene;
|
||
public AssetLabelReference labelReference;
|
||
public override async UniTask WaitAsync(CancellationToken cancellationToken = default)
|
||
{
|
||
var maxValue = 100;
|
||
this._slider.maxValue = maxValue;
|
||
this._slider.value = 0;
|
||
this._slider.wholeNumbers = true;
|
||
|
||
var tempStream = this._slider
|
||
.OnValueChangedAsObservable()
|
||
.Subscribe(_ => {
|
||
_text.text = $"{_}%";
|
||
});
|
||
//var size = await Addressables.GetDownloadSizeAsync(labelReference.RuntimeKey);
|
||
//Debug.Log("main:" + size);
|
||
|
||
//var res = await Addressables.LoadResourceLocationsAsync(labelReference.RuntimeKey);
|
||
//foreach (var item in res)
|
||
//{
|
||
// var size = await Addressables.GetDownloadSizeAsync(item.PrimaryKey);
|
||
// Debug.Log(item.PrimaryKey + " : " + size);
|
||
//}
|
||
//await UniTask.WaitUntil(() => Input.GetKeyDown(KeyCode.Space));
|
||
var allLoader = Addressables.DownloadDependenciesAsync(labelReference.labelString, autoReleaseHandle: true);
|
||
|
||
|
||
var target = 0;
|
||
UniTask.Create(async () => {
|
||
while (cancellationToken.IsCancellationRequested != true)
|
||
{
|
||
if (this._slider.value < target)
|
||
{
|
||
this._slider.value++;
|
||
}
|
||
if (this._slider.value >= maxValue)
|
||
{
|
||
break;
|
||
}
|
||
await UniTask.Delay(TimeSpan.FromSeconds(baseTimeSlice), cancellationToken: cancellationToken);
|
||
}
|
||
}).Forget();
|
||
|
||
UniTask.Create(async () => {
|
||
if (allLoader.Status == AsyncOperationStatus.Failed)
|
||
{
|
||
//Debug.LogError("场景加载异常: " + allLoader.OperationException.ToString());
|
||
await UniTask.Yield(cancellationToken);
|
||
}
|
||
while (!allLoader.IsDone)
|
||
{
|
||
// 加载进度(0~1)
|
||
//var percentage = sceneAsyncLoader.GetDownloadStatus().Percent;
|
||
var percentage = allLoader.PercentComplete;
|
||
//Debug.Log("load进度: " + percentage);
|
||
//Debug.Log("download进度: " + allLoader.GetDownloadStatus().Percent);
|
||
target = (int)(allLoader.GetDownloadStatus().Percent * 90);
|
||
await UniTask.Yield(cancellationToken);
|
||
}
|
||
}).Forget();
|
||
|
||
await allLoader;
|
||
Debug.Log("cache over!");
|
||
//await this._slider.OnValueChangedAsAsyncEnumerable().FirstAsync(_ => _ >= 90);
|
||
//await UniTask.WaitUntil(() => Input.GetKeyDown(KeyCode.Space));
|
||
|
||
// load main
|
||
{
|
||
var handle = SceneManager.LoadSceneAsync(1);
|
||
handle.allowSceneActivation = false;
|
||
//target = maxValue - 2;
|
||
//await UniTask.WaitUntil(() => handle.progress >= 0.9);
|
||
target = maxValue;
|
||
await this._slider.OnValueChangedAsAsyncEnumerable().FirstAsync(_ => _ >= maxValue);
|
||
//await UniTask.WaitUntil(() => Input.GetKeyDown(KeyCode.Space));
|
||
handle.allowSceneActivation = true;
|
||
|
||
//var sceneAsyncLoader = targetScene.LoadSceneAsync(UnityEngine.SceneManagement.LoadSceneMode.Single, activateOnLoad: false);
|
||
//var instance = await sceneAsyncLoader;
|
||
|
||
//await this._slider.OnValueChangedAsAsyncEnumerable().FirstAsync(_ => _ >= maxValue);
|
||
//await UniTask.WaitUntil(() => Input.GetKeyDown(KeyCode.Space));
|
||
//await instance.ActivateAsync();
|
||
}
|
||
tempStream.Dispose();
|
||
}
|
||
|
||
private void Reset()
|
||
{
|
||
_slider = GetComponentInChildren<Slider>(true);
|
||
_text = GetComponentInChildren<TextMeshProUGUI>(true);
|
||
}
|
||
|
||
private void FixedUpdate()
|
||
{
|
||
if (Input.GetKeyDown(KeyCode.C) == true)
|
||
{
|
||
Caching.ClearCache();
|
||
}
|
||
|
||
if (Input.GetKeyDown(KeyCode.A) == true)
|
||
{
|
||
Addressables.ClearDependencyCacheAsync(this.targetScene.RuntimeKey);
|
||
}
|
||
}
|
||
}
|