Modules_TooltipLineRenderer.cs 7.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239
  1. using System;
  2. using System.Collections;
  3. using System.Collections.Generic;
  4. using UnityEngine;
  5. namespace SC.XR.Unity.Module_Tooltip
  6. {
  7. [ExecuteAlways]
  8. [RequireComponent(typeof(LineRenderer))]
  9. public class Modules_TooltipLineRenderer : MonoBehaviour
  10. {
  11. public enum TooltipStartPointLocation
  12. {
  13. Auto,
  14. UpperLeft,
  15. UpperCenter,
  16. UpperRight,
  17. MiddleLeft,
  18. MiddleCenter,
  19. MiddleRight,
  20. BottomLeft,
  21. BottomCenter,
  22. BottonRight
  23. }
  24. [SerializeField]
  25. [Tooltip("Select the starting point of the line.")]
  26. private TooltipStartPointLocation tooltipStartPointLocation = TooltipStartPointLocation.Auto;
  27. [SerializeField]
  28. [Tooltip("The GameObject which the tooltip is connected to.")]
  29. private Transform target;
  30. [SerializeField]
  31. [Tooltip("Whether the point is visible.")]
  32. private bool isStartPointVisible;
  33. [SerializeField]
  34. [Tooltip("Whether the line is visible.")]
  35. private bool isLineVisible;
  36. private Transform Target
  37. {
  38. set { target = value; }
  39. get { return target; }
  40. }
  41. [SerializeField]
  42. [Range(0, 0.01f)]
  43. [Tooltip("The width of line")]
  44. private float width;
  45. private float Width
  46. {
  47. set { width = value; }
  48. get { return width; }
  49. }
  50. private LineRenderer lineRenderer;
  51. private Transform lable;
  52. private Transform background;
  53. private Transform[] startpoints;
  54. private Transform startpoint;
  55. private Dictionary<float, Transform> distanceDictionary;
  56. private List<float> distanceList;
  57. private Transform point;
  58. private void OnEnable()
  59. {
  60. lineRenderer = GetComponent<LineRenderer>();
  61. lable = transform.Find("Lable");
  62. background = lable.Find("Background");
  63. lineRenderer.useWorldSpace = false;
  64. startpoints = background.GetComponentsInChildren<Transform>();
  65. point = transform.Find("Point");
  66. }
  67. protected virtual void Update()
  68. {
  69. //Target = target;
  70. lineRenderer.widthMultiplier = Width;
  71. UpdateLine();
  72. SetVisible();
  73. }
  74. private void UpdateLine()
  75. {
  76. Vector3 point_start = GetStartPoint();
  77. Vector3[] points = { transform.InverseTransformPoint(point_start), transform.InverseTransformPoint(Target.position) };
  78. lineRenderer.SetPositions(points);
  79. }
  80. private Vector3 GetStartPoint()
  81. {
  82. switch (tooltipStartPointLocation)
  83. {
  84. case TooltipStartPointLocation.Auto:
  85. startpoint = GetNearbyPoint(startpoint);
  86. break;
  87. case TooltipStartPointLocation.UpperLeft:
  88. startpoint = startpoints[1];
  89. break;
  90. case TooltipStartPointLocation.UpperCenter:
  91. startpoint = startpoints[2];
  92. break;
  93. case TooltipStartPointLocation.UpperRight:
  94. startpoint = startpoints[3];
  95. break;
  96. case TooltipStartPointLocation.MiddleLeft:
  97. startpoint = startpoints[4];
  98. break;
  99. case TooltipStartPointLocation.MiddleCenter:
  100. startpoint = startpoints[5];
  101. break;
  102. case TooltipStartPointLocation.MiddleRight:
  103. startpoint = startpoints[6];
  104. break;
  105. case TooltipStartPointLocation.BottomLeft:
  106. startpoint = startpoints[7];
  107. break;
  108. case TooltipStartPointLocation.BottomCenter:
  109. startpoint = startpoints[8];
  110. break;
  111. case TooltipStartPointLocation.BottonRight:
  112. startpoint = startpoints[9];
  113. break;
  114. default:
  115. break;
  116. }
  117. return startpoint.position;
  118. }
  119. private Transform GetNearbyPoint(Transform startpoint)
  120. {
  121. distanceDictionary = new Dictionary<float, Transform>();
  122. distanceList = new List<float>();
  123. for (int i = 0; i < 9; i++)
  124. {
  125. float distance = Vector3.Distance(Target.position, startpoints[i + 1].position);
  126. try
  127. {
  128. distanceDictionary.Add(distance, startpoints[i + 1]);
  129. }
  130. catch (Exception)
  131. {
  132. }
  133. if (!distanceList.Contains(distance))
  134. {
  135. distanceList.Add(distance);
  136. }
  137. }
  138. distanceList.Sort();
  139. distanceDictionary.TryGetValue(distanceList[0], out startpoint);
  140. return startpoint;
  141. }
  142. private void SetVisible()
  143. {
  144. point.gameObject.GetComponent<MeshRenderer>().enabled = isStartPointVisible;
  145. lineRenderer.enabled = isLineVisible;
  146. }
  147. public Vector3 API_GetTargetPosition(bool isWorld)
  148. {
  149. if (isWorld)
  150. {
  151. return Target.position;
  152. }
  153. else
  154. {
  155. return transform.InverseTransformPoint(Target.position);
  156. }
  157. }
  158. public void API_SetTargetPosition(Vector3 newPosition, bool isWorld)
  159. {
  160. if (isWorld)
  161. {
  162. Target.transform.position = newPosition;
  163. }
  164. else
  165. {
  166. Target.transform.position = transform.TransformPoint(newPosition);
  167. }
  168. }
  169. public Vector3 API_GetLablePosition(bool isWorld)
  170. {
  171. if (lable == null)
  172. {
  173. lable = transform.Find("Lable");
  174. }
  175. if (isWorld)
  176. {
  177. return lable.transform.position;
  178. }
  179. else
  180. {
  181. return transform.InverseTransformPoint(lable.position);
  182. }
  183. }
  184. public void API_SetLablePosition(Vector3 newPosition, bool isWorld)
  185. {
  186. if (lable == null)
  187. {
  188. lable = transform.Find("Lable");
  189. }
  190. if (isWorld)
  191. {
  192. lable.transform.position = newPosition;
  193. }
  194. else
  195. {
  196. lable.transform.position = transform.TransformPoint(newPosition);
  197. }
  198. }
  199. public TooltipStartPointLocation API_GetStartPoint()
  200. {
  201. return tooltipStartPointLocation;
  202. }
  203. public void API_SetStartPoint(TooltipStartPointLocation startPoint)
  204. {
  205. tooltipStartPointLocation = startPoint;
  206. }
  207. public Vector3 API_GetStartPointPosition(bool isWorld)
  208. {
  209. if (isWorld)
  210. {
  211. return startpoint.position;
  212. }
  213. else
  214. {
  215. return transform.InverseTransformPoint(startpoint.position);
  216. }
  217. }
  218. public void API_SetStartPointVisible(bool state)
  219. {
  220. isStartPointVisible = state;
  221. }
  222. public void API_SetLineVisible(bool state)
  223. {
  224. isLineVisible = state;
  225. }
  226. public void API_SetLineWidth(float width)
  227. {
  228. Width = width;
  229. }
  230. }
  231. }