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

54 lines
1.4 KiB
C#
Raw Normal View History

2025-07-21 07:53:39 +00:00
using DG.Tweening;
using UnityEngine;
namespace DDD
{
public class FadeUi : MonoBehaviour, IEventHandler<FadeInEvent>, IEventHandler<FadeOutEvent>
{
private CanvasGroup _canvasGroup;
private GameObject _panel;
2025-07-21 07:53:39 +00:00
private void Awake()
{
_canvasGroup = GetComponent<CanvasGroup>();
_panel = transform.Find(CommonConstants.Panel).gameObject;
2025-07-21 07:53:39 +00:00
_canvasGroup.alpha = 0f;
_panel.SetActive(false);
}
private void OnEnable()
{
2025-07-21 07:53:39 +00:00
EventBus.Register<FadeInEvent>(this);
EventBus.Register<FadeOutEvent>(this);
}
private void OnDisable()
2025-07-21 07:53:39 +00:00
{
EventBus.Unregister<FadeInEvent>(this);
EventBus.Unregister<FadeOutEvent>(this);
}
public async void Invoke(FadeInEvent evt)
{
await _canvasGroup.DOFade(0f, evt.Duration)
.SetUpdate(true)
.AsyncWaitForCompletion();
_panel.SetActive(false);
2025-07-21 07:53:39 +00:00
evt.CompletionSource.SetResult(true);
}
public async void Invoke(FadeOutEvent evt)
{
_panel.SetActive(true);
2025-07-21 07:53:39 +00:00
await _canvasGroup.DOFade(1f, evt.Duration)
.SetUpdate(true)
.AsyncWaitForCompletion();
evt.CompletionSource.SetResult(true);
}
}
}