XRBoundLine.cs 5.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179
  1. using System.Collections;
  2. using System.Collections.Generic;
  3. using UnityEngine;
  4. using XRTool.Util;
  5. namespace XRTool.WorldUI
  6. {
  7. public enum ScaleType
  8. {
  9. UnChange = 0,
  10. Min = 1,
  11. Max = 2,
  12. Math
  13. }
  14. public class XRBoundLine : MonoBehaviour
  15. {
  16. [HideInInspector]
  17. public Transform[] horizontals;
  18. [HideInInspector]
  19. public Transform[] verticals;
  20. [HideInInspector]
  21. public Transform[] corners;
  22. /// <summary>
  23. /// 厚度比例
  24. /// </summary>
  25. private const float thicknessScale = 0.0145f;
  26. private const float BoundLen = 0.875f;
  27. private const float BoundCorner = 0.0625f;
  28. private float width;
  29. private float height;
  30. private float thickness;
  31. private bool isLocal = true;
  32. [HideInInspector]
  33. public ScaleType scaleType = ScaleType.Min;
  34. public Vector3 Scale
  35. {
  36. get
  37. {
  38. if (isLocal)
  39. {
  40. return transform.localScale;
  41. }
  42. return transform.lossyScale;
  43. }
  44. set
  45. {
  46. if (isLocal)
  47. {
  48. transform.localScale = value;
  49. }
  50. else
  51. {
  52. Vector3 scale = value;
  53. scale.x *= transform.localScale.x / transform.lossyScale.x;
  54. scale.y *= transform.localScale.y / transform.lossyScale.y;
  55. scale.z *= transform.localScale.z / transform.lossyScale.z;
  56. transform.localScale = value;
  57. }
  58. }
  59. }
  60. private void Awake()
  61. {
  62. width = transform.localScale.x;
  63. height = transform.localScale.y;
  64. thickness = transform.localScale.z;
  65. }
  66. /// <summary>
  67. /// 设置厚度
  68. /// </summary>
  69. public void SetThicknessScale(float thickness = thicknessScale)
  70. {
  71. Vector3 scale = transform.localScale;
  72. scale.z = thickness / thicknessScale;
  73. transform.localScale = scale;
  74. }
  75. /// <summary>
  76. /// 同步UI的背景框比例
  77. /// </summary>
  78. /// <param name="scale"></param>
  79. public void SetLine(Vector2 scale)
  80. {
  81. if (scale.x > 0 && scale.y > 0)
  82. {
  83. Vector3 tmp = transform.localScale;
  84. tmp.x = scale.x;
  85. tmp.y = scale.y;
  86. transform.localScale = tmp;
  87. Vector3 normalScale = Vector3.one;
  88. Vector3 h = Vector3.one;
  89. Vector3 v = Vector3.one;
  90. if (scaleType == ScaleType.UnChange)
  91. {
  92. }
  93. else if (scaleType == ScaleType.Min)
  94. {
  95. if (scale.x > scale.y)
  96. {
  97. normalScale.x = tmp.y / tmp.x;
  98. float bei = tmp.x / tmp.y;
  99. h.x = (BoundLen * bei / 2 + (bei * (1 - BoundLen) / 2 - BoundCorner)) / (BoundLen * bei / 2);
  100. }
  101. else
  102. {
  103. normalScale.y = tmp.x / tmp.y;
  104. float bei = tmp.y / tmp.x;
  105. v.y = (BoundLen * bei / 2 + (bei * (1 - BoundLen) / 2 - BoundCorner)) / (BoundLen * bei / 2);
  106. }
  107. }
  108. else if (scaleType == ScaleType.Max)
  109. {
  110. if (scale.x < scale.y)
  111. {
  112. normalScale.x = tmp.y / tmp.x;
  113. float bei = tmp.x / tmp.y;
  114. h.x = (BoundLen * bei / 2 + (bei * (1 - BoundLen) / 2 - BoundCorner)) / (BoundLen * bei / 2);
  115. }
  116. else
  117. {
  118. normalScale.y = tmp.x / tmp.y;
  119. float bei = tmp.y / tmp.x;
  120. v.y = (BoundLen * bei / 2 + (bei * (1 - BoundLen) / 2 - BoundCorner)) / (BoundLen * bei / 2);
  121. }
  122. }
  123. for (int i = 0; i < corners.Length; i++)
  124. {
  125. Transform corner = corners[i];
  126. corner.localScale = normalScale;
  127. }
  128. ///同步水平长度
  129. for (int i = 0; i < horizontals.Length; i++)
  130. {
  131. Transform horizontal = horizontals[i];
  132. horizontal.localScale = h;
  133. }
  134. ///同步垂直长度
  135. for (int i = 0; i < verticals.Length; i++)
  136. {
  137. Transform vertical = verticals[i];
  138. vertical.localScale = v;
  139. }
  140. }
  141. }
  142. /// <summary>
  143. /// 设置框的长宽
  144. /// </summary>
  145. /// <param name="width"></param>
  146. /// <param name="height"></param>
  147. public void SetLine(float width = 1, float height = 1, float thickness = thicknessScale)
  148. {
  149. if (thickness != this.thickness)
  150. {
  151. SetThicknessScale(thickness);
  152. this.thickness = thickness;
  153. }
  154. if (this.width != width || this.height != height)
  155. {
  156. ///比例相差不大,直接同比例缩放
  157. if (Mathf.Abs(width - height) < 0.0001f)
  158. {
  159. Vector3 scale = transform.localScale;
  160. scale.x = scale.y = width;
  161. transform.localScale = scale;
  162. }
  163. else
  164. {
  165. }
  166. this.width = width;
  167. this.height = height;
  168. }
  169. }
  170. }
  171. }