123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215 |
- using System.Collections;
- using System.Collections.Generic;
- using UnityEngine;
- using UnityEngine.Events;
- using UnityEngine.EventSystems;
- using SC.XR.Unity.Module_Tooltip;
- using System;
- [AddComponentMenu("SDK/TooltipControl")]
- [RequireComponent(typeof(BoxCollider))]
- public class Modules_TooltipCtrl : MonoBehaviour, IPointerHandler
- {
- public enum TooltipShowTrigger
- {
- NULL,
- AlwaysShow,
- OnPointerClick,
- OnPointerEnter,
- OnDrag
- }
- [SerializeField]
- public List<TooltipList> tooltipList = new List<TooltipList>();
- private List<GameObject> tooltip_AlwaysShow;
- private List<GameObject> tooltip_OnPointerClick;
- private List<GameObject> tooltip_OnPointerEnter;
- private List<GameObject> tooltip_OnDrag;
- private bool state_click;
- private bool state_drag;
- private void Start()
- {
- ResetTooltipList();
- }
- private void ResetTooltipList()
- {
- tooltip_AlwaysShow = new List<GameObject>();
- tooltip_OnPointerClick = new List<GameObject>();
- tooltip_OnPointerEnter = new List<GameObject>();
- tooltip_OnDrag = new List<GameObject>();
- state_click = false;
- state_drag = false;
- if (tooltipList.Count != 0)
- {
- //向各列表中添加GameObject,同时根据类型设置Active
- for (int i = 0; i < tooltipList.Count; i++)
- {
- switch (tooltipList[i].trigger)
- {
- case TooltipShowTrigger.AlwaysShow:
- tooltip_AlwaysShow.Add(tooltipList[i].tooltip);
- tooltipList[i].tooltip.SetActive(true);
- break;
- case TooltipShowTrigger.OnPointerClick:
- tooltip_OnPointerClick.Add(tooltipList[i].tooltip);
- tooltipList[i].tooltip.SetActive(false);
- break;
- case TooltipShowTrigger.OnPointerEnter:
- tooltip_OnPointerEnter.Add(tooltipList[i].tooltip);
- tooltipList[i].tooltip.SetActive(false);
- break;
- case TooltipShowTrigger.OnDrag:
- tooltip_OnDrag.Add(tooltipList[i].tooltip);
- tooltipList[i].tooltip.SetActive(false);
- break;
- default:
- break;
- }
- }
- }
- else
- {
- return;
- }
- }
- public GameObject InstantiateNewTooltip(GameObject tooltip)
- {
- Vector3 location = transform.position;
- location.y += transform.lossyScale.y / 2 + 0.1f;
- int count = tooltipList.Count;
- tooltip = Instantiate(tooltip, location, transform.rotation, transform);
- Vector3 scale = new Vector3(1 / transform.lossyScale.x, 1 / transform.lossyScale.y, 1 / transform.lossyScale.z);
- tooltip.transform.localScale = scale;
- tooltip.name = "Tooltip_" + count;
- return tooltip;
- }
- void IPointerEnterHandler.OnPointerEnter(PointerEventData eventData)
- {
- //throw new System.NotImplementedException();
- for (int i = 0; i < tooltip_OnPointerEnter.Count; i++)
- {
- tooltip_OnPointerEnter[i].SetActive(true);
- }
- }
- void IPointerExitHandler.OnPointerExit(PointerEventData eventData)
- {
- //throw new System.NotImplementedException();
- for (int i = 0; i < tooltip_OnPointerEnter.Count; i++)
- {
- tooltip_OnPointerEnter[i].SetActive(false);
- }
- }
- void IPointerDownHandler.OnPointerDown(PointerEventData eventData)
- {
- //throw new System.NotImplementedException();
- if (!state_drag)
- {
- for (int i = 0; i < tooltip_OnDrag.Count; i++)
- {
- tooltip_OnDrag[i].SetActive(true);
- }
- state_drag = !state_drag;
- }
- }
- void IPointerClickHandler.OnPointerClick(PointerEventData eventData)
- {
- if (state_click)
- {
- for (int i = 0; i < tooltip_OnPointerClick.Count; i++)
- {
- tooltip_OnPointerClick[i].SetActive(false);
- }
- state_click = !state_click;
- }
- else
- {
- for (int i = 0; i < tooltip_OnPointerClick.Count; i++)
- {
- tooltip_OnPointerClick[i].SetActive(true);
- }
- state_click = !state_click;
- }
- //throw new System.NotImplementedException();
- }
- void IPointerUpHandler.OnPointerUp(PointerEventData eventData)
- {
- //throw new System.NotImplementedException();
- if (state_drag)
- {
- for (int i = 0; i < tooltip_OnDrag.Count; i++)
- {
- tooltip_OnDrag[i].SetActive(false);
- }
- state_drag = !state_drag;
- }
- }
- void IDragHandler.OnDrag(PointerEventData eventData)
- {
- //throw new System.NotImplementedException();
- }
- public TooltipShowTrigger API_GetTooltipShowTrigger(GameObject gameObject)
- {
- for (int i = 0; i < tooltipList.Count; i++)
- {
- if (tooltipList[i].tooltip == gameObject)
- {
- return tooltipList[i].trigger;
- }
- }
- Debug.Log("Unable to find the tooltip!");
- return 0;
- }
- public void API_SetTooltipShowTrigger(GameObject gameObject, TooltipShowTrigger trigger)
- {
- for (int i = 0; i < tooltipList.Count; i++)
- {
- if (tooltipList[i].tooltip == gameObject)
- {
- tooltipList[i].trigger = trigger;
- ResetTooltipList();
- return;
- }
- }
- Debug.Log("Unable to find the tooltip!");
- }
- public GameObject API_AddNewTooltip(TooltipShowTrigger showTrigger, string text)
- {
- TooltipList newtooltip = new TooltipList();
- GameObject tooltip = Resources.Load<GameObject>("Prefabs/Tooltip");
- newtooltip.tooltip = InstantiateNewTooltip(tooltip);
- newtooltip.tooltip.GetComponent<Modules_TooltipUI>().API_SetLableText(text);
- newtooltip.trigger = showTrigger;
- tooltipList.Add(newtooltip);
- ResetTooltipList();
- return newtooltip.tooltip;
- }
- public void API_DelTooltip(GameObject tooltip)
- {
- for (int i = 0; i < tooltipList.Count; i++)
- {
- if (tooltipList[i].tooltip == tooltip)
- {
- tooltipList.Remove(tooltipList[i]);
- Destroy(tooltip);
- ResetTooltipList();
- return;
- }
- }
- Debug.Log("Unable to find the tooltip!");
- }
- }
- [System.Serializable]
- public class TooltipList
- {
- [SerializeField]
- public GameObject tooltip;
- [SerializeField]
- public Modules_TooltipCtrl.TooltipShowTrigger trigger;
- }
|