using System.Collections; using System.Collections.Generic; using UnityEngine; using SC.XR.Unity.Module_Tooltip; using UnityEngine.EventSystems; [AddComponentMenu("SDK/TooltipIncrease")] [RequireComponent(typeof(BoxCollider))] public class Module_TooltipIncrease : MonoBehaviour, IPointerHandler { private enum AppearTrigger { OnPointClick, OnPointerEnter } private enum DisappearTrigger { NeverDisappear, OnPointClick, OnPointerExit, } [SerializeField] [Tooltip("When to show tooltip.")] private AppearTrigger appearTrigger = AppearTrigger.OnPointClick; [SerializeField] [Tooltip("When to disappear tooltip.")] private DisappearTrigger disappearTrigger = DisappearTrigger.OnPointClick; [SerializeField] [Range(0f, 5f)] [Tooltip("Wait seconds before tooltip appear.")] private float appearDelay = 0; [SerializeField] [Range(0f, 5f)] [Tooltip("Wait seconds before tooltip diaappear.")] private float disappearDelay = 0; [SerializeField] [Tooltip("Select the axis which object Lable will rotate.")] private Modules_Tooltip.TooltipRotationAxis rotationAxis = Modules_Tooltip.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 = null; [SerializeField] [Tooltip("Select the starting point of the line.")] private Modules_TooltipLineRenderer.TooltipStartPointLocation tooltipStartPointLocation = Modules_TooltipLineRenderer.TooltipStartPointLocation.Auto; [SerializeField] [Tooltip("Whether the point is visible.")] private bool isStartPointVisible = true; [SerializeField] [Tooltip("Whether the line is visible.")] private bool isLineVisible = true; [SerializeField] [Range(0, 0.01f)] [Tooltip("The width of line")] private float lineWidth = 0.003f; [SerializeField] [Tooltip("The local position of lable.")] private Vector3 lablePosition = new Vector3(0, 0.2f, 0); [SerializeField] [Tooltip("The local position of target.")] private Vector3 targetPosition = new Vector3(0, 0.1f, 0); [TextArea] [SerializeField] [Tooltip("Text for the ToolTip to display")] private string toolTipText = "New Tooltip"; [SerializeField] [Tooltip("")] private Color textColor = Color.white; [SerializeField] [Range(0f, 100f)] [Tooltip("Size of text")] private float textSize = 32; [SerializeField] [Range(0f, 100f)] [Tooltip("The width of lable")] private float lableWidth = 20; [SerializeField] [Range(0f, 100f)] [Tooltip("The height of lable")] private float lableHeight = 5; [SerializeField] [Range(0, 0.02f)] [Tooltip("The scale of lable")] private float lableScale = 0.01f; [SerializeField] [Range(0f, 100f)] [Tooltip("The scale of tooltip.\nShould be equal to 1/localscale of this gameobject.")] private float tooltipScale = 1; private bool isCreated; private bool isActive; private GameObject tooltip; private Modules_Tooltip modules_Tooltip; private Modules_TooltipLineRenderer modules_TooltipLineRenderer; private Modules_TooltipUI modules_TooltipUI; private void Start() { isCreated = false; isActive = false; tooltip = Resources.Load("Prefabs/Tooltip"); } private void AppearTooltip() { if (!isCreated) { if (tooltip == null) { Debug.Log("Tooltip Load Failed."); return; } tooltip = Instantiate(tooltip, transform); modules_Tooltip = tooltip.GetComponent(); modules_TooltipLineRenderer = tooltip.GetComponent(); modules_TooltipUI = tooltip.GetComponent(); modules_Tooltip.API_SetTooltipRotationAxis(rotationAxis); modules_Tooltip.API_SetTooltipRotationAxisTarget(rotationTarget); modules_TooltipLineRenderer.API_SetStartPoint(tooltipStartPointLocation); modules_TooltipLineRenderer.API_SetStartPointVisible(isStartPointVisible); modules_TooltipLineRenderer.API_SetLineVisible(isLineVisible); modules_TooltipLineRenderer.API_SetLineWidth(lineWidth); modules_TooltipUI.API_SetLableText(toolTipText); modules_TooltipUI.API_SetLableTextColor(textColor); modules_TooltipUI.API_SetLableTextSize(textSize); modules_TooltipUI.API_SetLableSize(lableWidth, lableHeight); modules_TooltipUI.API_SetLableScale(lableScale); tooltip.transform.localScale = new Vector3(tooltipScale, tooltipScale, tooltipScale); //print(targetPosition); //print(lablePosition); modules_TooltipLineRenderer.API_SetTargetPosition(targetPosition, false); //print(modules_TooltipLineRenderer.API_GetTargetPosition(false)); modules_TooltipLineRenderer.API_SetLablePosition(lablePosition, false); //print(modules_TooltipLineRenderer.API_GetLablePosition(false)); isCreated = true; } else { tooltip.SetActive(true); //print(modules_TooltipLineRenderer.API_GetTargetPosition(false)); //print(modules_TooltipLineRenderer.API_GetLablePosition(false)); } isActive = true; } private void DisappearTooltip() { if (isCreated) { tooltip.SetActive(false); } isActive = false; } private IEnumerator WaitForAppear() { yield return new WaitForSeconds(appearDelay); AppearTooltip(); } private IEnumerator WaitForDisappear() { yield return new WaitForSeconds(disappearDelay); DisappearTooltip(); } void IPointerExitHandler.OnPointerExit(PointerEventData eventData) { if (disappearTrigger == DisappearTrigger.OnPointerExit) { StartCoroutine("WaitForDisappear"); } } void IPointerEnterHandler.OnPointerEnter(PointerEventData eventData) { if (appearTrigger == AppearTrigger.OnPointerEnter) { StartCoroutine("WaitForAppear"); } } void IPointerDownHandler.OnPointerDown(PointerEventData eventData) { } void IPointerClickHandler.OnPointerClick(PointerEventData eventData) { if (appearTrigger == AppearTrigger.OnPointClick && !isActive) { StartCoroutine("WaitForAppear"); } else if (disappearTrigger == DisappearTrigger.OnPointClick && isActive) { StartCoroutine("WaitForDisappear"); } } void IPointerUpHandler.OnPointerUp(PointerEventData eventData) { } void IDragHandler.OnDrag(PointerEventData eventData) { } #if UNITY_EDITOR private void OnDrawGizmos() { if (Application.isPlaying) { return; } if (gameObject == UnityEditor.Selection.activeGameObject) { Gizmos.color = Color.cyan; Vector3 lablecenter = transform.position + new Vector3(transform.localScale.x * tooltipScale * lablePosition.x, transform.localScale.y * tooltipScale * lablePosition.y, transform.localScale.z * tooltipScale * lablePosition.z); Vector3 targetcenter = transform.position + new Vector3(transform.localScale.x * tooltipScale * targetPosition.x, transform.localScale.y * tooltipScale * targetPosition.y, transform.localScale.z * tooltipScale * targetPosition.z); Gizmos.DrawWireCube(lablecenter, new Vector3(lableWidth, lableHeight, 1) * lableScale); Gizmos.DrawLine(targetcenter, lablecenter); } } #endif // UNITY_EDITOR }