using System.Collections; using System.Collections.Generic; using TMPro; using UnityEngine; namespace SC.XR.Unity.Module_Tooltip { public class Modules_Tooltip : MonoBehaviour { public enum TooltipRotationAxis { None, X, Y, Z, XY, XZ, YZ, XYZ } private Transform lable; [SerializeField] [Tooltip("Select the axis which object Lable will rotate.")] private TooltipRotationAxis rotationAxis = TooltipRotationAxis.XY; [SerializeField] [Tooltip("Select the target which object Lable will towards to." + "\nIf no target is selected,the head of SDKSystem will be used.")] private Transform RotationTarget; private Quaternion originalRotation; private bool readytState = false; private void Start() { GetReady(); } private void Update() { HeadRotate(); } private void GetReady() { lable = transform.Find("Lable"); originalRotation = lable.rotation; if (RotationTarget == null) { RotationTarget = Camera.main.transform; } readytState = true; } private void HeadRotate() { if (RotationTarget == null) { RotationTarget = Camera.main.transform; } Vector3 headToTarger = RotationTarget.position - lable.position; //Too close to rotate if (headToTarger.sqrMagnitude < 0.01) { return; } bool useHeadUp = true; switch (rotationAxis) { case TooltipRotationAxis.None: return; case TooltipRotationAxis.X: headToTarger.x = 0; useHeadUp = false; break; case TooltipRotationAxis.Y: headToTarger.y = 0; useHeadUp = false; break; case TooltipRotationAxis.Z: headToTarger.x = 0; headToTarger.y = 0; break; case TooltipRotationAxis.XY: useHeadUp = false; break; case TooltipRotationAxis.XZ: headToTarger.x = 0; break; case TooltipRotationAxis.YZ: headToTarger.y = 0; break; case TooltipRotationAxis.XYZ: default: break; } if (useHeadUp) { lable.rotation = Quaternion.LookRotation(-headToTarger, Camera.main.transform.up); } else { lable.rotation = Quaternion.LookRotation(-headToTarger); } } public TooltipRotationAxis API_GetTooltipRotationAxis() { if (!readytState) { GetReady(); } return rotationAxis; } public void API_SetTooltipRotationAxis(TooltipRotationAxis newRotationAxis) { if (!readytState) { GetReady(); } lable.rotation = originalRotation; rotationAxis = newRotationAxis; } public void API_SetTooltipRotationAxisTarget(Transform target) { if (!readytState) { GetReady(); } if (target != null) { RotationTarget = target; } } } }