| | 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 | |
|
| 4794 | 10 | | public bool hideOnEnable = true; |
| 4794 | 11 | | public float animSpeedFactor = 1.0f; |
| | 12 | | public bool disableAfterFadeOut; |
| 4794 | 13 | | public string visibleParam = "visible"; |
| | 14 | |
|
| 66 | 15 | | public bool isVisible => animator.GetBool(visibleParamHash); |
| | 16 | |
|
| | 17 | | private Animator animatorValue; |
| | 18 | | private Animator animator |
| | 19 | | { |
| | 20 | | get |
| | 21 | | { |
| 1298 | 22 | | if (animatorValue == null) |
| | 23 | | { |
| 323 | 24 | | animatorValue = GetComponent<Animator>(); |
| | 25 | | } |
| | 26 | |
|
| 1298 | 27 | | return animatorValue; |
| | 28 | | } |
| | 29 | | } |
| | 30 | | private int? visibleParamHashValue = null; |
| | 31 | |
|
| | 32 | | private int visibleParamHash |
| | 33 | | { |
| | 34 | | get |
| | 35 | | { |
| 531 | 36 | | if (!visibleParamHashValue.HasValue) |
| 323 | 37 | | visibleParamHashValue = Animator.StringToHash(visibleParam); |
| | 38 | |
|
| 531 | 39 | | return visibleParamHashValue.Value; |
| | 40 | | } |
| | 41 | | } |
| | 42 | |
|
| | 43 | | public void Show(bool instant = false) |
| | 44 | | { |
| 73 | 45 | | animator.speed = animSpeedFactor; |
| 73 | 46 | | animator.SetBool(visibleParamHash, true); |
| | 47 | |
|
| 73 | 48 | | if (instant) |
| 12 | 49 | | animator.Update(10); |
| 73 | 50 | | } |
| | 51 | |
|
| | 52 | | public void Hide(bool instant = false) |
| | 53 | | { |
| 392 | 54 | | animator.speed = animSpeedFactor; |
| 392 | 55 | | animator.SetBool(visibleParamHash, false); |
| | 56 | |
|
| 392 | 57 | | if (instant) |
| 290 | 58 | | animator.Update(10); |
| 392 | 59 | | } |
| | 60 | |
|
| | 61 | | public void AnimEvent_HideFinished() |
| | 62 | | { |
| 8 | 63 | | OnWillFinishHide?.Invoke(this); |
| | 64 | |
|
| 8 | 65 | | if (disableAfterFadeOut) |
| | 66 | | { |
| 3 | 67 | | gameObject?.SetActive(false); |
| | 68 | | } |
| 8 | 69 | | } |
| | 70 | |
|
| 12 | 71 | | public void AnimEvent_ShowFinished() { OnWillFinishStart?.Invoke(this); } |
| | 72 | |
|
| | 73 | | private void OnEnable() |
| | 74 | | { |
| 785 | 75 | | if ( hideOnEnable ) |
| | 76 | | { |
| 282 | 77 | | Hide(true); |
| | 78 | | } |
| 785 | 79 | | } |
| | 80 | | } |