SCButton.cs 5.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190
  1. using System.Collections;
  2. using System.Collections.Generic;
  3. using UnityEngine;
  4. using UnityEngine.Events;
  5. using UnityEngine.EventSystems;
  6. using DG.Tweening;
  7. using SC;
  8. [RequireComponent(typeof(BoxCollider))]
  9. [AddComponentMenu("ShadowCreator/SCButton")]
  10. public class SCButton : MonoBehaviour, IPointerDownHandler, IPointerUpHandler, IPointerClickHandler, IDragHandler, IPointerEnterHandler, IPointerExitHandler {
  11. public enum Transition {
  12. None,
  13. Scale,
  14. Position,
  15. }
  16. public enum ScaleType {
  17. Null,
  18. Z,
  19. XYZ,
  20. }
  21. [Header("Animation Settings")]
  22. public ScaleType scaleType = ScaleType.Null;
  23. public float duration = 0.3f;
  24. public float scaleRatio = 0.2f;
  25. public bool PositionAnimation = false;
  26. public float forwardRatio = 0.05f;
  27. [Header("Events Settings")]
  28. public UnityEvent onClick = new UnityEvent();
  29. public UnityEvent onEnter = new UnityEvent();
  30. public UnityEvent onDown = new UnityEvent();
  31. public UnityEvent onUp = new UnityEvent();
  32. public UnityEvent onExit = new UnityEvent();
  33. public Vector3 initScale;
  34. public Vector3 initPosition;
  35. /// <summary>
  36. /// 子集中需要触发的效果
  37. /// </summary>
  38. private PointEffectBase[] _effectComponents;
  39. public PointEffectBase[] effectComponents {
  40. get {
  41. if(_effectComponents == null) {
  42. _effectComponents = GetComponentsInChildren<PointEffectBase>();
  43. }
  44. return _effectComponents;
  45. }
  46. }
  47. void Awake() {
  48. initScale = transform.localScale;
  49. initPosition = transform.localPosition;
  50. }
  51. public virtual void Start() { }
  52. public virtual void Update() { }
  53. public virtual void OnDestroy() {
  54. if(onClick != null) {
  55. onClick.RemoveAllListeners();
  56. }
  57. }
  58. public virtual void OnPointerEnter( PointerEventData eventData ) {
  59. foreach(var item in effectComponents) {
  60. if(item.gameObject != gameObject)
  61. item.OnPointerEnter(eventData);
  62. }
  63. if(onEnter != null) {
  64. onEnter.Invoke();
  65. }
  66. OnEnterAnimation();
  67. }
  68. public virtual void OnPointerDown( PointerEventData eventData ) {
  69. foreach(var item in effectComponents) {
  70. if(item.gameObject != gameObject)
  71. item.OnPointerDown(eventData);
  72. }
  73. if(onDown != null) {
  74. onDown.Invoke();
  75. }
  76. }
  77. public virtual void OnPointerUp( PointerEventData eventData ) {
  78. foreach(var item in effectComponents) {
  79. if(item.gameObject != gameObject)
  80. item.OnPointerUp(eventData);
  81. }
  82. if(onUp != null) {
  83. onUp.Invoke();
  84. }
  85. }
  86. public virtual void OnPointerClick( PointerEventData eventData ) {
  87. foreach(var item in effectComponents) {
  88. if(item.gameObject != gameObject)
  89. item.OnPointerClick(eventData);
  90. }
  91. if(onClick != null) {
  92. onClick.Invoke();
  93. }
  94. OnClickAnimation();
  95. }
  96. public virtual void OnDrag( PointerEventData eventData ) {
  97. foreach(var item in effectComponents) {
  98. if(item.gameObject != gameObject)
  99. item.OnDrag(eventData);
  100. }
  101. }
  102. public virtual void OnPointerExit( PointerEventData eventData ) {
  103. foreach(var item in effectComponents) {
  104. if(item.gameObject != gameObject)
  105. item.OnPointerExit(eventData);
  106. }
  107. if(onExit != null) {
  108. onExit.Invoke();
  109. }
  110. OnExitAnimation();
  111. }
  112. public virtual void OnEnterAnimation() {
  113. //if(scaleType == ScaleType.Z) {
  114. // transform.DOLocalMoveZ(initScale.z, 0.5f).SetEase(Ease.OutQuart).SetAutoKill(true);
  115. //} else if(scaleType == ScaleType.XYZ) {
  116. // transform.DOScale(initScale * scalRatio, transitionTime).SetEase(Ease.InOutExpo).SetAutoKill(true);
  117. //}
  118. if(PositionAnimation) {
  119. transform.DOLocalMove(initPosition + new Vector3(0, 0, forwardRatio * -1), duration).SetEase(Ease.InOutExpo).SetAutoKill(true);
  120. }
  121. }
  122. public virtual void OnClickAnimation() {
  123. if(scaleType == ScaleType.Z) {
  124. transform.DOScaleZ(initScale.z * scaleRatio, duration / 2).SetEase(Ease.InOutExpo).SetId("OnClickAnimation").OnComplete(ClickFinish).SetAutoKill(true);
  125. } else if(scaleType == ScaleType.XYZ) {
  126. transform.DOScale(initScale * scaleRatio, duration).SetEase(Ease.InOutExpo).SetAutoKill(true);
  127. }
  128. }
  129. public virtual void ClickFinish() {
  130. foreach(var item in effectComponents) {
  131. if(item.gameObject != gameObject)
  132. item.ClickFinish();
  133. }
  134. transform.DOScaleZ(initScale.z, duration).SetEase(Ease.InOutExpo);
  135. }
  136. public virtual void OnExitAnimation() {
  137. DOTween.Kill("OnClickAnimation");
  138. if(scaleType == ScaleType.Z) {
  139. transform.DOScaleZ(initScale.z, duration).SetEase(Ease.InOutExpo).SetAutoKill(true);
  140. } else if(scaleType == ScaleType.XYZ) {
  141. transform.DOScale(initScale , duration).SetEase(Ease.InOutExpo).SetAutoKill(true);
  142. }
  143. if(PositionAnimation) {
  144. transform.DOLocalMove(initPosition, duration).SetEase(Ease.InOutExpo).SetAutoKill(true);
  145. }
  146. }
  147. }