92 lines
2.5 KiB
C#
92 lines
2.5 KiB
C#
using System;
|
||
using UnityEngine;
|
||
using UnityEngine.SceneManagement;
|
||
using UnityEngine.UI;
|
||
using TMPro;
|
||
using System.Collections;
|
||
using System.Threading;
|
||
using Cysharp.Threading.Tasks;
|
||
|
||
namespace BrewMonster
|
||
{
|
||
public class LoadingSceneController : MonoSingleton<LoadingSceneController>
|
||
{
|
||
[Header("UI")]
|
||
public GameObject goContent;
|
||
public Slider progressBar;
|
||
public TMP_Text percentText;
|
||
public TMP_Text loadingText;
|
||
|
||
private bool finished;
|
||
private CancellationTokenSource cts;
|
||
|
||
public void ShowLoadingScene(bool active)
|
||
{
|
||
goContent.SetActive(active);
|
||
|
||
// if (active)
|
||
// {
|
||
// Debug.LogError("Loading scene controller started");
|
||
//
|
||
// finished = false;
|
||
// loadingText.text = "Loading...";
|
||
// UpdateUI(0);
|
||
//
|
||
// cts?.Cancel();
|
||
// cts = new CancellationTokenSource();
|
||
//
|
||
// ObserveLoadingAsync(cts.Token).Forget();
|
||
// }
|
||
// else
|
||
// {
|
||
// cts?.Cancel();
|
||
// }
|
||
}
|
||
|
||
async UniTaskVoid ObserveLoadingAsync(CancellationToken token)
|
||
{
|
||
try
|
||
{
|
||
while (!finished && !token.IsCancellationRequested)
|
||
{
|
||
float progress = Mathf.Clamp01(SceneLoader.LoadingProgress / 100f);
|
||
UpdateUI(progress);
|
||
|
||
if (SceneLoader.SceneLoadProcess == SceneLoadProcess.EndLoading)
|
||
{
|
||
finished = true;
|
||
await EndLoadingAsync(token);
|
||
break;
|
||
}
|
||
|
||
await UniTask.Yield(PlayerLoopTiming.Update, token);
|
||
}
|
||
}
|
||
catch (OperationCanceledException)
|
||
{
|
||
// Normal cancel – ignore
|
||
}
|
||
}
|
||
|
||
async UniTask EndLoadingAsync(CancellationToken token)
|
||
{
|
||
UpdateUI(1f);
|
||
loadingText.text = "Entering scene...";
|
||
|
||
await UniTask.Delay(TimeSpan.FromSeconds(0.2f), cancellationToken: token);
|
||
|
||
ShowLoadingScene(false);
|
||
}
|
||
|
||
public void UpdateUI(float progress)
|
||
{
|
||
progressBar.value = progress;
|
||
percentText.text = $"{Mathf.RoundToInt(progress * 100f)}%";
|
||
}
|
||
public void SetLoadingText(string content)
|
||
{
|
||
loadingText.text = content;
|
||
}
|
||
}
|
||
}
|