UGUI 文本加粗

这是用的Aril字体,通常情况下,直接用<b>达不到这样粗的效果,所以需要重新写一个字体效果,类似描边的效果。

上面的图片就是效果和所设置的参数。

下面是代码,参考其他人的代码改编的

using UnityEngine;
using System.Collections.Generic;
using System.Text.RegularExpressions;
using UnityEngine.UI;
[RequireComponent(typeof(Text))]
public class BoldTextEffect : BaseMeshEffect
{
    [Range(1, 5)] public int Strength = 5;
    public int startBIndex = 0;
    public int endBIndex = 999;

    private Text m_Text = null;

    private Text TextComp
    {
        get
        {
            if (m_Text == null)
            {
                m_Text = GetComponent<Text>();
            }

            return m_Text;
        }
    }

    public Color effectColor = Color.white;

    protected void ApplyShadowZeroAlloc(List<UIVertex> verts, Color32 color, int start, int end, float x, float y)
    {
        int num = verts.Count + end - start;
        if (verts.Capacity < num)
            verts.Capacity = num;
        for (int index = start; index < end; ++index)
        {
            UIVertex vert = verts[index];
            verts.Add(vert);
            Vector3 position = vert.position;
            position.x += x;
            position.y += y;
            vert.position = position;
            Color32 color32 = color;
            vert.color = color32;
            verts[index] = vert;
        }
    }

    private static readonly Regex s_BoldBeginRegex = new Regex("<b>", RegexOptions.Singleline);
    private static readonly Regex s_BoldEndRegex = new Regex("</b>", RegexOptions.Singleline);

    private MatchCollection begin = null;
    private MatchCollection end = null;


    public override void ModifyMesh(VertexHelper vh)
    {
        if (!IsActive())
        {
            return;
        }

        List<UIVertex> verts = new List<UIVertex>();
        vh.GetUIVertexStream(verts);

        begin = s_BoldBeginRegex.Matches(TextComp.text);
        end = s_BoldEndRegex.Matches(TextComp.text);

        if (begin != null && end != null)
        {
            int i = startBIndex;
            if (i < 0) i = 0;
            for (; i < endBIndex && i < begin.Count && i < end.Count; ++i)
            {
                ApplyShadowZeroAlloc(verts, effectColor, begin[i].Index * 6, end[i].Index * 6, 1, 0f);
                for (int j = 0; j < Strength; ++j)
                {
                    ApplyShadowZeroAlloc(verts, effectColor, begin[i].Index * 6, end[i].Index * 6, -1, 0f);
                }
            }
        }

        vh.Clear();
        vh.AddUIVertexTriangleStream(verts);
    }

}

 

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值