UGUI中为Text增加下划线

本文介绍了一种在Unity中为Text组件添加动态删除线效果的方法。通过重写Text的OnPopulateMesh函数,获取文字顶点并绘制下划线,实现了根据文字内容动态变化的删除线效果。代码示例详细解释了如何找到文字底部的最小和最大位置,创建并绘制删除线。虽然此方法可能导致性能影响,但能提供更精确的删除线显示效果。

  今天遇到一个需要给UIText添加删除线的功能,以前博主一般是制作一个同样宽度的Image,盖在Text上,但是这次需要动态换行,所以就重写了Text的功能,具体代码如下。

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

public class DeleteLineText : Text
{
    public float distance = 4f;
    public float lineHeight = 4f;
    public Color underLineColor;
    private bool useUnderLineColor = true;

    private List<UIVertex> textVertArr = new List<UIVertex>();
    private int startIndex = 0;
    private int endIndex = 0;

    protected override void Start()
    {
        base.Start();
        underLineColor = Color.red;
        lineHeight = 5.0f;
        distance = -15.0f;
    }

    protected override void OnPopulateMesh(VertexHelper toFill)
    {
        base.OnPopulateMesh(toFill); //渲染text字体的顶点
        toFill.GetUIVertexStream(textVertArr); //toFill创建vertex流  vertex数组的长度是文字长度的6倍 因为一个文字有六个顶点
        if (textVertArr.Count <= 0) return;
        SetUnderLine(toFill);
    }

   
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值