ProjectDDD/Assets/_DDD/_Scripts/GameUi/BaseUi/CommonUis/FadeUi.cs

58 lines
1.4 KiB
C#
Raw Normal View History

2025-08-14 08:21:17 +00:00
using System.Threading.Tasks;
2025-07-21 07:53:39 +00:00
using DG.Tweening;
namespace DDD
{
2025-08-14 08:21:17 +00:00
public class FadeUi : BaseUi, IEventHandler<FadeInEvent>, IEventHandler<FadeOutEvent>
2025-07-21 07:53:39 +00:00
{
2025-08-24 11:44:32 +00:00
protected override void OnDestroy()
2025-07-21 07:53:39 +00:00
{
2025-08-24 11:44:32 +00:00
base.OnDestroy();
2025-07-21 07:53:39 +00:00
2025-08-24 11:44:32 +00:00
EventBus.Unregister<FadeInEvent>(this);
EventBus.Unregister<FadeOutEvent>(this);
}
2025-08-24 11:44:32 +00:00
protected override void OnCreatedInitialize()
{
2025-08-24 11:44:32 +00:00
base.OnCreatedInitialize();
_canvasGroup.alpha = 0f;
2025-08-21 07:25:27 +00:00
2025-07-21 07:53:39 +00:00
EventBus.Register<FadeInEvent>(this);
EventBus.Register<FadeOutEvent>(this);
}
2025-08-14 08:21:17 +00:00
public void Invoke(FadeInEvent evt)
{
_ = FadeInAsync(evt);
}
public void Invoke(FadeOutEvent evt)
{
_ = FadeOutAsync(evt);
}
private async Task FadeInAsync(FadeInEvent evt)
2025-07-21 07:53:39 +00:00
{
await _canvasGroup.DOFade(0f, evt.Duration)
.SetUpdate(true)
.AsyncWaitForCompletion();
2025-08-14 08:21:17 +00:00
ClosePanel();
2025-07-21 07:53:39 +00:00
evt.CompletionSource.SetResult(true);
}
2025-08-14 08:21:17 +00:00
private async Task FadeOutAsync(FadeOutEvent evt)
2025-07-21 07:53:39 +00:00
{
2025-08-14 08:21:17 +00:00
OpenPanel();
2025-07-21 07:53:39 +00:00
await _canvasGroup.DOFade(1f, evt.Duration)
.SetUpdate(true)
.AsyncWaitForCompletion();
evt.CompletionSource.SetResult(true);
}
}
}