Modules_TooltipCtrl.cs 6.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215
  1. using System.Collections;
  2. using System.Collections.Generic;
  3. using UnityEngine;
  4. using UnityEngine.Events;
  5. using UnityEngine.EventSystems;
  6. using SC.XR.Unity.Module_Tooltip;
  7. using System;
  8. [AddComponentMenu("SDK/TooltipControl")]
  9. [RequireComponent(typeof(BoxCollider))]
  10. public class Modules_TooltipCtrl : MonoBehaviour, IPointerHandler
  11. {
  12. public enum TooltipShowTrigger
  13. {
  14. NULL,
  15. AlwaysShow,
  16. OnPointerClick,
  17. OnPointerEnter,
  18. OnDrag
  19. }
  20. [SerializeField]
  21. public List<TooltipList> tooltipList = new List<TooltipList>();
  22. private List<GameObject> tooltip_AlwaysShow;
  23. private List<GameObject> tooltip_OnPointerClick;
  24. private List<GameObject> tooltip_OnPointerEnter;
  25. private List<GameObject> tooltip_OnDrag;
  26. private bool state_click;
  27. private bool state_drag;
  28. private void Start()
  29. {
  30. ResetTooltipList();
  31. }
  32. private void ResetTooltipList()
  33. {
  34. tooltip_AlwaysShow = new List<GameObject>();
  35. tooltip_OnPointerClick = new List<GameObject>();
  36. tooltip_OnPointerEnter = new List<GameObject>();
  37. tooltip_OnDrag = new List<GameObject>();
  38. state_click = false;
  39. state_drag = false;
  40. if (tooltipList.Count != 0)
  41. {
  42. //向各列表中添加GameObject,同时根据类型设置Active
  43. for (int i = 0; i < tooltipList.Count; i++)
  44. {
  45. switch (tooltipList[i].trigger)
  46. {
  47. case TooltipShowTrigger.AlwaysShow:
  48. tooltip_AlwaysShow.Add(tooltipList[i].tooltip);
  49. tooltipList[i].tooltip.SetActive(true);
  50. break;
  51. case TooltipShowTrigger.OnPointerClick:
  52. tooltip_OnPointerClick.Add(tooltipList[i].tooltip);
  53. tooltipList[i].tooltip.SetActive(false);
  54. break;
  55. case TooltipShowTrigger.OnPointerEnter:
  56. tooltip_OnPointerEnter.Add(tooltipList[i].tooltip);
  57. tooltipList[i].tooltip.SetActive(false);
  58. break;
  59. case TooltipShowTrigger.OnDrag:
  60. tooltip_OnDrag.Add(tooltipList[i].tooltip);
  61. tooltipList[i].tooltip.SetActive(false);
  62. break;
  63. default:
  64. break;
  65. }
  66. }
  67. }
  68. else
  69. {
  70. return;
  71. }
  72. }
  73. public GameObject InstantiateNewTooltip(GameObject tooltip)
  74. {
  75. Vector3 location = transform.position;
  76. location.y += transform.lossyScale.y / 2 + 0.1f;
  77. int count = tooltipList.Count;
  78. tooltip = Instantiate(tooltip, location, transform.rotation, transform);
  79. Vector3 scale = new Vector3(1 / transform.lossyScale.x, 1 / transform.lossyScale.y, 1 / transform.lossyScale.z);
  80. tooltip.transform.localScale = scale;
  81. tooltip.name = "Tooltip_" + count;
  82. return tooltip;
  83. }
  84. void IPointerEnterHandler.OnPointerEnter(PointerEventData eventData)
  85. {
  86. //throw new System.NotImplementedException();
  87. for (int i = 0; i < tooltip_OnPointerEnter.Count; i++)
  88. {
  89. tooltip_OnPointerEnter[i].SetActive(true);
  90. }
  91. }
  92. void IPointerExitHandler.OnPointerExit(PointerEventData eventData)
  93. {
  94. //throw new System.NotImplementedException();
  95. for (int i = 0; i < tooltip_OnPointerEnter.Count; i++)
  96. {
  97. tooltip_OnPointerEnter[i].SetActive(false);
  98. }
  99. }
  100. void IPointerDownHandler.OnPointerDown(PointerEventData eventData)
  101. {
  102. //throw new System.NotImplementedException();
  103. if (!state_drag)
  104. {
  105. for (int i = 0; i < tooltip_OnDrag.Count; i++)
  106. {
  107. tooltip_OnDrag[i].SetActive(true);
  108. }
  109. state_drag = !state_drag;
  110. }
  111. }
  112. void IPointerClickHandler.OnPointerClick(PointerEventData eventData)
  113. {
  114. if (state_click)
  115. {
  116. for (int i = 0; i < tooltip_OnPointerClick.Count; i++)
  117. {
  118. tooltip_OnPointerClick[i].SetActive(false);
  119. }
  120. state_click = !state_click;
  121. }
  122. else
  123. {
  124. for (int i = 0; i < tooltip_OnPointerClick.Count; i++)
  125. {
  126. tooltip_OnPointerClick[i].SetActive(true);
  127. }
  128. state_click = !state_click;
  129. }
  130. //throw new System.NotImplementedException();
  131. }
  132. void IPointerUpHandler.OnPointerUp(PointerEventData eventData)
  133. {
  134. //throw new System.NotImplementedException();
  135. if (state_drag)
  136. {
  137. for (int i = 0; i < tooltip_OnDrag.Count; i++)
  138. {
  139. tooltip_OnDrag[i].SetActive(false);
  140. }
  141. state_drag = !state_drag;
  142. }
  143. }
  144. void IDragHandler.OnDrag(PointerEventData eventData)
  145. {
  146. //throw new System.NotImplementedException();
  147. }
  148. public TooltipShowTrigger API_GetTooltipShowTrigger(GameObject gameObject)
  149. {
  150. for (int i = 0; i < tooltipList.Count; i++)
  151. {
  152. if (tooltipList[i].tooltip == gameObject)
  153. {
  154. return tooltipList[i].trigger;
  155. }
  156. }
  157. Debug.Log("Unable to find the tooltip!");
  158. return 0;
  159. }
  160. public void API_SetTooltipShowTrigger(GameObject gameObject, TooltipShowTrigger trigger)
  161. {
  162. for (int i = 0; i < tooltipList.Count; i++)
  163. {
  164. if (tooltipList[i].tooltip == gameObject)
  165. {
  166. tooltipList[i].trigger = trigger;
  167. ResetTooltipList();
  168. return;
  169. }
  170. }
  171. Debug.Log("Unable to find the tooltip!");
  172. }
  173. public GameObject API_AddNewTooltip(TooltipShowTrigger showTrigger, string text)
  174. {
  175. TooltipList newtooltip = new TooltipList();
  176. GameObject tooltip = Resources.Load<GameObject>("Prefabs/Tooltip");
  177. newtooltip.tooltip = InstantiateNewTooltip(tooltip);
  178. newtooltip.tooltip.GetComponent<Modules_TooltipUI>().API_SetLableText(text);
  179. newtooltip.trigger = showTrigger;
  180. tooltipList.Add(newtooltip);
  181. ResetTooltipList();
  182. return newtooltip.tooltip;
  183. }
  184. public void API_DelTooltip(GameObject tooltip)
  185. {
  186. for (int i = 0; i < tooltipList.Count; i++)
  187. {
  188. if (tooltipList[i].tooltip == tooltip)
  189. {
  190. tooltipList.Remove(tooltipList[i]);
  191. Destroy(tooltip);
  192. ResetTooltipList();
  193. return;
  194. }
  195. }
  196. Debug.Log("Unable to find the tooltip!");
  197. }
  198. }
  199. [System.Serializable]
  200. public class TooltipList
  201. {
  202. [SerializeField]
  203. public GameObject tooltip;
  204. [SerializeField]
  205. public Modules_TooltipCtrl.TooltipShowTrigger trigger;
  206. }