Files
test/Assets/PerfectWorld/Scripts/SceneLoader/LoadingSceneController.cs
T
2025-12-16 16:53:01 +07:00

92 lines
2.5 KiB
C#
Raw Blame History

This file contains ambiguous Unicode characters
This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.
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;
}
}
}