PressableButton.cs 5.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200
  1. using System.Collections;
  2. using System.Collections.Generic;
  3. using UnityEngine;
  4. using UnityEngine.EventSystems;
  5. using SC.XR.Unity.Module_InputSystem;
  6. using DG.Tweening;
  7. using UnityEngine.Events;
  8. [RequireComponent(typeof(BoxCollider))]
  9. [AddComponentMenu("SDK/PressableButton")]
  10. public class PressableButton : PointerHandler
  11. {
  12. private const float duration = 0.1f;
  13. [SerializeField]
  14. private List<InteractionPressableEntry> m_Delegates;
  15. public List<InteractionPressableEntry> Triggers
  16. {
  17. get
  18. {
  19. if (m_Delegates == null)
  20. m_Delegates = new List<InteractionPressableEntry>();
  21. return m_Delegates;
  22. }
  23. set { m_Delegates = value; }
  24. }
  25. [SerializeField]
  26. protected SCAudiosConfig.AudioType PressAudio = SCAudiosConfig.AudioType.ButtonPress;
  27. [SerializeField]
  28. protected SCAudiosConfig.AudioType ReleaseAudio = SCAudiosConfig.AudioType.ButtonUnpress;
  29. public Transform VisualMove;
  30. public Transform VisualScale;
  31. protected Vector3 MoveObjInitLocalPosition;
  32. protected Vector3 ScaleObjInitLocalScale;
  33. private Tween scaleTween;
  34. private Tween moveTween;
  35. [SerializeField]
  36. [Range(0.2f, 0.8f)]
  37. [Tooltip("The minimum percentage of the original scale the compressableButtonVisuals can be compressed to.")]
  38. protected float minCompressPercentage = 0.25f;
  39. private BoxCollider boxCollider;
  40. protected BoxCollider BoxCollider
  41. {
  42. get
  43. {
  44. if (boxCollider == null)
  45. {
  46. boxCollider = GetComponent<BoxCollider>();
  47. }
  48. return boxCollider;
  49. }
  50. }
  51. void Awake()
  52. {
  53. if (VisualScale != null)
  54. {
  55. ScaleObjInitLocalScale = VisualScale.localScale;
  56. }
  57. if (VisualMove != null)
  58. {
  59. MoveObjInitLocalPosition = VisualMove.localPosition;
  60. }
  61. }
  62. public override void OnPointerEnter(PointerEventData eventData)
  63. {
  64. Execute(InteractionPressableType.PointerEnter, eventData);
  65. }
  66. public override void OnPointerExit(PointerEventData eventData)
  67. {
  68. SCPointEventData mSCPointEventData = eventData as SCPointEventData;
  69. Execute(InteractionPressableType.PointerExit, eventData);
  70. if (VisualScale != null && mSCPointEventData.rawPointerPress == null)
  71. {
  72. if (VisualScale.localScale.z < ScaleObjInitLocalScale.z)
  73. {
  74. //DOTween.KillAll();
  75. if (scaleTween != null && scaleTween.IsPlaying())
  76. {
  77. scaleTween.Kill();
  78. }
  79. scaleTween = VisualScale.transform.DOScaleZ(ScaleObjInitLocalScale.z, duration);
  80. }
  81. }
  82. if (VisualMove != null && mSCPointEventData.rawPointerPress == null)
  83. {
  84. if (moveTween != null && moveTween.IsPlaying())
  85. {
  86. moveTween.Kill();
  87. }
  88. moveTween = VisualMove.transform.DOLocalMoveZ(MoveObjInitLocalPosition.z, duration);
  89. }
  90. }
  91. public override void OnPointerDown(PointerEventData eventData)
  92. {
  93. //base.OnPointerDown(eventData);
  94. SCPointEventData mSCPointEventData = eventData as SCPointEventData;
  95. Execute(InteractionPressableType.PointerDown, eventData);
  96. //DOTween.KillAll();
  97. if (VisualScale != null)
  98. {
  99. if (scaleTween != null && scaleTween.IsPlaying())
  100. {
  101. scaleTween.Kill();
  102. }
  103. if (VisualScale.localScale.z > ScaleObjInitLocalScale.z * minCompressPercentage)
  104. {
  105. scaleTween = VisualScale.transform.DOScaleZ(ScaleObjInitLocalScale.z * minCompressPercentage, duration);
  106. }
  107. }
  108. if (VisualMove != null)
  109. {
  110. if (moveTween != null && moveTween.IsPlaying())
  111. {
  112. moveTween.Kill();
  113. }
  114. moveTween = VisualMove.transform.DOLocalMoveZ(MoveObjInitLocalPosition.z + (BoxCollider.size.z / 2 * (1 - minCompressPercentage)), duration);
  115. }
  116. AudioSystem.getInstance.PlayAudioOneShot(gameObject, PressAudio);
  117. }
  118. public override void OnPointerClick(PointerEventData eventData)
  119. {
  120. Execute(InteractionPressableType.PointerClick, eventData);
  121. }
  122. public override void OnPointerUp(PointerEventData eventData)
  123. {
  124. SCPointEventData mSCPointEventData = eventData as SCPointEventData;
  125. Execute(InteractionPressableType.PointerUp, eventData);
  126. //DOTween.KillAll();
  127. if (VisualScale != null)
  128. {
  129. if (scaleTween != null && scaleTween.IsPlaying())
  130. {
  131. scaleTween.Kill();
  132. }
  133. if (VisualScale.localScale.z < ScaleObjInitLocalScale.z)
  134. {
  135. scaleTween = VisualScale.transform.DOScaleZ(ScaleObjInitLocalScale.z, duration);
  136. }
  137. }
  138. if (VisualMove != null)
  139. {
  140. if (moveTween != null && moveTween.IsPlaying())
  141. {
  142. moveTween.Kill();
  143. }
  144. moveTween = VisualMove.transform.DOLocalMoveZ(MoveObjInitLocalPosition.z, duration);
  145. }
  146. AudioSystem.getInstance.PlayAudioOneShot(gameObject, ReleaseAudio);
  147. }
  148. public override void OnDrag(PointerEventData eventData)
  149. {
  150. Execute(InteractionPressableType.Drag, eventData);
  151. }
  152. private void Execute(InteractionPressableType id, BaseEventData eventData)
  153. {
  154. for (int i = 0; i < Triggers.Count; i++)
  155. {
  156. InteractionPressableEntry entry = Triggers[i];
  157. if (entry.eventID == id)
  158. {
  159. entry.callback?.Invoke(eventData);
  160. }
  161. }
  162. }
  163. }