Files
test/Assets/PerfectWorld/Scripts/SceneLoader/LoadingSceneController.cs
2026-02-26 15:01:46 +07:00

88 lines
2.2 KiB
C#
Raw Permalink 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;
private float _progress;
public void SetProgress(float progress)
{
_progress = progress;
}
public void ShowLoadingScene(bool active)
{
#if !UNITY_EDITOR
goContent.SetActive(active);
#endif
}
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;
}
void Update()
{
UpdateUI(_progress);
}
}
}