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

相关推荐
叶帆10 天前
【YFIOs】用C#开发硬件之设备上云
开发语言·unity·c#
久数君10 天前
AI三维建模工具“造形家”:地理场景三维化的高效解决方案
unity·glb·ai算法·ai三维建模工具·地图框选·造形家·城市建筑模型
会思考的猴子10 天前
Unity VFX 属性 Postion 和 TargetPostion
unity
hai31524754310 天前
九章编程法 · 猜数字游戏 (GW-BASIC 重构版) *
人工智能·microsoft·游戏引擎·游戏程序
心前阳光10 天前
Unity资源导入之自动化资源导入
unity·自动化·游戏引擎
心前阳光10 天前
Unity之2021.3.45f2c1发布安卓程序遇到的问题
android·unity·游戏引擎
纪纯10 天前
PicoVR Unity Integration SDK 3.4 常用交互API
unity·游戏引擎·vr·pico
龙智DevSecOps解决方案11 天前
3A 游戏优化技术栈:如何打通引擎级分析工具与 DevOps 持续集成管线?
unity·性能优化·游戏开发·技术美术·perforce·unrealengine
葛兰岱尔11 天前
从 SolidWorks 到 Three.js,从 Inventor 到 Unity——制造业CAD模型“几何-语义一体化“转换,不再是天方夜谭!
开发语言·javascript·unity
鼎艺创新科技11 天前
三维电子沙盘中OSGB倾斜摄影数据的加载与渲染
游戏引擎·cocos2d