Unity技巧:轻松实现鼠标悬停文本时的动态变色效果

文章目录


前言

在游戏或应用中,给用户的界面添加一些小的互动效果能让它们更加吸引人。比如,当策划要求你这样做的时候 ,当用户将鼠标悬停在文字上时,文字颜色改变,这样的效果会让界面看起来更有趣。本文将教你如何在Unity中实现这个效果,将写好的脚本挂载到按钮上即可。


一、Text

csharp 复制代码
using UnityEngine;
using UnityEngine.UI;
using UnityEngine.EventSystems;

public class TextColorChange : MonoBehaviour, IPointerEnterHandler, IPointerExitHandler
{
    public Color normalColor = Color.blue; // 默认颜色
    public Color hoverColor = Color.white; // 悬停时颜色

    private Text textComponent;

    private void Start()
    {
        textComponent = GetComponentInChildren<Text>();
        if (textComponent == null)
        {
            Debug.LogError("没有找到Text组件,请确保文本对象是Button的子对象,并且拥有Text组件。");
        }
        else
        {
            textComponent.color = normalColor;
        }
    }

    public void OnPointerEnter(PointerEventData eventData)
    {
        if (textComponent != null)
        {
            textComponent.color = hoverColor; // 悬浮时将字体颜色改为悬停颜色
        }
    }

    public void OnPointerExit(PointerEventData eventData)
    {
        if (textComponent != null)
        {
            textComponent.color = normalColor; // 离开时将字体颜色还原为默认颜色
        }
    }
}

二、TMP_Text

csharp 复制代码
using UnityEngine;
using TMPro;
using UnityEngine.EventSystems;

public class TMPTextColorChange : MonoBehaviour, IPointerEnterHandler, IPointerExitHandler
{
    public Color normalColor = Color.blue; // 默认颜色
    public Color hoverColor = Color.white; // 悬停时颜色

    private TMP_Text textMeshPro;

    private void Start()
    {
        textMeshPro = GetComponentInChildren<TMP_Text>();
        if (textMeshPro == null)
        {
            Debug.LogError("没有找到TMP_Text组件,请确保文本对象是Button的子对象,并且拥有TMP_Text组件。");
        }
        else
        {
            textMeshPro.color = normalColor;
        }
    }

    public void OnPointerEnter(PointerEventData eventData)
    {
        if (textMeshPro != null)
        {
            textMeshPro.color = hoverColor; // 悬浮时将字体颜色改为悬停颜色
        }
    }

    public void OnPointerExit(PointerEventData eventData)
    {
        if (textMeshPro != null)
        {
            textMeshPro.color = normalColor; // 离开时将字体颜色还原为默认颜色
        }
    }
}

二、颜色转换

如果要使用配置加载,或者用类似#xxxxx的颜色格式进行配置的话,使用以下逻辑封装成适合的方法。

csharp 复制代码
Color sample;
ColorUtility.TryParseHtmlString("#3790E7", out sample);
textMeshPro.color = sample;

Text.color = ColorUtility.TryParseHtmlString("#3790E7", out var color) ? color : Color.blue;

总结

使用这些脚本,你可以轻松地在Unity中实现鼠标悬停时改变文字颜色的效果。

相关推荐
ttod_qzstudio4 小时前
Unity中使用EzySlice实现模型切割与UV控制完全指南
unity
南無忘码至尊5 小时前
Unity 实现与 Ollama API 交互的实时流式响应处理
unity·游戏引擎·交互
平行云8 小时前
如何实现UE程序大并发多集群的像素流部署
unity·ue5·图形渲染
mit6.82421 小时前
[shad-PS4] Vulkan渲染器 | 着色器_重新编译器 | SPIR-V 格式
c++·游戏引擎·ps4
向宇it1 天前
【unity小技巧】在 Unity 中将 2D 精灵添加到 3D 游戏中,并实现阴影投射效果,实现类《八分旅人》《饥荒》等等的2.5D游戏效果
游戏·3d·unity·编辑器·游戏引擎·材质
向宇it1 天前
Unity Universal Render Pipeline/Lit光照材质介绍
游戏·unity·c#·游戏引擎·材质
__water2 天前
RHA《Unity兼容AndroidStudio打Apk包》
android·unity·jdk·游戏引擎·sdk·打包·androidstudio
不爱说话的采儿2 天前
UE5详细保姆教程(第四章)
笔记·ue5·游戏引擎·课程设计
温玉琳琅2 天前
【UE5】虚幻引擎小百科
ue5·游戏引擎·虚幻
两水先木示2 天前
【Unity3D】微信小游戏适配安全区域或胶囊控件(圆圈按钮)水平高度一致方案
unity·微信小游戏·安全区域·ui适配·胶囊控件·safearea