| | 1 | | using UnityEngine; |
| | 2 | | using UnityEngine.EventSystems; |
| | 3 | | using System; |
| | 4 | | using System.Collections; |
| | 5 | |
|
| | 6 | | public class UIHoverTriggerShowHideAnimator : MonoBehaviour, IPointerEnterHandler, IPointerExitHandler |
| | 7 | | { |
| | 8 | | [SerializeField] ShowHideAnimator showHideAnimator = null; |
| | 9 | | [SerializeField] bool hideAnimatorOnAwake = false; |
| | 10 | | [SerializeField] bool enableAnimatorOnHover = false; |
| | 11 | |
|
| | 12 | | [Range(0, 10)] |
| | 13 | | [SerializeField] float setVisibleDelay = 0; |
| | 14 | |
|
| | 15 | | [Range(0, 10)] |
| | 16 | | [SerializeField] float setHideDelay = 0; |
| | 17 | |
|
| | 18 | | Coroutine delayRoutine = null; |
| | 19 | |
|
| 0 | 20 | | public void SetShowHideAnimator(ShowHideAnimator showHideAnimator) { this.showHideAnimator = showHideAnimator; } |
| | 21 | |
|
| | 22 | | void Awake() |
| | 23 | | { |
| 215 | 24 | | if (hideAnimatorOnAwake) |
| | 25 | | { |
| 198 | 26 | | showHideAnimator.gameObject.SetActive(false); |
| | 27 | | } |
| 215 | 28 | | } |
| | 29 | |
|
| 0 | 30 | | void IPointerEnterHandler.OnPointerEnter(PointerEventData eventData) { Show(); } |
| | 31 | |
|
| 0 | 32 | | void IPointerExitHandler.OnPointerExit(PointerEventData eventData) { Hide(); } |
| | 33 | |
|
| | 34 | | IEnumerator DelayRoutine(float delay, Action callback) |
| | 35 | | { |
| 0 | 36 | | if (delay > 0) |
| | 37 | | { |
| 0 | 38 | | yield return new WaitForSeconds(delay); |
| | 39 | | } |
| 0 | 40 | | callback?.Invoke(); |
| 0 | 41 | | } |
| | 42 | |
|
| | 43 | | void Show() |
| | 44 | | { |
| 0 | 45 | | StartDelayRoutine(setVisibleDelay, () => |
| | 46 | | { |
| 0 | 47 | | if (enableAnimatorOnHover && !showHideAnimator.gameObject.activeSelf) |
| | 48 | | { |
| 0 | 49 | | showHideAnimator.gameObject.SetActive(true); |
| | 50 | | } |
| 0 | 51 | | showHideAnimator.Show(); |
| 0 | 52 | | }); |
| 0 | 53 | | } |
| | 54 | |
|
| | 55 | | void Hide() |
| | 56 | | { |
| 0 | 57 | | StartDelayRoutine(setHideDelay, () => |
| | 58 | | { |
| 0 | 59 | | showHideAnimator.Hide(); |
| 0 | 60 | | }); |
| 0 | 61 | | } |
| | 62 | |
|
| | 63 | | void StopDelayRoutine() |
| | 64 | | { |
| 0 | 65 | | if (delayRoutine != null) |
| | 66 | | { |
| 0 | 67 | | StopCoroutine(delayRoutine); |
| | 68 | | } |
| 0 | 69 | | } |
| | 70 | |
|
| | 71 | | void StartDelayRoutine(float delay, Action callback) |
| | 72 | | { |
| 0 | 73 | | StopDelayRoutine(); |
| 0 | 74 | | delayRoutine = StartCoroutine(DelayRoutine(delay, callback)); |
| 0 | 75 | | } |
| | 76 | | } |