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中实现鼠标悬停时改变文字颜色的效果。

相关推荐
向宇it11 分钟前
【unity组件介绍】URP Decal Projector贴花投影器,将特定材质(贴花)投影到场景中的其他对象上。
游戏·3d·unity·c#·游戏引擎·材质
快乐觉主吖11 小时前
Unity网络通信的插件分享,及TCP粘包分包问题处理
tcp/ip·unity·游戏引擎
erxij2 天前
【游戏引擎之路】登神长阶(十八):3天制作Galgame引擎《Galplayer》——无敌之道心
游戏引擎
lizz312 天前
GAMES101 lec2-数学基础1(线性代数)
线性代数·游戏引擎·图形渲染
啊基米德2 天前
lua(xlua)基础知识点记录一
unity·lua·xlua
夜色。2 天前
Unity Android Logcat插件 输出日志中文乱码解决
android·unity
X-mj2 天前
Unity URP + XR 自定义 Skybox 在真机变黑问题全解析与解决方案(支持 Pico、Quest 等一体机)
unity·游戏引擎·xr
erxij2 天前
【游戏引擎之路】登神长阶(十七):Humanoid动画——长风破浪会有时,直挂云帆济沧海
游戏引擎
erxij2 天前
【游戏引擎之路】登神长阶(十九):3D物理引擎——岁不寒,无以知松柏;事不难,无以知君子
3d·游戏引擎
心疼你的一切2 天前
Unity 多人游戏框架学习系列一
学习·游戏·unity·c#·游戏引擎