Module_TooltipIncrease.cs 8.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245
  1. using System.Collections;
  2. using System.Collections.Generic;
  3. using UnityEngine;
  4. using SC.XR.Unity.Module_Tooltip;
  5. using UnityEngine.EventSystems;
  6. [AddComponentMenu("SDK/TooltipIncrease")]
  7. [RequireComponent(typeof(BoxCollider))]
  8. public class Module_TooltipIncrease : MonoBehaviour, IPointerHandler
  9. {
  10. private enum AppearTrigger
  11. {
  12. OnPointClick,
  13. OnPointerEnter
  14. }
  15. private enum DisappearTrigger
  16. {
  17. NeverDisappear,
  18. OnPointClick,
  19. OnPointerExit,
  20. }
  21. [SerializeField]
  22. [Tooltip("When to show tooltip.")]
  23. private AppearTrigger appearTrigger = AppearTrigger.OnPointClick;
  24. [SerializeField]
  25. [Tooltip("When to disappear tooltip.")]
  26. private DisappearTrigger disappearTrigger = DisappearTrigger.OnPointClick;
  27. [SerializeField]
  28. [Range(0f, 5f)]
  29. [Tooltip("Wait seconds before tooltip appear.")]
  30. private float appearDelay = 0;
  31. [SerializeField]
  32. [Range(0f, 5f)]
  33. [Tooltip("Wait seconds before tooltip diaappear.")]
  34. private float disappearDelay = 0;
  35. [SerializeField]
  36. [Tooltip("Select the axis which object Lable will rotate.")]
  37. private Modules_Tooltip.TooltipRotationAxis rotationAxis =
  38. Modules_Tooltip.TooltipRotationAxis.XY;
  39. [SerializeField]
  40. [Tooltip("Select the target which object Lable will towards to." +
  41. "\nIf no target is selected,the head of SDKSystem will be used.")]
  42. private Transform rotationTarget = null;
  43. [SerializeField]
  44. [Tooltip("Select the starting point of the line.")]
  45. private Modules_TooltipLineRenderer.TooltipStartPointLocation tooltipStartPointLocation =
  46. Modules_TooltipLineRenderer.TooltipStartPointLocation.Auto;
  47. [SerializeField]
  48. [Tooltip("Whether the point is visible.")]
  49. private bool isStartPointVisible = true;
  50. [SerializeField]
  51. [Tooltip("Whether the line is visible.")]
  52. private bool isLineVisible = true;
  53. [SerializeField]
  54. [Range(0, 0.01f)]
  55. [Tooltip("The width of line")]
  56. private float lineWidth = 0.003f;
  57. [SerializeField]
  58. [Tooltip("The local position of lable.")]
  59. private Vector3 lablePosition = new Vector3(0, 0.2f, 0);
  60. [SerializeField]
  61. [Tooltip("The local position of target.")]
  62. private Vector3 targetPosition = new Vector3(0, 0.1f, 0);
  63. [TextArea]
  64. [SerializeField]
  65. [Tooltip("Text for the ToolTip to display")]
  66. private string toolTipText = "New Tooltip";
  67. [SerializeField]
  68. [Tooltip("")]
  69. private Color textColor = Color.white;
  70. [SerializeField]
  71. [Range(0f, 100f)]
  72. [Tooltip("Size of text")]
  73. private float textSize = 32;
  74. [SerializeField]
  75. [Range(0f, 100f)]
  76. [Tooltip("The width of lable")]
  77. private float lableWidth = 20;
  78. [SerializeField]
  79. [Range(0f, 100f)]
  80. [Tooltip("The height of lable")]
  81. private float lableHeight = 5;
  82. [SerializeField]
  83. [Range(0, 0.02f)]
  84. [Tooltip("The scale of lable")]
  85. private float lableScale = 0.01f;
  86. [SerializeField]
  87. [Range(0f, 100f)]
  88. [Tooltip("The scale of tooltip.\nShould be equal to 1/localscale of this gameobject.")]
  89. private float tooltipScale = 1;
  90. private bool isCreated;
  91. private bool isActive;
  92. private GameObject tooltip;
  93. private Modules_Tooltip modules_Tooltip;
  94. private Modules_TooltipLineRenderer modules_TooltipLineRenderer;
  95. private Modules_TooltipUI modules_TooltipUI;
  96. private void Start()
  97. {
  98. isCreated = false;
  99. isActive = false;
  100. tooltip = Resources.Load<GameObject>("Prefabs/Tooltip");
  101. }
  102. private void AppearTooltip()
  103. {
  104. if (!isCreated)
  105. {
  106. if (tooltip == null)
  107. {
  108. Debug.Log("Tooltip Load Failed.");
  109. return;
  110. }
  111. tooltip = Instantiate(tooltip, transform);
  112. modules_Tooltip = tooltip.GetComponent<Modules_Tooltip>();
  113. modules_TooltipLineRenderer = tooltip.GetComponent<Modules_TooltipLineRenderer>();
  114. modules_TooltipUI = tooltip.GetComponent<Modules_TooltipUI>();
  115. modules_Tooltip.API_SetTooltipRotationAxis(rotationAxis);
  116. modules_Tooltip.API_SetTooltipRotationAxisTarget(rotationTarget);
  117. modules_TooltipLineRenderer.API_SetStartPoint(tooltipStartPointLocation);
  118. modules_TooltipLineRenderer.API_SetStartPointVisible(isStartPointVisible);
  119. modules_TooltipLineRenderer.API_SetLineVisible(isLineVisible);
  120. modules_TooltipLineRenderer.API_SetLineWidth(lineWidth);
  121. modules_TooltipUI.API_SetLableText(toolTipText);
  122. modules_TooltipUI.API_SetLableTextColor(textColor);
  123. modules_TooltipUI.API_SetLableTextSize(textSize);
  124. modules_TooltipUI.API_SetLableSize(lableWidth, lableHeight);
  125. modules_TooltipUI.API_SetLableScale(lableScale);
  126. tooltip.transform.localScale = new Vector3(tooltipScale, tooltipScale, tooltipScale);
  127. //print(targetPosition);
  128. //print(lablePosition);
  129. modules_TooltipLineRenderer.API_SetTargetPosition(targetPosition, false);
  130. //print(modules_TooltipLineRenderer.API_GetTargetPosition(false));
  131. modules_TooltipLineRenderer.API_SetLablePosition(lablePosition, false);
  132. //print(modules_TooltipLineRenderer.API_GetLablePosition(false));
  133. isCreated = true;
  134. }
  135. else
  136. {
  137. tooltip.SetActive(true);
  138. //print(modules_TooltipLineRenderer.API_GetTargetPosition(false));
  139. //print(modules_TooltipLineRenderer.API_GetLablePosition(false));
  140. }
  141. isActive = true;
  142. }
  143. private void DisappearTooltip()
  144. {
  145. if (isCreated)
  146. {
  147. tooltip.SetActive(false);
  148. }
  149. isActive = false;
  150. }
  151. private IEnumerator WaitForAppear()
  152. {
  153. yield return new WaitForSeconds(appearDelay);
  154. AppearTooltip();
  155. }
  156. private IEnumerator WaitForDisappear()
  157. {
  158. yield return new WaitForSeconds(disappearDelay);
  159. DisappearTooltip();
  160. }
  161. void IPointerExitHandler.OnPointerExit(PointerEventData eventData)
  162. {
  163. if (disappearTrigger == DisappearTrigger.OnPointerExit)
  164. {
  165. StartCoroutine("WaitForDisappear");
  166. }
  167. }
  168. void IPointerEnterHandler.OnPointerEnter(PointerEventData eventData)
  169. {
  170. if (appearTrigger == AppearTrigger.OnPointerEnter)
  171. {
  172. StartCoroutine("WaitForAppear");
  173. }
  174. }
  175. void IPointerDownHandler.OnPointerDown(PointerEventData eventData)
  176. {
  177. }
  178. void IPointerClickHandler.OnPointerClick(PointerEventData eventData)
  179. {
  180. if (appearTrigger == AppearTrigger.OnPointClick && !isActive)
  181. {
  182. StartCoroutine("WaitForAppear");
  183. }
  184. else if (disappearTrigger == DisappearTrigger.OnPointClick && isActive)
  185. {
  186. StartCoroutine("WaitForDisappear");
  187. }
  188. }
  189. void IPointerUpHandler.OnPointerUp(PointerEventData eventData)
  190. {
  191. }
  192. void IDragHandler.OnDrag(PointerEventData eventData)
  193. {
  194. }
  195. #if UNITY_EDITOR
  196. private void OnDrawGizmos()
  197. {
  198. if (Application.isPlaying)
  199. {
  200. return;
  201. }
  202. if (gameObject == UnityEditor.Selection.activeGameObject)
  203. {
  204. Gizmos.color = Color.cyan;
  205. Vector3 lablecenter = transform.position + new Vector3(transform.localScale.x * tooltipScale * lablePosition.x,
  206. transform.localScale.y * tooltipScale * lablePosition.y, transform.localScale.z * tooltipScale * lablePosition.z);
  207. Vector3 targetcenter = transform.position + new Vector3(transform.localScale.x * tooltipScale * targetPosition.x,
  208. transform.localScale.y * tooltipScale * targetPosition.y, transform.localScale.z * tooltipScale * targetPosition.z);
  209. Gizmos.DrawWireCube(lablecenter, new Vector3(lableWidth, lableHeight, 1) * lableScale);
  210. Gizmos.DrawLine(targetcenter, lablecenter);
  211. }
  212. }
  213. #endif // UNITY_EDITOR
  214. }