当前位置:网站首页>(十三)文本渲染Text
(十三)文本渲染Text
2022-06-12 08:53:00 【81192_csdn】
1.前言
unity的文本渲染通过Text组件实现,文字的渲染也离不开贴图、mesh以及材质。材质则是根据MaskableGraphic材质,所以了解Text的主线则是了解如何生成贴图和mesh即可。本文不会去讨论文字贴图和mesh产生的技术细节,只讨论如何利用unity现有的组件去获取贴图和mesh。
2.逻辑梳理
基本逻辑如下:
1)当需要更新时(比如更新文字大小时)标记更新材质和mesh
2)Canvas处于preRender时通过Font类获取贴图。
3)Canvas处于preRender时通过TextGenerator获取Mesh信息
3.贴图获取
贴图获取是通过字体获取,即(font.material.mainTexture)如下所示:
public override Texture mainTexture
{
get
{
if (font != null && font.material != null && font.material.mainTexture != null)
return font.material.mainTexture;
if (m_Material != null)
return m_Material.mainTexture;
return base.mainTexture;
}
}
Font字体类存储了字体的相关信息,如果项目种中使用了多个字体,则每个字体对应的Text列表则是通过FontUpdateTracker维护的,并且当字体贴图更改时(比如添加新字符时),通过FontTextureChanged通知Text去更新mesh以及贴图(如果cavas处于rebuilding中则只更新mesh)。
4.获取Mesh
mesh的获取是通过TextGenerator,根据字体的设置,在OnPopulateMesh中去更新网格。如下所示:其中verts 还包含了uv信息。
// Apply the offset to the vertices
IList<UIVertex> verts = cachedTextGenerator.verts;
float unitsPerPixel = 1 / pixelsPerUnit;
//Last 4 verts are always a new line... (\n)
int vertCount = verts.Count - 4;
在获取顶点数据前先通过PopulateWithErrors传入设置、文本以及本游戏物体来生成相关数据。
Vector2 extents = rectTransform.rect.size;
var settings = GetGenerationSettings(extents);
cachedTextGenerator.PopulateWithErrors(text, settings, gameObject);
TextGenerator在InputField中使用比较多,可以通过它获取更多信息。
5.结语
上述时Text相关实现的基本逻辑,Text组件还需要进行一些特殊情况处理。下面附几篇文字渲染相关的文章:
附录一 OpenGL实现文字渲染
附录二 Unity字体渲染
边栏推荐
- Method to limit the input box to only numbers
- When the uniapp page jumps with complex data parameters.
- Background location case 1
- [character set 7] what are the wide character codes and multi byte codes of Chinese characters
- Audio and video related links
- Loading circling effect during loading
- (p21-p24) unified data initialization method: List initialization, initializing objects of non aggregate type with initialization list, initializer_ Use of Lisy template class
- The newline character with in the string is converted to an array
- QT realizes multi screen and multi-resolution adaptation
- Shell basic syntax -- arithmetic operation
猜你喜欢

43 cas d'analyse du réseau neuronal MATLAB: chapitre 7 régression du réseau RBF - - réalisation de la régression fonctionnelle non linéaire

Engineers learn music theory (I) try to understand music

【指针进阶三】实现C语言快排函数qsort&回调函数

2022 safety officer-c certificate special operation certificate examination question bank and simulation examination

Inheritance of row height
![[character set 9] will GBK be garbled when copied to unicode?](/img/dc/c9ec4a90355d30479f23fdead4b349.png)
[character set 9] will GBK be garbled when copied to unicode?

《MATLAB 神经网络43个案例分析》:第7章 RBF网络的回归--非线性函数回归的实现

Build personal blog and web.

Production scheduling status of manufacturing enterprises and solutions of APS system

Background position position NOUN
随机推荐
Application method of new version UI of idea + use method of non test qualification and related introduction
《MATLAB 神经网络43个案例分析》:第7章 RBF网络的回归--非线性函数回归的实现
Error: ER_ NOT_ SUPPORTED_ AUTH_ MODE: Client does not support authentication protocol requested ... ...
Code generation tool Autocode for XML Publishing
Union selector
Background position - exact units
Composition of box model
动态创建表单并提交
IP, DNS, domain name, URL, hosts
(P13) use of final keyword
RuntimeError:Input and parameter tensors are not at the same device, found input tensor at cuda:0 an
Is it really expensive for enterprises to launch MES software?
About weights exercise
Install iptables services and open ports
第八章-数据处理的两个基本问题
Does database and table splitting cause reading diffusion problems? How to solve it?
The difference between deep copy and shallow copy
(p21-p24) unified data initialization method: List initialization, initializing objects of non aggregate type with initialization list, initializer_ Use of Lisy template class
Handling abnormal data
动态线段树leetcode.699