XRButton.cs 4.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174
  1. using DG.Tweening;
  2. using System;
  3. using System.Collections;
  4. using System.Collections.Generic;
  5. using UnityEngine;
  6. using UnityEngine.EventSystems;
  7. using UnityEngine.UI;
  8. using XRTool.Util;
  9. namespace XRTool.WorldUI
  10. {
  11. /// <summary>
  12. /// 空间按钮
  13. /// </summary>
  14. public class XRButton : Button
  15. {
  16. [Range(0, 200f)]
  17. public float thickness = 50;
  18. /// <summary>
  19. /// 压缩比例,按下按钮时的压缩比例
  20. /// </summary>
  21. [Range(0.05f, 1)]
  22. public float pressDis = 0.5f;
  23. [Range(1, 10)]
  24. public float pressTime = 3f;
  25. public bool isShowBack = false;
  26. public bool isShowBox = true;
  27. private XRIcon3D box;
  28. private Transform back;
  29. private RectTransform body;
  30. private Transform btnTip;
  31. public bool isTweenOnClick = true;
  32. public XRIcon3D Box
  33. {
  34. get
  35. {
  36. if (!box)
  37. {
  38. box = UnityUtil.GetBreadthChild<XRIcon3D>(transform, "BoxIcon3D");
  39. }
  40. return box;
  41. }
  42. set => box = value;
  43. }
  44. public Transform Back
  45. {
  46. get
  47. {
  48. if (!back)
  49. {
  50. back = UnityUtil.GetBreadthChild<Transform>(transform, "Back");
  51. }
  52. return back;
  53. }
  54. set => back = value;
  55. }
  56. public RectTransform Body
  57. {
  58. get
  59. {
  60. if (!body)
  61. {
  62. body = GetComponent<RectTransform>();
  63. }
  64. return body;
  65. }
  66. set => body = value;
  67. }
  68. public Transform BtnTip
  69. {
  70. get
  71. {
  72. if (!btnTip)
  73. {
  74. btnTip = UnityUtil.GetBreadthChild<Transform>(transform, "BtnTip");
  75. }
  76. return btnTip;
  77. }
  78. set => btnTip = value;
  79. }
  80. public override void OnPointerDown(PointerEventData eventData)
  81. {
  82. base.OnPointerDown(eventData);
  83. if (!isTweenOnClick) return;
  84. if (Box)
  85. {
  86. //Box.DOKill();
  87. //Box.DOScaleZ(thickness * pressDis, (1 - pressDis) / pressTime / 3 * 2);
  88. Box.DoThickness(pressDis, (1 - pressDis) / pressTime / 3 * 2);
  89. }
  90. if (targetGraphic)
  91. {
  92. targetGraphic.transform.DOKill();
  93. targetGraphic.transform.DOLocalMoveZ(-thickness * pressDis / 2, (1 - pressDis) / pressTime);
  94. }
  95. }
  96. public override void OnPointerUp(PointerEventData eventData)
  97. {
  98. base.OnPointerUp(eventData);
  99. if (!isTweenOnClick) return;
  100. if (Box)
  101. {
  102. //Box.DOKill();
  103. //Box.DOScaleZ(thickness, (1 - pressDis) / pressTime / 3 * 2);
  104. Box.DoThickness(1, (1 - pressDis) / pressTime / 3 * 2);
  105. }
  106. if (targetGraphic)
  107. {
  108. targetGraphic.transform.DOKill();
  109. targetGraphic.transform.DOLocalMoveZ(-thickness / 2, (1 - pressDis) / pressTime);
  110. }
  111. }
  112. /// <summary>
  113. /// 更新按钮的厚度
  114. /// </summary>
  115. public void UpdateThickness()
  116. {
  117. Vector3 tmp = targetGraphic.rectTransform.anchoredPosition3D;
  118. tmp.z = -thickness / 2;
  119. targetGraphic.rectTransform.anchoredPosition3D = tmp;
  120. if (Box)
  121. {
  122. Box.SetThickness(thickness);
  123. }
  124. }
  125. protected override void OnRectTransformDimensionsChange()
  126. {
  127. base.OnRectTransformDimensionsChange();
  128. AutoSetSize();
  129. }
  130. public void AutoSetSize()
  131. {
  132. for (int i = 0; i < Body.childCount; i++)
  133. {
  134. var child = Body.GetChild(i);
  135. //as RectTransform;
  136. if (child is RectTransform)
  137. {
  138. (child as RectTransform).sizeDelta = Body.rect.size;
  139. }
  140. else
  141. {
  142. Vector3 tmp = child.localScale;
  143. tmp.x = Body.rect.size.x;
  144. tmp.y = Body.rect.size.y;
  145. child.localScale = tmp;
  146. }
  147. }
  148. }
  149. public override void OnPointerEnter(PointerEventData eventData)
  150. {
  151. base.OnPointerEnter(eventData);
  152. if (BtnTip)
  153. {
  154. BtnTip.gameObject.SetActive(true);
  155. }
  156. }
  157. public override void OnPointerExit(PointerEventData eventData)
  158. {
  159. base.OnPointerExit(eventData);
  160. if (BtnTip)
  161. {
  162. BtnTip.gameObject.SetActive(false);
  163. }
  164. }
  165. }
  166. }