Unity3d UGUI Text 增加下划线

本文介绍了如何在Unity3d的UGUI Text组件中添加下划线,提供了一个简单易用的方法,该方法支持单行或多行文本,并能根据Text内容自动刷新。相较于使用"_"字符实现,此方案利用Image组件避免了非等宽字符下划线长度不匹配的问题。附带了参考链接和Demo下载地址,欢迎交流改进。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

1.简单易用,只需要添加到带有Text脚本的GameObject对象即可
2.支持单行或者多行
3.可以根据Text文字内容改变而自动刷新
4. 效果图如下:
多行展示效果 单行显示效果

using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.EventSystems;
using UnityEngine.UI;

class UnderlineProperty
{
    public Color _color;
    public Vector3 _position;
    public float _width;
    public float _height;
    public Vector2 _privot;
}

public class MultipleLinkButton : MonoBehaviour, IPointerDownHandler, IPointerUpHandler
{
    private Text _text;
    private int _curCharacterCount = 0;
    private List<Image> _lines = new List<Image>();
    private System.Action _clickEvent = null; //下划线点击事件
    private bool _isInitUnderline = false;

    public System.Action ClickEvent
    {
        get
        {
            return _clickEvent;
        }

        set
        {
            _clickEvent = value;
        }
    }

    // Use this for initialization
    void Start () {
        _text = transform.GetComponent<Text>();
        _text.gameObject.AddComponent<Button>().onClick.AddListener(()=> {
            if (ClickEvent != null)
                ClickEvent();
        });
    }

    void Update()
    {
        //做初始化
        if (_text.cachedTextGenerator.lineCount 
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值