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

相关推荐
qq_428639616 分钟前
虚幻基础16:locomotion direction
游戏引擎·虚幻
Petrichorzncu17 小时前
Games104——网络游戏的进阶架构
游戏引擎
qq_4286396119 小时前
虚幻基础11:坐标计算&旋转计算
游戏引擎·虚幻
qq_4286396119 小时前
虚幻基础09:帧运算
游戏引擎·虚幻
学游戏开发的20 小时前
UE求职Demo开发日志#19 给物品找图标,实现装备增加属性,背包栏UI显示装备
c++·笔记·游戏引擎·unreal engine
土了个豆子的1 天前
unity中的动画混合树
unity·游戏引擎
学游戏开发的1 天前
UE学习日志#19 C++笔记#5 基础复习5 引用1
c++·笔记·学习·游戏引擎·unreal engine
奔跑的犀牛先生1 天前
unity学习26:用Input接口去监测: 鼠标,键盘,虚拟轴,虚拟按键
unity
Dr.勿忘2 天前
C#面试常考随笔8:using关键字有哪些用法?
开发语言·unity·面试·c#·游戏引擎
存储服务专家StorageExpert2 天前
答疑解惑:如何监控EMC unity存储系统磁盘重构rebuild进度
运维·unity·存储维护·emc存储