123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245 |
- 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<GameObject>("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_Tooltip>();
- modules_TooltipLineRenderer = tooltip.GetComponent<Modules_TooltipLineRenderer>();
- modules_TooltipUI = tooltip.GetComponent<Modules_TooltipUI>();
- 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
- }
|