HoverEffect.cs 4.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159
  1. using System.Collections;
  2. using System.Collections.Generic;
  3. using UnityEngine;
  4. using UnityEngine.UI;
  5. using UnityEngine.EventSystems;
  6. namespace SoftKitty
  7. {
  8. public class HoverEffect : MonoBehaviour, IPointerEnterHandler, IPointerExitHandler, IPointerDownHandler, IPointerUpHandler
  9. {
  10. public RectTransform _targetGraphic;
  11. public bool _usePosition = false;
  12. public Vector2 _hoverPosition = Vector2.zero;
  13. public bool _useAngle = false;
  14. public float _hoverAngle = 0F;
  15. public bool _useColor = false;
  16. public Color _hoverColor = new Color(1F, 1F, 1F, 1F);
  17. public bool _useScale = false;
  18. public Vector3 _hoverScale = Vector3.one;
  19. public string _soundHover = "bt_hover";
  20. public string _soundDown = "bt_down";
  21. public string _soundUp = "bt_up";
  22. #region Variables
  23. [HideInInspector]
  24. public bool isHover = false;
  25. Button mButton;
  26. Vector2 _oriPos;
  27. float _oriAngle;
  28. Color _oriColor;
  29. Color _oriColorSub;
  30. Vector3 _oriScale;
  31. bool inited = false;
  32. #endregion
  33. #region Internal Methods
  34. public void OnPointerEnter(PointerEventData eventData)
  35. {
  36. if (mButton)
  37. {
  38. if (!mButton.interactable) return;
  39. }
  40. isHover = true;
  41. SoundManager.Play2D(_soundHover);
  42. }
  43. public void OnPointerExit(PointerEventData eventData)
  44. {
  45. isHover = false;
  46. }
  47. void Start()
  48. {
  49. Init();
  50. }
  51. void OnEnable()
  52. {
  53. Init();
  54. }
  55. void Init()
  56. {
  57. if (inited)
  58. {
  59. ResetColor();
  60. return;
  61. }
  62. if (GetComponent<Button>()) mButton = GetComponent<Button>();
  63. if (_targetGraphic == null) _targetGraphic = GetComponent<RectTransform>();
  64. _oriPos = _targetGraphic.anchoredPosition;
  65. _oriAngle = _targetGraphic.localEulerAngles.z;
  66. _oriScale = _targetGraphic.localScale;
  67. _oriColor = GetColor(_targetGraphic);
  68. inited = true;
  69. }
  70. void OnDisable()
  71. {
  72. ResetColor();
  73. }
  74. void ResetColor()
  75. {
  76. if (!inited)
  77. {
  78. return;
  79. }
  80. isHover = false;
  81. if (_usePosition) _targetGraphic.anchoredPosition = _oriPos;
  82. if (_useAngle) _targetGraphic.localEulerAngles = new Vector3(0F, 0F, _oriAngle);
  83. if (_useScale) _targetGraphic.localScale = _oriScale;
  84. if (_useColor)
  85. {
  86. SetColor(_targetGraphic, _oriColor);
  87. }
  88. }
  89. void Update()
  90. {
  91. if (!inited) return;
  92. if (_usePosition) _targetGraphic.anchoredPosition = Vector2.Lerp(_targetGraphic.anchoredPosition, isHover ? _hoverPosition : _oriPos, Time.unscaledDeltaTime * 10F);
  93. if (_useAngle) _targetGraphic.localEulerAngles = new Vector3(0F, 0F, Mathf.Lerp(_targetGraphic.localEulerAngles.z, isHover ? _hoverAngle : _oriAngle, Time.unscaledDeltaTime * 10F));
  94. if (_useScale) _targetGraphic.localScale = Vector3.Lerp(_targetGraphic.localScale, isHover ? _hoverScale : _oriScale, Time.unscaledDeltaTime * 10F);
  95. if (_useColor)
  96. {
  97. SetColor(_targetGraphic, Color.Lerp(GetColor(_targetGraphic), isHover ? _hoverColor : _oriColor, Time.unscaledDeltaTime * 10F));
  98. }
  99. }
  100. public void ClickButton()
  101. {
  102. if (mButton) mButton.onClick.Invoke();
  103. }
  104. private Color GetColor(RectTransform _item)
  105. {
  106. if (_item.GetComponent<MaskableGraphic>())
  107. {
  108. return _item.GetComponent<MaskableGraphic>().color;
  109. }
  110. else
  111. {
  112. return Color.white;
  113. }
  114. }
  115. private void SetColor(RectTransform _item, Color _color)
  116. {
  117. if (_item.GetComponent<MaskableGraphic>())
  118. {
  119. _item.GetComponent<MaskableGraphic>().color = _color;
  120. }
  121. }
  122. public void OnPointerDown(PointerEventData eventData)
  123. {
  124. SoundManager.Play2D(_soundDown);
  125. }
  126. public void OnPointerUp(PointerEventData eventData)
  127. {
  128. SoundManager.Play2D(_soundUp);
  129. }
  130. #endregion
  131. }
  132. }