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 = new List(); private List tooltip_AlwaysShow; private List tooltip_OnPointerClick; private List tooltip_OnPointerEnter; private List tooltip_OnDrag; private bool state_click; private bool state_drag; private void Start() { ResetTooltipList(); } private void ResetTooltipList() { tooltip_AlwaysShow = new List(); tooltip_OnPointerClick = new List(); tooltip_OnPointerEnter = new List(); tooltip_OnDrag = new List(); 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("Prefabs/Tooltip"); newtooltip.tooltip = InstantiateNewTooltip(tooltip); newtooltip.tooltip.GetComponent().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; }