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

相关推荐
SlowFeather17 分钟前
Unity TMP可控角度多色渐变文字
unity·游戏引擎
霜绛6 小时前
Unity:UGUI笔记(一)——三大基础控件、组合控件
笔记·学习·unity·游戏引擎
小趴菜822712 小时前
Android中加载unity aar包实现方案
android·unity·游戏引擎
今夕资源网20 小时前
牛童三国单机游戏Unity源码 免费开源
游戏·unity·单机游戏·游戏源码·unity源码·unity游戏
future_studio1 天前
聊聊 Unity(小白专享、C# 小程序 之 图片播放器)
unity·小程序·c#
ellis19702 天前
toLua[七] Examples 06_LuaCoroutine2分析
unity
L X..2 天前
Unity 光照贴图异常修复笔记
unity·c#·游戏引擎
Xeon_CC2 天前
打开多个Unity编辑器时使用Visual Studio调试,弹出选择Unity实例窗口,但是没有实例
unity·编辑器·visual studio·调试·unity 调试
小L~~~2 天前
2025吉比特-游戏引擎开发-一面复盘
数据结构·算法·游戏引擎
云卓SKYDROID2 天前
无人机中继器模式技术对比
人工智能·游戏引擎·php·无人机·cocos2d·高科技·云卓科技