BasicTooltip.cs 3.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105
  1. using System.Collections;
  2. using System.Collections.Generic;
  3. using UnityEngine;
  4. using UnityEngine.EventSystems;
  5. using UnityEngine.UI.Extensions;
  6. [RequireComponent(typeof(RectTransform))]
  7. [AddComponentMenu("UI/Basic Tooltip")]
  8. public class BasicTooltip : MonoBehaviour {
  9. private static BasicTooltipReceiver target;
  10. private static TextPic textTitle, textDescription;
  11. public string title;
  12. [TextArea] public string description;
  13. private RectTransform rectTransform;
  14. void Awake() {
  15. rectTransform = GetComponent<RectTransform>();
  16. if (target == null) {
  17. // 初始化控件获取
  18. target = GameObject.Find("TooltipBasic").GetComponent<BasicTooltipReceiver>();
  19. foreach (TextPic textMesh in target.GetComponentsInChildren<TextPic>()) {
  20. if (textMesh.name == "TextTooltipBasicTitle") textTitle = textMesh;
  21. if (textMesh.name == "TextTooltipBasicDescription") textDescription = textMesh;
  22. }
  23. // 清空
  24. textTitle.text = ""; textTitle.gameObject.SetActive(false);
  25. textDescription.text = "";
  26. }
  27. Update();
  28. }
  29. private bool _hovered = false;
  30. private bool hovered {
  31. get { return _hovered; }
  32. set {
  33. if (_hovered == value) return;
  34. _hovered = value;
  35. if (value) {
  36. StartHover(UIExtensionsInputManager.MousePosition, true);
  37. StartCoroutine(HoveredMouseFollowingLoop());
  38. } else {
  39. target.HideTooltip();
  40. }
  41. }
  42. }
  43. void Update() {
  44. if (Camera.current == null) return; // 不知道为啥鼠标按下并移动的时候会有一瞬间让 Camera.current 变成 null
  45. hovered = !ReorderableListElement.isAnythingDragging && // 拖动中不显示 Tooltip
  46. RectTransformUtility.RectangleContainsScreenPoint(rectTransform, Input.mousePosition, Camera.current);
  47. // 在显示时更新 Tooltip 内容
  48. if (!hovered) return;
  49. if (textTitle.rawText != title) { // 只在属性变化时更新
  50. textTitle.text = title;
  51. textTitle.gameObject.SetActive(title != string.Empty); // 如果标题为空就隐藏
  52. }
  53. if (textDescription.rawText != description) {
  54. textDescription.text = description;
  55. }
  56. }
  57. #region "原始 Sender"
  58. /// <summary>
  59. /// This info is needed to make sure we make the necessary translations if the tooltip and this trigger are children of different space canvases
  60. /// </summary>
  61. private bool isChildOfOverlayCanvas = false;
  62. void Start() {
  63. //attempt to check if our canvas is overlay or not and check our "is overlay" accordingly
  64. Canvas ourCanvas = GetComponentInParent<Canvas>();
  65. if (ourCanvas && ourCanvas.renderMode == RenderMode.ScreenSpaceOverlay) {
  66. isChildOfOverlayCanvas = true;
  67. }
  68. }
  69. /// <summary>
  70. /// Checks if the tooltip and the transform this trigger is attached to are children of differently-spaced Canvases
  71. /// </summary>
  72. public bool WorldToScreenIsRequired {
  73. get {
  74. return (isChildOfOverlayCanvas && target.guiMode == RenderMode.ScreenSpaceCamera) ||
  75. (!isChildOfOverlayCanvas && target.guiMode == RenderMode.ScreenSpaceOverlay);
  76. }
  77. }
  78. IEnumerator HoveredMouseFollowingLoop() {
  79. while (hovered) {
  80. StartHover(UIExtensionsInputManager.MousePosition);
  81. yield return null;
  82. }
  83. }
  84. //public void OnDeselect(BaseEventData eventData) {
  85. // hovered = false;
  86. //}
  87. void StartHover(Vector3 position, bool shouldCanvasUpdate = false) {
  88. Update();
  89. target.SetTooltip(position, shouldCanvasUpdate);
  90. }
  91. #endregion
  92. }