Whether you’re working on an FPS, a puzzle game or a VR experience, one of the aspects you have to take care of is User Interface. Luckily enough, as of March 2017, TextMesh Pro has joined the Unity family. This means that making your UI in Unity look great has become much easier and faster!
无论您是从事FPS,益智游戏还是VR体验,用户界面都是您需要考虑的方面之一。 幸运的是,截至2017年3月, TextMesh Pro已加入Unity系列 。 这意味着使您的Unity中的UI看起来更加美观和便捷!
For those of you who haven’t followed these developments, TextMesh Pro is a replacement for Unity’s default text components. It is just as performant (even more, in some cases) and it uses a completely different rendering technique called Signed Distance Field (SDF), originally used by Valve in Team Fortress 2. Together with having the power to make your text look great without much effort, TextMesh Pro also provides you with a much more advanced control over it, via inspector or scripting. In this post, we’re going to take a look at how to make the most out of this incredible tool!
对于尚未遵循这些开发方法的人,TextMesh Pro可以替代Unity的默认文本组件。 它与性能一样(在某些情况下甚至更多),它使用了一种完全不同的渲染技术,称为Signed Distance Field (SDF),最初由Team Fortress 2中的Valve使用。此外,它还具有使文本看起来很棒而没有的功能。通过大量的努力,TextMesh Pro还可以通过检查器或脚本为您提供更高级的控制。 在本文中,我们将探讨如何充分利用这个令人难以置信的工具!
There are two main reasons why it is a great idea to start your projects using TextMesh Pro. First, visual improvements. Thanks to SDF rendering, it’s easy to change the way your text looks without having to recreate its font. Every TextMesh Pro component has a material attached to it that you can tweak in order to modify the style of your text. Second, better control. The TextMesh Pro component includes all variables you can find in the ordinary text component, plus a lot more. And if this wasn’t enough, just know that TextMesh Pro is currently used by over a quarter of a million developers around the world!
使用TextMesh Pro启动项目是一个好主意的主要原因有两个。 首先,视觉上的改进。 借助SDF渲染,可以轻松更改文本的外观,而无需重新创建其字体。 每个TextMesh Pro组件都有一个附加的材质,您可以对其进行调整,以修改文本的样式。 第二,更好的控制。 TextMesh Pro组件包括您可以在普通文本组件中找到的所有变量,以及更多其他内容。 如果这还不够的话,那就知道全世界有超过四分之一的开发人员正在使用TextMesh Pro!
TextMesh Pro与默认文本 (TextMesh Pro vs default Text)
Working with the default Unity text, you might have noticed that sometimes stretching or resizing the object causes it to look blurry. This is because the text doesn’t hold information of what it would look like when resized, and Unity therefore has to “improvise” and attempt to generate the missing pixels on the fly. Because of the different rendering technique that TextMesh Pro is using, this is no longer an issue. SDF is based on the principle of rendering a Font Atlas at a high resolution so that the font always has information about what a character would look like when resized.
使用默认的Unity文本时,您可能已经注意到有时拉伸或调整对象大小会导致其看起来模糊。 这是因为文本无法容纳调整大小后的外观信息,因此Unity必须“改进”并尝试动态生成丢失的像素。 由于TextMesh Pro使用的渲染技术不同,因此这不再是问题。 SDF基于高分辨率渲染Font Atlas的原理,因此字体始终具有有关调整大小后的字符外观的信息。
With TextMesh Pro you can import any font file and create your own font asset (Window > TextMesh Pro > Font Asset Creator). This allows you to choose the resolution for its Font Atlas (which will determine how effective SDF rendering will be for your text). Obviously, the lower the resolution you choose, the faster the Font Atlas will be generated.
使用TextMesh Pro,您可以导入任何字体文件并创建自己的字体资产(“ 窗口”>“ TextMesh Pro”>“字体资产创建器” )。 这使您可以为其Font Atlas选择字体的分辨率(这将确定SDF渲染文本的效果)。 显然,选择的分辨率越低,生成字体图集的速度就越快。
Since the Font has information about how it would look like at different sizes, it can also reconstruct its Outline and Dropped Shadow from the Font Atlas. Simply tweak the material properties and watch your text change its look entirely!
由于字体具有有关在不同大小下的外观的信息,因此它还可以从“字体集”中重建其轮廓和阴影。 只需调整材质属性,然后看着您的文字完全改变外观!
更好的控制 (Better control)
As we have seen, TextMesh Pro offers great looking text. But what’s the point of nice text if you have no control over it? The TextMesh Pro component has options that allow you to customize font size, spacing, alignment, kerning, or to enable Autosize and to fit your text into a Container. The last two, in particular, give you great control over working with different platforms or different languages, as they allow your text to autosize depending on a given text container without the need of any scripting. However, if you do wish to change these settings at runtime, you can access all variables in the TextMesh Pro components from the TextMeshPro API.
如我们所见,TextMesh Pro提供了美观的文本。 但是,如果您无法控制好文字,那又有什么意义呢? TextMesh Pro组件具有一些选项,可让您自定义字体大小,间距,对齐方式,字距调整或启用“自动调整大小”,并使文本适合“容器”。 特别是后两者,使您可以很好地控制使用不同平台或不同语言的情况,因为它们允许您根据给定的文本容器自动调整文本大小,而无需任何脚本。 但是,如果您确实希望在运行时更改这些设置,则可以从TextMeshPro API访问TextMesh Pro组件中的所有变量。
For an extra layer of customization, you can also add the Text Info Debug Tool component to your text object so that you can visually represent characters, words, links, lines etc.
对于额外的自定义层,您还可以将“ 文本信息调试工具”组件添加到文本对象,以便可以直观地表示字符,单词,链接,线条等。
If you want to save time reformatting your text every time you insert a header, a title, a quote etc., you can set up a Style Sheet for any specific purpose. One example could be to inject a decoration to a header. To create a Style Sheet, select Create > TextMesh Pro > Style Sheet. You can set this new asset as your default Style Sheet from Edit > Project Settings > TextMesh Pro Settings.
如果您想节省每次插入标题,标题,引号等时重新格式化文本的时间,则可以针对任何特定目的设置样式表。 一个示例可能是在标头上插入装饰。 要创建样式表,请选择创建> TextMesh Pro>样式表 。 您可以从“ 编辑”>“项目设置”>“ TextMesh Pro设置”将此新资产设置为默认样式表。
富文本 (Rich Text)
In addition to controlling the way the text looks from the inspector or a script, you can control it from the text field itself. If you are familiar with HTML or XML, you can customize the look of your text as you are typing. If you’re not, just read the guide on how to use Rich Text with TextMesh Pro) to get started! This is particularly useful in cases where you want to use multiple styles, sizes, materials in the same text object.
除了控制从检查器或脚本显示文本的方式之外,还可以从文本字段本身控制文本。 如果您熟悉HTML或XML,则可以在键入时自定义文本的外观。 如果您不是这样,则只需阅读有关如何将Rich Text与TextMesh Pro结合使用的指南,即可开始使用! 如果要在同一个文本对象中使用多种样式,大小和材料,此功能特别有用。
定制材料 (Customizing Materials)
Another way of making TextMesh Pro look great is to apply a Surface Shader to your text. This will allow lighting in the scene to affect the text. In the example below, a few real-time point lights are moving around the scene and affecting the text.
使TextMesh Pro看起来很棒的另一种方法是将Surface Shader应用于文本。 这将允许场景中的光照影响文本。 在下面的示例中,一些实时点光源在场景中移动并影响文本。
The material properties give you the option to customize settings like Face, Outline, Bevel, Lighting, BumpMap, EnvMap, Glow, and Debug settings.
材质属性使您可以选择自定义设置,例如“面”,“轮廓”,“斜角”,“照明”,“凹凸贴图”,“ EnvMap”,“发光”和“调试”设置。
Once you’ve created a material that you’re happy with, you can create a Material Preset that we will be able to reuse at any point, specifically for the Font Asset we’re using. We can do so by right-clicking on the Material name and selecting ‘Create Material Preset’. This will create an asset that we can select from our TextMesh Pro component > Font Settings > Material Preset.
一旦创建了您满意的素材,就可以创建一个素材预置 ,我们可以在任何时候重复使用它,特别是针对我们正在使用的字体资产。 我们可以通过右键单击“材料”名称并选择“创建材料预设”来实现。 这将创建一个资产,我们可以从TextMesh Pro组件>字体设置>材质预设中进行选择。
后备系统 (Fallback System)
If you have generated a Font Atlas that is missing certain characters, TextMesh Pro will make the character fall back to a default glyph whenever typed. You can change this glyph by going on Edit > Project Settings > TextMesh Pro Settings. Alternatively, you can set up one or more Font Assets to which TextMesh Pro will fall back to if the character is not found in the primary Atlas. For optimization purposes, it makes sense to keep the main Font Atlas at a higher resolution, and all the fallback Atlases to a lower one.
如果生成的字体地图集缺少某些字符,则每次键入时,TextMesh Pro都会使该字符退回到默认字形。 您可以通过以下方式更改此字形: 编辑>项目设置> TextMesh Pro设置。 或者,您可以设置一个或多个字体资产,如果在主图集中找不到该字符,则TextMesh Pro将退回到该字体资产。 出于优化目的,将主字体地图集保持在较高的分辨率,而将所有后备地图集保持在较低的分辨率是有意义的。
In addition to that, you can use the TextMesh Pro Settings to set up Resources paths for Font & Materials or Sprite Assets. When using Rich Text, you can insert a Sprite depending on which are available in the primary Sprite Asset you have defined in the Settings. The easiest way of doing so is by importing a Sprite Sheet, then slicing it in the Sprite Editor, right-clicking on the asset and selecting Create > TextMesh Pro > Sprite Asset. From this new asset, you can customize settings such as offset or pivot. Likewise, you can set up a series of Fallback Sprite Assets to fall back to if a Sprite is not found in the primary Sprite Assets.
除此之外,您可以使用TextMesh Pro设置来设置“字体和材质”或“精灵资源”的资源路径。 使用富文本格式时,您可以插入Sprite,具体取决于您在“设置”中定义的主要Sprite资产中的可用资源。 最简单的方法是导入一个Sprite Sheet,然后在Sprite Editor中对其进行切片,右键单击资产,然后选择Create> TextMesh Pro> Sprite Asset。 通过此新资产,您可以自定义设置,例如偏移或枢轴。 同样,如果在主Sprite资产中未找到Sprite,则可以设置一系列Fallback Sprite资产以回退。
加起来… (Summing up…)
In terms of performance, as we have already mentioned, TextMesh Pro works similarly to the default text: it still renders on quads, so it is as efficient as using a bitmap font. There is also no runtime memory allocation: TextMesh Pro will only allocate space for the text objects when you press play. If you’re increasing the number of characters by a significant amount, only one reallocation is made; if you are decreasing the number of characters, no reallocation is made, unless it’s decreased of at least 256 characters. And in terms of improvements, you can obtain better results for styles like Outline and Drop Shadow simply because of the SDF rendering technique used by TextMesh Pro.
在性能方面,正如我们已经提到的,TextMesh Pro的工作方式与默认文本类似:它仍在四边形上呈现,因此它与使用位图字体一样有效。 也没有运行时内存分配:当您按下play时,TextMesh Pro只会为文本对象分配空间。 如果要大量增加字符数,则仅进行一次重新分配;否则,将重新分配字符。 如果要减少字符数,则不会进行重新分配,除非减少了至少256个字符。 在改进方面,仅由于TextMesh Pro使用的SDF渲染技术,您就可以为“轮廓”和“阴影”等样式获得更好的结果。
Most of what was mentioned in this blog is available as an example scene when you import TextMesh Pro into your project (from Window > Package Manager > TextMesh Pro > Install from Unity 2018.1 onwards). I strongly recommend you to take a look at them before you start using TextMesh Pro, as their content might answer most of the questions you have. However, if you want to provide your feedback or still have any questions, you are welcome to do so via the Forums.
当您将TextMesh Pro导入项目时(从Window> Package Manager> TextMesh Pro>从Unity 2018.1开始安装 ),此博客中提到的大多数内容都可以作为示例场景使用。 我强烈建议您在开始使用TextMesh Pro之前先看一下它们,因为它们的内容可能会回答您遇到的大多数问题。 但是,如果您想提供反馈或仍然有任何疑问,欢迎通过论坛提供 。
翻译自: https://blogs.unity3d.com/2018/10/16/making-the-most-of-textmesh-pro-in-unity-2018/