Game3DButton.cs 4.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144
  1. using UnityEngine;
  2. using UnityEngine.Events;
  3. using UnityEngine.EventSystems;
  4. using DG.Tweening;
  5. /// <summary>
  6. /// 3D的 按钮
  7. /// </summary>
  8. public class Game3DButton : MonoBehaviour, IPointerDownHandler, IPointerUpHandler, IPointerClickHandler, IPointerExitHandler,IPointerEnterHandler
  9. {
  10. [SerializeField]
  11. private Ease mTweenType = Ease.OutCubic;//缓动的参数
  12. [SerializeField]
  13. private float duration = 0.2f;//缓动的持续时间
  14. private float wScale = 1.03f;//宽缩放倍数
  15. private float hScale = 1.05f;//高缩放倍数
  16. private AudioSource _audioSource;
  17. private Vector3 initPosition;//初始的位置
  18. private Quaternion initRotation;//初始的角度
  19. private Vector3 initScale;//默认初始的局部Scale大小
  20. private float transitionTime = 0.1f;//按键反应时间
  21. private float forwardNum = 0.008f;//按键的键程
  22. [SerializeField]
  23. private Renderer mRenderer;//材质球 点击变色 可以没有
  24. private Color DefaultColor;//记录按钮的初始颜色
  25. private Color ClickColor;//点击的变得颜色
  26. private Color FocusColor;//暂时没有用
  27. public UnityEvent onClick;//点击事件
  28. protected void Awake()
  29. {
  30. initPosition = transform.localPosition;
  31. initRotation = transform.localRotation;
  32. initScale = transform.localScale;
  33. init();
  34. }
  35. private void Start()
  36. {
  37. _audioSource = (AudioSource)gameObject.GetComponent<AudioSource>();
  38. if(_audioSource != null)
  39. {
  40. _audioSource.playOnAwake = false;
  41. _audioSource.loop = false;
  42. }
  43. }
  44. private void OnEnable()
  45. {
  46. transform.DOPause();
  47. transform.localPosition = initPosition;
  48. transform.localRotation = initRotation;
  49. ChangeColor(DefaultColor);
  50. }
  51. protected virtual void ChangeColor(Color co)
  52. {
  53. if (mRenderer != null)
  54. {
  55. mRenderer.material.SetColor("_Color", co);
  56. }
  57. }
  58. protected virtual void ChangeMetallic(float value)
  59. {
  60. if (mRenderer != null)
  61. {
  62. mRenderer.material.SetFloat("_Metallic", value);
  63. }
  64. }
  65. protected virtual void init()
  66. {
  67. if (mRenderer != null)
  68. {
  69. DefaultColor = mRenderer.material.GetColor("_Color");
  70. }
  71. ClickColor = new Color(0.9f, 0.9f, 0.9f);
  72. }
  73. public virtual void OnPointerDown(PointerEventData data)
  74. {
  75. ChangeColor(ClickColor);
  76. }
  77. public virtual void OnPointerUp(PointerEventData data)
  78. {
  79. }
  80. public virtual void OnPointerEnter(PointerEventData data)
  81. {
  82. OnEnterAnimation();
  83. ChangeMetallic(0.65f);
  84. }
  85. public virtual void OnPointerExit(PointerEventData data)
  86. {
  87. OnExitAnimation();
  88. ChangeMetallic(0);
  89. }
  90. public virtual void OnPointerClick(PointerEventData data)
  91. {
  92. if (_audioSource != null)
  93. {
  94. _audioSource.Play();
  95. }
  96. OnClickAnimation();
  97. }
  98. private void ClickFun()
  99. {
  100. ChangeColor(DefaultColor);
  101. if (onClick != null)
  102. {
  103. onClick.Invoke();
  104. }
  105. }
  106. public virtual void OnClickAnimation()
  107. {
  108. transform.DOLocalMove(initPosition + new Vector3(0, 0, forwardNum * 1), transitionTime).SetEase(Ease.InOutExpo).OnComplete(ClickFinish).SetAutoKill(true);
  109. //transform.DOScaleZ(initScale.z * tweenScale, duration / 2).SetEase(Ease.InOutExpo).SetId("OnClickAnimation").OnComplete(ClickFinish).SetAutoKill(true);
  110. }
  111. public virtual void ClickFinish()
  112. {
  113. //transform.DOScaleZ(initScale.z, duration).SetEase(mLeanTweenType);
  114. transform.DOLocalMove(initPosition , transitionTime).SetEase(Ease.InOutExpo).OnComplete(ClickFun).SetAutoKill(true);
  115. }
  116. public virtual void OnEnterAnimation()
  117. {
  118. transform.DOScale(new Vector3(initScale.x * wScale,initScale.y * hScale, initScale.z), duration).SetEase(mTweenType);
  119. }
  120. public virtual void OnExitAnimation()
  121. {
  122. transform.DOScale(initScale, duration).SetEase(mTweenType);
  123. }
  124. }