| | 1 | | using DG.Tweening; |
| | 2 | | using DG.Tweening.Core; |
| | 3 | | using DG.Tweening.Plugins.Options; |
| | 4 | | using System; |
| | 5 | | using UnityEngine; |
| | 6 | | using UnityEngine.UI; |
| | 7 | |
|
| | 8 | | [RequireComponent(typeof(CanvasGroup))] [DisallowMultipleComponent] |
| | 9 | | public class ShowHideAnimator : MonoBehaviour |
| | 10 | | { |
| | 11 | | private const float BASE_DURATION = 0.2f; |
| | 12 | |
|
| 18028 | 13 | | public bool hideOnEnable = true; |
| 18028 | 14 | | public float animSpeedFactor = 1.0f; |
| | 15 | | public bool disableAfterFadeOut; |
| | 16 | |
|
| | 17 | | [SerializeField] private CanvasGroup canvasGroup; |
| | 18 | |
|
| | 19 | | private GraphicRaycaster raycaster; |
| | 20 | |
|
| 58 | 21 | | public bool isVisible => canvasGroup == null || canvasGroup.blocksRaycasts; |
| | 22 | |
|
| | 23 | | public event Action<ShowHideAnimator> OnWillFinishHide; |
| | 24 | | public event Action<ShowHideAnimator> OnWillFinishStart; |
| | 25 | |
|
| | 26 | | private void Awake() |
| | 27 | | { |
| 12217 | 28 | | if (canvasGroup == null) |
| 6318 | 29 | | canvasGroup = GetComponent<CanvasGroup>(); |
| | 30 | |
|
| 12217 | 31 | | raycaster = GetComponent<GraphicRaycaster>(); |
| 12217 | 32 | | } |
| | 33 | |
|
| | 34 | | private void OnEnable() |
| | 35 | | { |
| 17996 | 36 | | if (hideOnEnable) |
| 885 | 37 | | Hide(instant: true); |
| 17996 | 38 | | } |
| | 39 | |
|
| | 40 | | public void Show(bool instant = false) |
| | 41 | | { |
| 169 | 42 | | SetVisibility(visible: true, OnShowCompleted, instant); |
| | 43 | |
|
| | 44 | | void OnShowCompleted() => |
| 8 | 45 | | OnWillFinishStart?.Invoke(this); |
| 169 | 46 | | } |
| | 47 | |
|
| | 48 | | public void Hide(bool instant = false) |
| | 49 | | { |
| 1336 | 50 | | SetVisibility(visible: false, OnHideCompleted, instant); |
| | 51 | |
|
| | 52 | | void OnHideCompleted() |
| | 53 | | { |
| 416 | 54 | | OnWillFinishHide?.Invoke(this); |
| | 55 | |
|
| 416 | 56 | | if (disableAfterFadeOut && gameObject != null) |
| 1 | 57 | | gameObject.SetActive(false); |
| 416 | 58 | | } |
| 1336 | 59 | | } |
| | 60 | |
|
| | 61 | | /// <summary> |
| | 62 | | /// Show and then hide after delay (Show->Delay->Hide) |
| | 63 | | /// </summary> |
| | 64 | | public void ShowDelayHide(float delay) |
| | 65 | | { |
| 0 | 66 | | SetVisibility(visible: true, onComplete: HideAfterDelay); |
| | 67 | |
|
| | 68 | | void HideAfterDelay() => |
| 0 | 69 | | SetVisibility(visible: false, null).SetDelay(delay); |
| 0 | 70 | | } |
| | 71 | |
|
| | 72 | | private TweenerCore<float, float, FloatOptions> SetVisibility(bool visible, TweenCallback onComplete, bool instant = |
| | 73 | | { |
| 1505 | 74 | | if (canvasGroup == null) |
| | 75 | | { |
| 0 | 76 | | Debug.LogError($"Show Hide Animator in GameObject: {gameObject.name} has no canvasGroup assigned", gameObjec |
| 0 | 77 | | return null; |
| | 78 | | } |
| | 79 | |
|
| 1505 | 80 | | if (raycaster != null) |
| 596 | 81 | | raycaster.enabled = visible; |
| | 82 | |
|
| | 83 | | // When instant, we use duration 0 instead of just modifying the canvas group to mock the old animator behaviour |
| 1505 | 84 | | float duration = instant ? 0 : BASE_DURATION * animSpeedFactor; |
| | 85 | |
|
| 1505 | 86 | | canvasGroup.blocksRaycasts = visible; |
| 1505 | 87 | | canvasGroup.DOKill(); |
| | 88 | |
|
| 1505 | 89 | | return canvasGroup.DOFade(visible ? 1 : 0, duration) |
| | 90 | | .SetEase(Ease.InOutQuad) |
| | 91 | | .OnComplete(onComplete) |
| | 92 | | .SetLink(canvasGroup.gameObject, LinkBehaviour.KillOnDestroy) |
| | 93 | | .SetLink(canvasGroup.gameObject, LinkBehaviour.KillOnDisable); |
| | 94 | | } |
| | 95 | | } |