using System.Collections; using UnityEngine; using System; public class Window : MonoBehaviour { public event Action OnClose; public event Action OnHide; public event Action OnAfterClose; public event Action OnShow; // public VideoWindowMediator videoMediator; private bool _isClosed = false; private Animator _animator; private WindowAnimationSettings _animationSettings; private static readonly int OpenTrigger = Animator.StringToHash("OpenTrigger"); private static readonly int CloseTrigger = Animator.StringToHash("CloseTrigger"); private static readonly int AnimationInteger = Animator.StringToHash("Animation"); private Action _animationCompleteCallback; public void Init() { if(TryGetComponent(out _animator)) if (!TryGetComponent(out _animationSettings)) throw new Exception("Window with Animator must have WindowAnimationSettings component."); OnInit(); } protected virtual void OnInit() { } private IEnumerator CloseRoutine() { yield return new WaitUntil(() => !gameObject.activeSelf); _isClosed = true; OnClose?.Invoke(this); if (_isClosed) { OnAfterClose?.Invoke(this); Destroy(gameObject); } yield return null; } public void Close() { if (_isClosed) return; //FIX for NullReferenceException: Object reference not set to an instance of an object. Window+d__19.MoveNext () Hide(); UI.StartCoroutine(CloseRoutine()); } public void Show() { if (IsVisible()) return; SetVisible(true); if (_animator != null) { _animator.SetInteger(AnimationInteger, _animationSettings.OpenAnimation); _animator.SetTrigger(OpenTrigger); OnShowed(); OnShow?.Invoke(this); } else { OnShowed(); OnShow?.Invoke(this); } // if(videoMediator != null && Settings.CrossPromoIsActive) // videoMediator.ShowVideo(); } private void SetVisible(bool value = true) { gameObject.SetActive(value); //debug SetMouseInteraction(value); } public void SetMouseInteraction(bool state) { gameObject.GetOrCreate().blocksRaycasts = state; } protected virtual void OnShowed() { } public void Hide() { if (IsVisible() == false) return; if (_animator != null) { _animator.SetInteger(AnimationInteger, _animationSettings.CloseAnimation); _animator.SetTrigger(CloseTrigger); SetMouseInteraction(false); _animationCompleteCallback = () => { OnHidden(); SetVisible(false); OnHide?.Invoke(this); }; } else { OnHidden(); SetVisible(false); OnHide?.Invoke(this); } // if(videoMediator != null) // videoMediator.HideVideo(); } protected virtual void OnHidden() { } public bool IsVisible() { try { if (gameObject == null) return false; } catch (Exception) { return false; } return gameObject.activeInHierarchy; } public virtual bool NeedShowBackground => false; public virtual int ZOrder => 0; public virtual bool CanBeClosedByBackground => false; protected virtual string WindowOpenSfx => "window_open"; protected virtual string WindowCloseSfx => "window_close"; public IEnumerator CloseWithDelay(float delay) { yield return new WaitForSeconds(delay); Close(); } public void AnimationComplete() { _animationCompleteCallback?.Invoke(); _animationCompleteCallback = null; } }