Unity2017.1官方UGUI文档翻译——Rich Text
Rich Text
The text for UI elements and text meshes can incorporate multiple font styles and sizes. Rich text is supported both for the UI System and the legacy GUI system. The Text, GUIStyle, GUIText and TextMesh classes have a Rich Text setting which instructs Unity to look for markup tags within the text. The Debug.Log function can also use these markup tags to enhance error reports from code. The tags are not displayed but indicate style changes to be applied to the text.
UI元素和文本网格的文本可以包含多种字体样式和大小。富文本同时支持UI系统和传统的GUI系统。 Text,GUIStyle,GUIText和TextMesh类具有富文本设置,指示Unity在文本内查找标记标签。 Debug.Log函数还可以使用这些标记标签来增强代码中的错误报告(比如显示为红色)。 标签不显示,但表明了将应用于文本的样式更改。
Markup format
标记格式
The markup system is inspired by HTML but isn’t intended to be strictly compatible with standard HTML. The basic idea is that a section of text can be enclosed inside a pair of matching tags:-
标记系统受HTML的启发,但不打算与标准HTML严格兼容。 其基本思想是可以在一对匹配的标签中包含一段文本:
We are <b>not</b> amused
As the example shows, the tags are just pieces of text inside the “angle bracket” characters, < and >. The text inside the tag denotes its name (which in this case is just b). Note that the tag at the end of the section has the same name as the one at the start but with the slash / character added. The tags are not displayed to the user directly but are interpreted as instructions for styling the text they enclose. The b tag used in the example above applies boldface to the word “not”, so the text will appear onscreen as:-
如示例所示,标签只是“尖括号”字符<和>中的文本片段。 标签内的文字表示其名称(在这个例子中是b)。 注意,这一段末尾的标签与起始标签名称相同,但添加了斜杠/字符。 这些标签不直接显示给用户,而是被解释为指导他们所包括的文本的样式。 上例中使用的b标签将粗体字应用于单词“not”,因此文本将在屏幕上显示为:
We are not amused
A marked up section of text (including the tags that enclose it) is referred to as an element.
被标记的一段文本(包括外面的标签)被称为元素。
Nested elements
嵌套元素
It is possible to apply more than one style to a section of text by “nesting” one element inside another
通过将一个元素“嵌套”到另一个元素中,可以将多个样式应用于一段文本
We are <b><i>definitely not</i></b> amused
The i tag applies italic style, so this would be presented onscreen as
i标签应用斜体样式,所以在屏幕上将显示
We are definitely not amused
Note the ordering of the ending tags, which is in reverse to that of the starting tags. The reason for this is perhaps clearer when you consider that the inner tags need not span the whole text of the outermost element
注意结束标签的顺序,与起始标签顺序相反。这样做的原因是帮助你更清晰的考虑内部标签不必跨越整个文本的最外层元素。(标签不能交叉包含,只能一对标签包含另一对;而一对标签不能只包含另一对标签的一部分。)
We are <b>absolutely <i>definitely</i> not</b> amused
which gives
显示为:
We are absolutely definitely not amused
Tag parameters
标签参数
Some tags have a simple all-or-nothing effect on the text but others might allow for variations. For example, the color tag needs to know which color to apply. Information like this is added to tags by the use of parameters:-
有一些标签只是简单的有或无的效果,但是也有其他的一些是允许变化的。比如,颜色标签需要知道应用哪个颜色。像这样的信息通过使用参数被添加到标签:
We are <color=green>green</color> with envy
Note that the ending tag doesn’t include the parameter value. Optionally, the value can be surrounded by quotation marks but this isn’t required.
请注意,结束标签不包含参数值。 可选择的,参数值值可以用引号括起来,但这不是必需的。
Supported tags
支持的标签
The following list describes all the styling tags supported by Unity.
下面的列表描述所有unity支持的标签样式。
b
Renders the text in boldface.
以粗体显示文本
We are <b>not</b> amused.
i
Renders the text in italics.
以斜体显示文本
We are <i>usually</i> not amused.
size
Sets the size of the text according to the parameter value, given in pixels.
根据参数值设置文本的大小,以像素为单位。
We are <size=50>largely</size> unaffected.
Although this tag is available for Debug.Log, you will find that the line spacing in the window bar and Console looks strange if the size is set too large.
虽然这个标签可用于Debug.Log,但如果尺寸设置得太大,您会发现窗口栏和控制台中的行间距看起来很奇怪。
color
Sets the color of the text according to the parameter value. The color can be specified in the traditional HTML format. #rrggbbaa …where the letters correspond to pairs of hexadecimal digits denoting the red, green, blue and alpha (transparency) values for the color. For example, cyan at full opacity would be specified by
根据参数值设置文本的颜色。 颜色可以用传统的HTML格式指定。 #rrggbbaa ...其中每一对字母分别表示该颜色的红色,绿色,蓝色和alpha(透明度)值的十六进制数字。 例如,完全不透明度的青色可以这么表示
<color=#00ffffff>…
Another option is to use the name of the color. This is easier to understand but naturally, the range of colors is limited and full opacity is always assumed.
<color=cyan>…
The available color names are given in the table below.
另一种选择是使用颜色的名称。 这很容易理解,但自然的,颜色的范围是有限的,而且总是假设完全不透明。
<color = cyan> ...
可用的颜色名称在下表中给出(这个就不翻译了,详见原文)。
material
This is only useful for text meshes and renders a section of text with a material specified by the parameter. The value is an index into the text mesh’s array of materials as shown by the inspector.
这只对文本网格有用,并用参数指定的材质渲染一段文本。 该值是inspector显示的文本网格材质数组的索引。
We are <material=2>texturally</material> amused
quad
This is only useful for text meshes and renders an image inline with the text. It takes parameters that specify the material to use for the image, the image height in pixels, and a further four that denote a rectangular area of the image to display. Unlike the other tags, quad does not surround a piece of text and so there is no ending tag - the slash character is placed at the end of the initial tag to indicate that it is “self-closing”.
这仅仅用于文本网格,在文本中渲染图片。 它需要指定要用于图片的材质的参数,以像素为单位的图片高度,以及另外四个参数表示要显示的图片的矩形区域。 与其他标签不同,quad不包围一段文本,所以没有结束标签 - 斜杠字符放在最初的标签末尾,以表明它是“自己封闭”的。
<quad material=1 size=20 x=0.1 y=0.1 width=0.5 height=0.5 />
This selects the material at position in the renderer’s material array and sets the height of the image to 20 pixels. The rectangular area of image starts at given by the x, y, width and height values, which are all given as a fraction of the unscaled width and height of the texture.
这将选择渲染器材质数组中位置的材质,并将图片的高度设置为20像素。 图片的矩形区域从x,y,宽度和高度值开始,它们都是按照纹理的未缩放宽度和高度的给出的。
Editor GUI
编辑器GUI
Rich text is disabled by default in the editor GUI system but it can be enabled explicitly using a custom GUIStyle. The richText property should be set to true and the style passed to the GUI function in question:-
在编辑器GUI系统中,默认情况下禁用富文本,但可以使用自定义GUIStyle显式启用。 richText属性应设置为true,并在调用GUI函数的时候把样式传进去:
GUIStyle style = new GUIStyle ();
style.richText = true;
GUILayout.Label("<size=30>Some <color=yellow>RICH</color> text</size>",style);