| | 1 | | using System; |
| | 2 | | using UnityEngine; |
| | 3 | |
|
| | 4 | | [RequireComponent(typeof(Animator))] |
| | 5 | | public class ShowHideAnimator : MonoBehaviour |
| | 6 | | { |
| | 7 | | public event System.Action<ShowHideAnimator> OnWillFinishHide; |
| | 8 | | public event System.Action<ShowHideAnimator> OnWillFinishStart; |
| | 9 | |
|
| 8088 | 10 | | public bool hideOnEnable = true; |
| 8088 | 11 | | public float animSpeedFactor = 1.0f; |
| | 12 | | public bool disableAfterFadeOut; |
| 8088 | 13 | | public string visibleParam = "visible"; |
| | 14 | |
|
| 54 | 15 | | public bool isVisible => animator.GetBool(visibleParamHash); |
| | 16 | |
|
| | 17 | | private Animator animatorValue; |
| | 18 | |
|
| | 19 | | private Animator animator |
| | 20 | | { |
| | 21 | | get |
| | 22 | | { |
| 3220 | 23 | | if (animatorValue == null) |
| | 24 | | { |
| 590 | 25 | | animatorValue = GetComponent<Animator>(); |
| | 26 | | } |
| | 27 | |
|
| 3220 | 28 | | return animatorValue; |
| | 29 | | } |
| | 30 | | } |
| | 31 | |
|
| | 32 | | private int? visibleParamHashValue = null; |
| | 33 | |
|
| | 34 | | private int visibleParamHash |
| | 35 | | { |
| | 36 | | get |
| | 37 | | { |
| 817 | 38 | | if (!visibleParamHashValue.HasValue) |
| 585 | 39 | | visibleParamHashValue = Animator.StringToHash(visibleParam); |
| | 40 | |
|
| 817 | 41 | | return visibleParamHashValue.Value; |
| | 42 | | } |
| | 43 | | } |
| | 44 | |
|
| | 45 | | public void Show(bool instant = false) |
| | 46 | | { |
| 133 | 47 | | animator.speed = animSpeedFactor; |
| | 48 | |
|
| 133 | 49 | | if ( animator.isActiveAndEnabled ) |
| 122 | 50 | | animator.SetBool(visibleParamHash, true); |
| | 51 | |
|
| 133 | 52 | | if (instant) |
| 9 | 53 | | animator.Update(10); |
| 133 | 54 | | } |
| | 55 | |
|
| | 56 | | public void Hide(bool instant = false) |
| | 57 | | { |
| 742 | 58 | | animator.speed = animSpeedFactor; |
| | 59 | |
|
| 742 | 60 | | if ( animator.isActiveAndEnabled ) |
| 641 | 61 | | animator.SetBool(visibleParamHash, false); |
| | 62 | |
|
| 742 | 63 | | if (instant) |
| 644 | 64 | | animator.Update(10); |
| 742 | 65 | | } |
| | 66 | |
|
| | 67 | | public void AnimEvent_HideFinished() |
| | 68 | | { |
| 32 | 69 | | OnWillFinishHide?.Invoke(this); |
| | 70 | |
|
| 32 | 71 | | if (disableAfterFadeOut) |
| | 72 | | { |
| 1 | 73 | | gameObject?.SetActive(false); |
| | 74 | | } |
| 32 | 75 | | } |
| | 76 | |
|
| 26 | 77 | | public void AnimEvent_ShowFinished() { OnWillFinishStart?.Invoke(this); } |
| | 78 | |
|
| | 79 | | private void OnEnable() |
| | 80 | | { |
| 3839 | 81 | | if ( hideOnEnable ) |
| | 82 | | { |
| 534 | 83 | | Hide(true); |
| | 84 | | } |
| 3839 | 85 | | } |
| | 86 | | } |