53 lines
1.3 KiB
C#
53 lines
1.3 KiB
C#
using Cysharp.Threading.Tasks;
|
|
using System.Threading;
|
|
using UnityEngine;
|
|
using UnityEngine.UI;
|
|
|
|
public class ExitDisplay : MonoBehaviour
|
|
{
|
|
[SerializeField] private CanvasGroup progressGroup;
|
|
[SerializeField] private Image bar;
|
|
|
|
private bool groupVisible = false;
|
|
private float progress;
|
|
private CancellationTokenSource cts;
|
|
|
|
public void SetProgress(float percent)
|
|
{
|
|
progress = percent;
|
|
}
|
|
|
|
private void Update()
|
|
{
|
|
if(!groupVisible && progress>0)
|
|
{
|
|
cts?.Cancel();
|
|
cts?.Dispose();
|
|
cts = new();
|
|
|
|
SetVisibility(true, cts.Token).Forget();
|
|
}
|
|
if (groupVisible && progress == 0)
|
|
{
|
|
cts?.Cancel();
|
|
cts?.Dispose();
|
|
cts = new();
|
|
|
|
SetVisibility(false, cts.Token).Forget();
|
|
}
|
|
|
|
bar.fillAmount = progress;
|
|
}
|
|
|
|
private async UniTask SetVisibility(bool visible, CancellationToken token)
|
|
{
|
|
float targetAlpha = visible ? 1 : 0;
|
|
groupVisible = visible;
|
|
|
|
while(progressGroup.alpha != targetAlpha && !token.IsCancellationRequested)
|
|
{
|
|
progressGroup.alpha= Mathf.MoveTowards(progressGroup.alpha, targetAlpha, Time.deltaTime);
|
|
await UniTask.Yield(PlayerLoopTiming.Update, token);
|
|
}
|
|
}
|
|
} |