51 lines
1.4 KiB
C#
51 lines
1.4 KiB
C#
![]() |
using DG.Tweening;
|
||
|
using UnityEngine;
|
||
|
|
||
|
namespace DDD
|
||
|
{
|
||
|
public class FadeUi : MonoBehaviour, IEventHandler<FadeInEvent>, IEventHandler<FadeOutEvent>
|
||
|
{
|
||
|
private CanvasGroup _canvasGroup;
|
||
|
|
||
|
private void Awake()
|
||
|
{
|
||
|
_canvasGroup = GetComponent<CanvasGroup>();
|
||
|
|
||
|
_canvasGroup.alpha = 0f;
|
||
|
_canvasGroup.gameObject.SetActive(false);
|
||
|
|
||
|
EventBus.Register<FadeInEvent>(this);
|
||
|
EventBus.Register<FadeOutEvent>(this);
|
||
|
}
|
||
|
|
||
|
private void OnDestroy()
|
||
|
{
|
||
|
EventBus.Unregister<FadeInEvent>(this);
|
||
|
EventBus.Unregister<FadeOutEvent>(this);
|
||
|
}
|
||
|
|
||
|
public async void Invoke(FadeInEvent evt)
|
||
|
{
|
||
|
await _canvasGroup.DOFade(0f, evt.Duration)
|
||
|
.SetUpdate(true)
|
||
|
.AsyncWaitForCompletion();
|
||
|
|
||
|
_canvasGroup.blocksRaycasts = false;
|
||
|
_canvasGroup.gameObject.SetActive(false);
|
||
|
|
||
|
evt.CompletionSource.SetResult(true);
|
||
|
}
|
||
|
|
||
|
public async void Invoke(FadeOutEvent evt)
|
||
|
{
|
||
|
_canvasGroup.gameObject.SetActive(true);
|
||
|
_canvasGroup.blocksRaycasts = true;
|
||
|
|
||
|
await _canvasGroup.DOFade(1f, evt.Duration)
|
||
|
.SetUpdate(true)
|
||
|
.AsyncWaitForCompletion();
|
||
|
|
||
|
evt.CompletionSource.SetResult(true);
|
||
|
}
|
||
|
}
|
||
|
}
|