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