Tooltip 用于ui布局
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using TMPro;
using UnityEngine.UI;
[ExecuteInEditMode()] // 可以在编辑模式下运行
public class Tooltip : MonoBehaviour
{
public TMP_Text header; // 头部文本
public TMP_Text content; // 内容文本
public Image image; // 图片
public LayoutElement layoutElement; // 布局元素,用于控制大小
public int characterWrapLinmit; // 字符换行限制
public RectTransform rectTransform; // RectTransform
// 控制大小和位置
public void ControlSizeAndPosition()
{
int headerLength = header.text.Length; // 头部文本长度
int contentLength = content.text.Length; // 内容文本长度
// 如果文本超过设定的换行限制,则启用布局元素
layoutElement.enabled = (characterWrapLinmit < headerLength || characterWrapLinmit < contentLength) ? true : false;
Vector2 position = Input.mousePosition; // 获取鼠标位置
transform.position = position; // 设置提示框位置
// 计算提示框的中心点相对于屏幕的位置
float pivotX = position.x / Screen.width;
float pivotY = position.y / Screen.height;
rectTransform.pivot = new Vector2(pivotX, pivotY); // 设置提示框的中心点
}
}
ToolTipCanvas 做成单列模式方便其他类调用
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class Singleton<T> : MonoBehaviour where T : Component
{
public static T instance { get; private set; }
protected virtual void Awake()
{
if(instance == null)
{
// as 强制转换类型
instance=this as T;
}
else
{
Destroy(instance);
}
}
}
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class ToolTipCanvas : Singleton<ToolTipCanvas>
{
public Canvas canvas;
public Tooltip toolTip;
public void Show(string content, string header=null,Sprite sprite=null)
{
canvas.enabled = true;
toolTip.content.gameObject.SetActive(true);
toolTip.content.text=content;
if(header!=null)
{
toolTip.header.gameObject.SetActive(true);
toolTip.header.text=header;
}
else
{
toolTip.header.gameObject.SetActive(false);
}
if(sprite!=null)
{
toolTip.image.gameObject.SetActive(true);
toolTip.image.sprite=sprite;
}
else
{
toolTip.image.gameObject.SetActive(false);
}
toolTip.ControlSizeAndPosition();
}
public void Hide()
{
canvas.enabled = false;
}
}
TooltipTriggter 放在ui物体上
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.EventSystems;
public class TooltipTriggter : MonoBehaviour, IPointerEnterHandler, IPointerExitHandler
{
public string header;
public string content;
public Sprite icon;
public float stayTime;//鼠标停留多久时间,显示消息
private bool isHovering = false;
Coroutine hoverRoutineCoroutine;
public void OnPointerEnter(PointerEventData eventData)
{
if(hoverRoutineCoroutine != null)
{
hoverRoutineCoroutine= StartCoroutine(HoverRoutineCoroutine());
}
else
{
StopAllCoroutines();
hoverRoutineCoroutine= StartCoroutine(HoverRoutineCoroutine());
}
}
public void OnPointerExit(PointerEventData eventData)
{
StopAllCoroutines();
ToolTipCanvas.instance.Hide();
}
private void OnDisable()
{
StopAllCoroutines();
ToolTipCanvas.instance.Hide();
}
private IEnumerator HoverRoutineCoroutine()
{
isHovering = true;
yield return new WaitForSeconds(stayTime);
if (isHovering)
{
ToolTipCanvas.instance.Show(content, header,icon);
}
}
}