Unity 中消息提醒框

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);
        }
    }
}

相关推荐
寂寞的船1 天前
【Unity知识速递】第13期 Unity 内存泄漏排查实战:从 Memory Profiler 到 Lua 引用泄漏的生产级全链路诊断
unity·junit·c#·游戏引擎·lua
寂寞的船2 天前
【Unity Bug挑战】第12期 Awaitable 二次 await 死锁之谜 —— 为什么同一个异步操作等两次就卡死?
unity·c#·游戏引擎·bug
探求虚无3 天前
用 AI 在 Unity 里做一款 RTS:最难的不是生成代码,而是证明它真的能玩
人工智能·unity·游戏引擎
17岁的勇气4 天前
Unity Shader unity文档学习笔记(二十二):shader变体
笔记·学习·unity
寂寞的船5 天前
【Unity知识速递】第10期 Unity GC 零分配实战:从 UnityEngine.Pool 到手动 GC 控制的全链路优化
jvm·unity·c#·游戏引擎·lucene
Martina_03216 天前
AI生成3D场景的霓虹灯一开就糊成光团?用6步检查Emissive、Bloom与曝光
人工智能·3d·ai·自然语言处理·游戏引擎·材质·贴图
寂寞的船7 天前
【Unity知识速递】第8期 IL2CPP 托管堆内存碎片与原生堆交互机制深度解析
unity·游戏引擎·交互
寂寞的船8 天前
【Unity知识速递】第5期 Unity 6.2 深度解析:AI Assistant 登场,World Space UI 与 Sentis 全面升级
人工智能·ui·unity
寂寞的船8 天前
【Unity知识速递】第4期 MCP for Unity:用 AI 自然语言控制编辑器
人工智能·unity·编辑器
淡海水8 天前
09-04-内存-Unity原生对象生命周期
unity·c#·游戏引擎·gc