BasicTooltip.cs 3.5 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798
  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. } else {
  38. target.HideTooltip();
  39. }
  40. }
  41. }
  42. void Update() {
  43. if (Camera.current == null) return; // 不知道为啥鼠标按下并移动的时候会有一瞬间让 Camera.current 变成 null
  44. hovered = !ReorderableListElement.isAnythingDragging && // 拖动中不显示 Tooltip
  45. RectTransformUtility.RectangleContainsScreenPoint(rectTransform, Input.mousePosition, Camera.current);
  46. // 在显示时更新 Tooltip 内容
  47. if (!hovered) return;
  48. if (textTitle.rawText != title) { // 只在属性变化时更新
  49. textTitle.text = title;
  50. textTitle.gameObject.SetActive(title != string.Empty); // 如果标题为空就隐藏
  51. }
  52. if (textDescription.rawText != description) {
  53. textDescription.text = description;
  54. }
  55. // 更新位置
  56. StartHover(UIExtensionsInputManager.MousePosition);
  57. }
  58. #region "原始 Sender"
  59. /// <summary>
  60. /// This info is needed to make sure we make the necessary translations if the tooltip and this trigger are children of different space canvases
  61. /// </summary>
  62. private bool isChildOfOverlayCanvas = false;
  63. void Start() {
  64. //attempt to check if our canvas is overlay or not and check our "is overlay" accordingly
  65. Canvas ourCanvas = GetComponentInParent<Canvas>();
  66. if (ourCanvas && ourCanvas.renderMode == RenderMode.ScreenSpaceOverlay) {
  67. isChildOfOverlayCanvas = true;
  68. }
  69. }
  70. /// <summary>
  71. /// Checks if the tooltip and the transform this trigger is attached to are children of differently-spaced Canvases
  72. /// </summary>
  73. public bool WorldToScreenIsRequired {
  74. get {
  75. return (isChildOfOverlayCanvas && target.guiMode == RenderMode.ScreenSpaceCamera) ||
  76. (!isChildOfOverlayCanvas && target.guiMode == RenderMode.ScreenSpaceOverlay);
  77. }
  78. }
  79. //public void OnDeselect(BaseEventData eventData) {
  80. // hovered = false;
  81. //}
  82. void StartHover(Vector3 position, bool shouldCanvasUpdate = false) {
  83. target.SetTooltip(position, shouldCanvasUpdate);
  84. }
  85. #endregion
  86. }