XRButton.cs 4.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142
  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. public bool isTweenOnClick = true;
  31. public XRIcon3D Box
  32. {
  33. get
  34. {
  35. if (!box)
  36. {
  37. box = UnityUtil.GetBreadthChild<XRIcon3D>(transform, "BoxIcon3D");
  38. }
  39. return box;
  40. }
  41. set => box = value;
  42. }
  43. public Transform Back
  44. {
  45. get
  46. {
  47. if (!back)
  48. {
  49. back = UnityUtil.GetBreadthChild<Transform>(transform, "Back");
  50. }
  51. return back;
  52. }
  53. set => back = value;
  54. }
  55. public RectTransform Body
  56. {
  57. get
  58. {
  59. if (!body)
  60. {
  61. body = GetComponent<RectTransform>();
  62. }
  63. return body;
  64. }
  65. set => body = value;
  66. }
  67. public override void OnPointerDown(PointerEventData eventData)
  68. {
  69. base.OnPointerDown(eventData);
  70. if (!isTweenOnClick) return;
  71. if (Box)
  72. {
  73. //Box.DOKill();
  74. //Box.DOScaleZ(thickness * pressDis, (1 - pressDis) / pressTime / 3 * 2);
  75. Box.DoThickness(pressDis, (1 - pressDis) / pressTime / 3 * 2);
  76. }
  77. if (targetGraphic)
  78. {
  79. targetGraphic.transform.DOKill();
  80. targetGraphic.transform.DOLocalMoveZ(-thickness * pressDis / 2, (1 - pressDis) / pressTime);
  81. }
  82. }
  83. public override void OnPointerUp(PointerEventData eventData)
  84. {
  85. base.OnPointerUp(eventData);
  86. if (!isTweenOnClick) return;
  87. if (Box)
  88. {
  89. //Box.DOKill();
  90. //Box.DOScaleZ(thickness, (1 - pressDis) / pressTime / 3 * 2);
  91. Box.DoThickness(1, (1 - pressDis) / pressTime / 3 * 2);
  92. }
  93. if (targetGraphic)
  94. {
  95. targetGraphic.transform.DOKill();
  96. targetGraphic.transform.DOLocalMoveZ(-thickness / 2, (1 - pressDis) / pressTime);
  97. }
  98. }
  99. /// <summary>
  100. /// 更新按钮的厚度
  101. /// </summary>
  102. public void UpdateThickness()
  103. {
  104. Vector3 tmp = targetGraphic.rectTransform.anchoredPosition3D;
  105. tmp.z = -thickness / 2;
  106. targetGraphic.rectTransform.anchoredPosition3D = tmp;
  107. if (Box)
  108. {
  109. Box.SetThickness(thickness);
  110. }
  111. }
  112. protected override void OnRectTransformDimensionsChange()
  113. {
  114. base.OnRectTransformDimensionsChange();
  115. AutoSetSize();
  116. }
  117. public void AutoSetSize()
  118. {
  119. for (int i = 0; i < Body.childCount; i++)
  120. {
  121. var child = Body.GetChild(i);
  122. //as RectTransform;
  123. if (child is RectTransform)
  124. {
  125. (child as RectTransform).sizeDelta = Body.rect.size;
  126. }
  127. else
  128. {
  129. Vector3 tmp = child.localScale;
  130. tmp.x = Body.rect.size.x;
  131. tmp.y = Body.rect.size.y;
  132. child.localScale = tmp;
  133. }
  134. }
  135. }
  136. }
  137. }