Unity UGUI 字体加粗特效

  • 1.前言
  • 2.优化(一)
  • 3.优化(二)

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;}}}

Unity UGUI 字体加粗特效相关推荐

  1. HTML怎么实现字体加粗

    HTML的加粗标签是<b>标签,是用来对你自定文字加粗,写法如下: 字体加粗:<b>这里是加粗的内容</b> 这样就可以实现加粗了! 转载于:https://www ...

  2. Android 设置TextView字体加粗

    今天,简单讲讲Android里如何设置TextView字体加粗. 不废话了,用过多次,还是没记住.直接上代码. 1.布局文件中这样设置即可: XML/HTML代码 android:textStyle= ...

  3. html 把文字显示控制,控制字体加粗显示的html标签是哪个

    控制字体加粗显示的html标签是哪个 发布时间:2021-06-09 09:27:30 来源:亿速云 阅读:88 作者:小新 这篇文章主要介绍了控制字体加粗显示的html标签是哪个,具有一定借鉴价值, ...

  4. android 字体加粗有阴影,Android TextView高级显示技巧实例小结

    本文实例总结了Android TextView高级显示技巧.分享给大家供大家参考,具体如下: 1. 自定义字体 可以使用setTypeface(Typeface)方法来设置文本框内文本的字体,而And ...

  5. Android字体加粗,UI小姐姐说太粗了,解决办法

    最近开发Android App,UI小姐姐验收的时候说,嗯,你这个字体没加粗呀 我说,好的,textStype="bold",然后屁颠屁颠的跑去给UI小姐姐看 结果UI小姐姐说,这 ...

  6. 字体加粗造成文字抖动

    解决思路:为文字预留宽度 <ul><li title="title 1">title 1</li><li title="titl ...

  7. 怎样在photoshop中把字体加粗并倒影

    怎样在photoshop中把字体加粗并倒影 用这个简单的办法可以: 1.选择字体右键消除锯齿锐化 + 仿粗体 (此步骤可选 1.字打好后,点"选择"--载入选区,点确定.这时字的边 ...

  8. HTML中如何将字体加粗-前端入门

    在HTML中要将字体加粗我们有两种方法,一种是利用b标签:另一种是利用strong标签,本篇文章我们就来介绍一下HTML中b标签和strong标签的用法. b标签和strong标签虽然都是可以让字体加 ...

  9. vb语言中怎样编码窗体中所有字体加粗_VBText控件中使字体加粗和倾斜的代码是什么...

    展开全部 VBText控件中使字体加粗和倾e69da5e6ba9062616964757a686964616f31333365656537斜的代码是: 加粗 Label1.FontBold = Tru ...

  10. Android-代码设置TextView字体加粗或者不加粗

    在最近的Android开发的需求当中,我遇到了特殊情况的字符串需要加粗.因为有字符串的区别,所以不能在XML里面单纯的实现控件的字体加粗,我们必须用代码来加粗. 注意:我使用的Kotlin代码 方式一 ...

最新文章

  1. 干货满满的 Python 实战项目,点赞收藏
  2. HTML5 LocalStorage 本地存储JSON数据
  3. cmake 常用命令
  4. vue项目token放在哪里_关于vue动态菜单的那点事
  5. SpringBoot | 第六章:常用注解介绍及简单使用
  6. nofollow标签_网站Nofollow标签的应用场景
  7. vue ---- vue简介
  8. python调用so库输出传入指针_python中使用ctypes调用so传参设置遇到的问题及解决方法...
  9. 手把手教你编写-微信机器人
  10. Windows 相关镜像及补丁下载地址
  11. JSP内置对象response常见用法
  12. 数据表与简单java类映射(角色权限)
  13. Windows10 笔记本电脑移动硬盘设备未就绪
  14. matlab atem(),非特定人的英文
  15. 【荐书】互联网电商系统的大数据方案书籍
  16. 【linux】一文总结linux的环境变量
  17. 2020年挂靠证书价格一览表哪个价格最高
  18. 什么叫做展望_什么叫做展望未来。。
  19. 图片处理Photoshop给广告模特专业润肤及磨皮
  20. ps学习笔记2(修复无损、水印等方法)

热门文章

  1. pdf文档页码怎么添加
  2. win10 快速截屏
  3. 愤怒的小鸟有PC版本了!
  4. 局域网简易聊天服务器&客户端
  5. Linux下更新Chrome和vscode
  6. 算法解析—同向双指针 字节笔试 万万没想到抓捕孔联顺,列表最大间隔不超过D
  7. vue项目使用mand mobile check选择项组点击选中,选中的列表延迟一位问题
  8. 推荐一款稳定快速免费的前端开源项目 CDN 加速服务
  9. 推荐的前端开源项目CDN加速服务
  10. 等待页面所有图片加载完毕