Modules_Tooltip.cs 3.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137
  1. using System.Collections;
  2. using System.Collections.Generic;
  3. using TMPro;
  4. using UnityEngine;
  5. namespace SC.XR.Unity.Module_Tooltip
  6. {
  7. public class Modules_Tooltip : MonoBehaviour
  8. {
  9. public enum TooltipRotationAxis
  10. {
  11. None,
  12. X,
  13. Y,
  14. Z,
  15. XY,
  16. XZ,
  17. YZ,
  18. XYZ
  19. }
  20. private Transform lable;
  21. [SerializeField]
  22. [Tooltip("Select the axis which object Lable will rotate.")]
  23. private TooltipRotationAxis rotationAxis = TooltipRotationAxis.XY;
  24. [SerializeField]
  25. [Tooltip("Select the target which object Lable will towards to." +
  26. "\nIf no target is selected,the head of SDKSystem will be used.")]
  27. private Transform RotationTarget;
  28. private Quaternion originalRotation;
  29. private bool readytState = false;
  30. private void Start()
  31. {
  32. GetReady();
  33. }
  34. private void Update()
  35. {
  36. HeadRotate();
  37. }
  38. private void GetReady()
  39. {
  40. lable = transform.Find("Lable");
  41. originalRotation = lable.rotation;
  42. if (RotationTarget == null)
  43. {
  44. RotationTarget = Camera.main.transform;
  45. }
  46. readytState = true;
  47. }
  48. private void HeadRotate()
  49. {
  50. if (RotationTarget == null)
  51. {
  52. RotationTarget = Camera.main.transform;
  53. }
  54. Vector3 headToTarger = RotationTarget.position - lable.position;
  55. //Too close to rotate
  56. if (headToTarger.sqrMagnitude < 0.01)
  57. {
  58. return;
  59. }
  60. bool useHeadUp = true;
  61. switch (rotationAxis)
  62. {
  63. case TooltipRotationAxis.None:
  64. return;
  65. case TooltipRotationAxis.X:
  66. headToTarger.x = 0;
  67. useHeadUp = false;
  68. break;
  69. case TooltipRotationAxis.Y:
  70. headToTarger.y = 0;
  71. useHeadUp = false;
  72. break;
  73. case TooltipRotationAxis.Z:
  74. headToTarger.x = 0;
  75. headToTarger.y = 0;
  76. break;
  77. case TooltipRotationAxis.XY:
  78. useHeadUp = false;
  79. break;
  80. case TooltipRotationAxis.XZ:
  81. headToTarger.x = 0;
  82. break;
  83. case TooltipRotationAxis.YZ:
  84. headToTarger.y = 0;
  85. break;
  86. case TooltipRotationAxis.XYZ:
  87. default:
  88. break;
  89. }
  90. if (useHeadUp)
  91. {
  92. lable.rotation = Quaternion.LookRotation(-headToTarger, Camera.main.transform.up);
  93. }
  94. else
  95. {
  96. lable.rotation = Quaternion.LookRotation(-headToTarger);
  97. }
  98. }
  99. public TooltipRotationAxis API_GetTooltipRotationAxis()
  100. {
  101. if (!readytState)
  102. {
  103. GetReady();
  104. }
  105. return rotationAxis;
  106. }
  107. public void API_SetTooltipRotationAxis(TooltipRotationAxis newRotationAxis)
  108. {
  109. if (!readytState)
  110. {
  111. GetReady();
  112. }
  113. lable.rotation = originalRotation;
  114. rotationAxis = newRotationAxis;
  115. }
  116. public void API_SetTooltipRotationAxisTarget(Transform target)
  117. {
  118. if (!readytState)
  119. {
  120. GetReady();
  121. }
  122. if (target != null)
  123. {
  124. RotationTarget = target;
  125. }
  126. }
  127. }
  128. }