1.前言
在项目组无可厚非会在一些描述的文本中加入粗体,比如标题或者是重要文字,然而Unity本身UGUI提供的Text的Bold属性在某些字体达到的效果并不尽人意,可以先看下原本Unity的效果:
2.优化(一)
原本的效果肯定是不满足美术需求的,我们需要通过字体渲染方面重新实现字体加粗效果,在本文中核心算法其实就是将文本重复绘制,也就是在同样的位置绘制某个字符网格多次,可以近似实现这个字符的加粗效果。
using UnityEngine;
using System.Collections.Generic;
using UnityEngine.UI;
[RequireComponent(typeof(Text))]
public class BoldTextEffect : BaseMeshEffect
{
[Range(0, 1)] public float Alpha;
[Range(1, 5)] public int Strength;
private Text m_Text = null;
private Text TextComp
{
get
{
if (m_Text == null)
{
m_Text = GetComponent<Text>();
}
return m_Text;
}
}
private Color effectColor
{
get
{
if (TextComp == null)
{
return Color.black;
}
return TextComp.color;
}
}
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;
color32.a = (byte) ((int) color32.a * (int) verts[index].color.a / (int) byte.MaxValue);
color32.a = (byte)(Alpha * color32.a);
vert.color = color32;
verts[index] = vert;
}
}
public override void ModifyMesh(VertexHelper vh)
{
if (!IsActive())
{
return;
}
List<UIVertex> verts = new List<UIVertex>();
vh.GetUIVertexStream(verts);
for (int i = 0; i < Strength; ++i)
{
ApplyShadowZeroAlloc(verts, effectColor, 0, verts.Count, 0, 0);
}
vh.Clear();
vh.AddUIVertexTriangleStream(verts);
}
}
在上面这个组件中暴露了两个可供美术调节的参数:Alpha,Strength
- Alpha : 重复渲染字体网格的透明度,用于调整加粗的硬度
- Strength: 重复渲染字体网格的次数,用于调整加粗的强度
使用方法,只需要将这个组件挂载到Text物体上,调整上面两个参数直至效果满意为止,下面是示例图:
3.优化(二)
现在实现了控件级的字体加粗,我们还需要实现RichText的<b>标签,也就是灵活的控制Text中某些字体的粗细效果。在这里实现的思路就是,通过记录<b>标签的开始和结束为止,然后只在其区间内的字体网格进行绘制。
using UnityEngine;
using System.Collections.Generic;
using System.Text.RegularExpressions;
using UnityEngine.UI;
[RequireComponent(typeof(Text))]
public class BoldTextEffect : BaseMeshEffect
{
[Range(0, 1)] public float Alpha;
[Range(1, 5)] public int Strength;
public string RichText = "";
private Text m_Text = null;
private Text TextComp
{
get
{
if (m_Text == null)
{
m_Text = GetComponent<Text>();
}
return m_Text;
}
}
private Color effectColor
{
get
{
if (TextComp == null)
{
return Color.black;
}
return TextComp.color;
}
}
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;
color32.a = (byte) ((int) color32.a * (int) verts[index].color.a / (int) byte.MaxValue);
color32.a = (byte)(Alpha * color32.a);
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);
if (!string.IsNullOrEmpty(RichText) && begin != null && end != null)
{
int offset = 0;
for (int i = 0; i < begin.Count && i < end.Count; ++i)
{
for (int j = 0; j < Strength; ++j)
{
ApplyShadowZeroAlloc(verts, effectColor, (begin[i].Index - offset) * 6, (end[i].Index - offset - 3) * 6, 0, 0);
}
offset += 7;
}
}
else
{
for (int i = 0; i < Strength; ++i)
{
ApplyShadowZeroAlloc(verts, effectColor, 0, verts.Count, 0, 0);
}
}
vh.Clear();
vh.AddUIVertexTriangleStream(verts);
}
public void SetText(string text)
{
this.RichText = text;
begin = s_BoldBeginRegex.Matches(RichText);
end = s_BoldEndRegex.Matches(RichText);
text = text.Replace("<b>", "");
text = text.Replace("</b>", "");
if (m_Text != null)
{
m_Text.text = text;
}
}
}