Game3DButton.cs 4.2 KB

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