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