diff --git a/Assets/_DDD/_Scripts/GameUi/BaseUi.cs b/Assets/_DDD/_Scripts/GameUi/BaseUi.cs index ac18cdc94..2a9e08e67 100644 --- a/Assets/_DDD/_Scripts/GameUi/BaseUi.cs +++ b/Assets/_DDD/_Scripts/GameUi/BaseUi.cs @@ -15,6 +15,11 @@ protected virtual void Awake() _canvasGroup = GetComponent(); _panel = transform.Find(CommonConstants.Panel).gameObject; } + + protected virtual void OnEnable() + { + + } protected virtual void Start() { @@ -27,6 +32,11 @@ protected virtual void Update() } + protected virtual void OnDisable() + { + + } + protected virtual void OnDestroy() { TryUnregister(); diff --git a/Assets/_DDD/_Scripts/GameUi/FadeUi.cs b/Assets/_DDD/_Scripts/GameUi/FadeUi.cs index 7f332230f..16bd91103 100644 --- a/Assets/_DDD/_Scripts/GameUi/FadeUi.cs +++ b/Assets/_DDD/_Scripts/GameUi/FadeUi.cs @@ -1,48 +1,54 @@ +using System.Threading.Tasks; using DG.Tweening; using UnityEngine; namespace DDD { - public class FadeUi : MonoBehaviour, IEventHandler, IEventHandler + public class FadeUi : BaseUi, IEventHandler, IEventHandler { - private CanvasGroup _canvasGroup; - private GameObject _panel; - - private void Awake() + protected override void Awake() { - _canvasGroup = GetComponent(); - _panel = transform.Find(CommonConstants.Panel).gameObject; + base.Awake(); _canvasGroup.alpha = 0f; - _panel.SetActive(false); } - private void OnEnable() + protected override void TryRegister() { EventBus.Register(this); EventBus.Register(this); } - private void OnDisable() + protected override void TryUnregister() { EventBus.Unregister(this); EventBus.Unregister(this); } - public async void Invoke(FadeInEvent evt) + public void Invoke(FadeInEvent evt) + { + _ = FadeInAsync(evt); + } + + public void Invoke(FadeOutEvent evt) + { + _ = FadeOutAsync(evt); + } + + private async Task FadeInAsync(FadeInEvent evt) { await _canvasGroup.DOFade(0f, evt.Duration) .SetUpdate(true) .AsyncWaitForCompletion(); - _panel.SetActive(false); + ClosePanel(); evt.CompletionSource.SetResult(true); } - public async void Invoke(FadeOutEvent evt) + private async Task FadeOutAsync(FadeOutEvent evt) { - _panel.SetActive(true); + OpenPanel(); await _canvasGroup.DOFade(1f, evt.Duration) .SetUpdate(true) diff --git a/Assets/_DDD/_Scripts/GameUi/InteractionMessageUi.cs b/Assets/_DDD/_Scripts/GameUi/InteractionMessageUi.cs index 76b366bf6..55f9fba11 100644 --- a/Assets/_DDD/_Scripts/GameUi/InteractionMessageUi.cs +++ b/Assets/_DDD/_Scripts/GameUi/InteractionMessageUi.cs @@ -1,4 +1,5 @@ using TMPro; +using Unity.VisualScripting; using UnityEngine; using UnityEngine.Localization; using UnityEngine.Localization.Components; @@ -11,35 +12,50 @@ public class InteractionMessageUi : BaseUi, IEventHandler(this); EventBus.Register(this); } - protected override void OnDestroy() + protected override void TryUnregister() { - base.OnDestroy(); - + base.TryUnregister(); + EventBus.Unregister(this); EventBus.Unregister(this); } public void Invoke(ShowInteractionUiEvent evt) + { + ShowInteractionUiEvent(evt); + } + + public void Invoke(HideInteractionUiEvent evt) + { + HideInteractionUiEvent(evt); + } + + private void ShowInteractionUiEvent(ShowInteractionUiEvent evt) { _previousLocalizedString = LocalizationManager.Instance.GetLocalizedString(evt.TextKey); _textLabel.color = evt.CanInteract ? _canInteractTextColor : _cannotInteractTextColor; - + if (_textLabelLocalizeStringEvent.StringReference != _previousLocalizedString) { _textLabelLocalizeStringEvent.StringReference = _previousLocalizedString; @@ -52,7 +68,7 @@ public void Invoke(ShowInteractionUiEvent evt) } } - public void Invoke(HideInteractionUiEvent evt) + private void HideInteractionUiEvent(HideInteractionUiEvent evt) { _filledImage.fillAmount = 0f; ClosePanel();