UI 元素和文本网格的文本可以包含多种字体样式和大小。Text、GUIStyle 和 TextMesh 类都具有 Rich Text 设置,此设置会指示 Unity 在文本中查找标记标签。Debug.Log 函数也可使用这些标记标签来增强代码中的错误报告。标签不会显示,但会指示要应用于文本的样式更改。
该标记系统受 HTML 的启发,但并不打算与标准 HTML 严格兼容。基本思想是可以将文本的一部分包含在一对匹配标签内:
We are <b>not</b> amused.
如示例所示,标签就是“尖括号”字符 <
和 >
内的文本片段。
_开始_标签放在这部分的开头。标签内的文本表示标签的名称(在本示例中就是 b)。
另一个标签放在这部分的结尾。这个标签就是_结束_标签。它具有与开始标签相同的名称,但是名称前面带有斜杠 /
字符。每个开始标签必须有一个对应的结束标签。如果开始标签没有对应的_结束_标签,则会呈现为常规文本。
标签不会直接显示给用户,而是用于说明其包裹的文本的样式。上例中使用的 b
标签将粗体应用于单词“not”,因此文本在屏幕上显示为:
We are not amused
标记的文本部分(包括将其包裹的标签)称为元素。
通过将一个元素“嵌套”在另一个元素中,可以将多个样式应用于文本的一部分
We are <b><i>definitely not</i></b> amused
<i>
标签表示应用斜体样式,因此将在屏幕上显示为
We are definitely not amused
请注意结束标签的顺序与起始标签的顺序相反。为了更清楚说明原因,现在让内部标签不必跨越最外层元素的整个文本
We are <b>absolutely <i>definitely</i> not</b> amused
得到的结果为
We are absolutely definitely not amused
有些标签对文本具有简单的“全有或全无”(all-or-nothing) 效果,但其他标签可能允许变化。例如,color 标签需要知道要应用的颜色。应使用参数将此类信息添加到标签中:
We are <color=green>green</color> with envy
生成的结果如下:
请注意,结束标签不包含参数值。可选择将值用引号引起来,但这不是必需的。
标签参数不能包含空格。例如:
We are <color = green>green</color> with envy
由于 =
字符两侧的空格而无效。
以下列表描述了 Unity 支持的所有样式标签。
标签 | 描述 | 示例 | 注意事项 |
---|---|---|---|
b | 以粗体显示文本。 | We are <b>not</b> amused. |
|
i | 以斜体显示文本。 | We are <i>usually</i> not amused. |
|
size | 根据参数值设置文本的大小(以像素为单位)。 | We are <size=50>largely</size> unaffected. |
尽管此标签可用于 Debug.Log,但如果大小设置得太大,您会发现窗口栏和控制台中的行间距看起来很奇怪。 |
color | 根据参数值设置文本的颜色。可使用传统的 HTML 格式指定颜色。#rrggbbaa …其中的字母对应于十六进制数字对,表示颜色的红、绿、蓝和 Alpha(透明度)值。例如,完全不透明的青色将指定为 color=#00ffffff …可通过大写或小写形式指定十六进制值; #FF0000 等效于 #ff0000 。 |
We are <color=#ff0000ff>colorfully</color> amused |
另一种选择是使用颜色的名称。这种方法更容易理解,但当然,颜色范围受限,并始终假设颜色完全不透明。<color=cyan>some text</color> 下表中列出了可用的颜色名称。 |
material | 这仅对文本网格有用,使用参数指定的材质渲染文本部分。值为 Inspector 显示的文本网格材质数组的索引。 | We are <material=2>texturally</material> amused |
|
quad | 这仅对文本网格有用,渲染与文本内联的图像。采用指定图像材质的参数、图像高度参数(以像素为单位)以及另外四个表示待显示图像的矩形区域的参数。与其他标签不同,quad 不会包裹一段文本,因此没有结束标签;斜杠字符放在初始标签的末尾,表示“自动关闭”。 | <quad material=1 size=20 x=0.1 y=0.1 width=0.5 height=0.5> |
这将选择渲染器材质数组中位置的材质,并将图像的高度设置为 20 像素。图像的矩形区域由 x、y、宽度和高度值决定,这些值全部表示为纹理的未缩放宽度和高度的一定比例。 |
下表列出了可以在 <color>
富文本标签中使用名称而不是十六进制值的颜色。
颜色名称 | 十六进制值 | 样本 |
---|---|---|
aqua(等同于 cyan) | #00ffffff |
![]() |
black | #000000ff |
![]() |
blue | #0000ffff |
![]() |
brown | #a52a2aff |
![]() |
cyan(等同于 aqua) | #00ffffff |
![]() |
darkblue | #0000a0ff |
![]() |
fuchsia(等同于 magenta) | #ff00ffff |
![]() |
green | #008000ff |
![]() |
grey | #808080ff |
![]() |
lightblue | #add8e6ff |
![]() |
lime | #00ff00ff |
![]() |
magenta(等同于 fuchsia) | #ff00ffff |
![]() |
maroon | #800000ff |
![]() |
navy | #000080ff |
![]() |
olive | #808000ff |
![]() |
orange | #ffa500ff |
![]() |
purple | #800080ff |
![]() |
red | #ff0000ff |
![]() |
silver | #c0c0c0ff |
![]() |
teal | #008080ff |
![]() |
white | #ffffffff |
![]() |
yellow | #ffff00ff |
![]() |
默认情况下,Editor GUI 系统中已禁用富文本,但可以使用自定义 GUIStyle 显式启用富文本。应将 richText
属性设置为 true,并将样式传递给相关的 GUI 函数:
GUIStyle style = new GUIStyle ();
style.richText = true;
GUILayout.Label("<size=30>Some <color=yellow>RICH</color> text</size>",style);
Did you find this page useful? Please give it a rating:
Thanks for rating this page!
What kind of problem would you like to report?
Thanks for letting us know! This page has been marked for review based on your feedback.
If you have time, you can provide more information to help us fix the problem faster.
Provide more information
You've told us this page needs code samples. If you'd like to help us further, you could provide a code sample, or tell us about what kind of code sample you'd like to see:
You've told us there are code samples on this page which don't work. If you know how to fix it, or have something better we could use instead, please let us know:
You've told us there is information missing from this page. Please tell us more about what's missing:
You've told us there is incorrect information on this page. If you know what we should change to make it correct, please tell us:
You've told us this page has unclear or confusing information. Please tell us more about what you found unclear or confusing, or let us know how we could make it clearer:
You've told us there is a spelling or grammar error on this page. Please tell us what's wrong:
You've told us this page has a problem. Please tell us more about what's wrong:
Thank you for helping to make the Unity documentation better!
Your feedback has been submitted as a ticket for our documentation team to review.
We are not able to reply to every ticket submitted.