Как сделать текст в юнити
«Обогащенный» текст (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.
Формат разметки
Система разметки текста в Unity была создана на основе HTML, однако при этом строгая совместимость со стандартным HTML не подразумевается. Основная идея заключается в том, что фрагменты текста можно заключать в пару согласующихся друг с другом тегов:-
We are not amused
Размеченный фрагмент текста (включая оборачивающие его теги) также называется элементом.
Вложенные элементы
Существует возможность применять более одного стиля к фрагменту текста при помощи «вложения» одних элементов в другие.
We are definitely not amused
Тег i задает фрагменту курсивный стиль шрифта, отображающийся на экране следующим образом:
We are **definitely not** amused
Обратите внимание, что порядок закрывающих тегов противоположен порядку открывающих. Причину этого проще понять, если вы примете во внимание тот факт, что внутренние теги не обязательно должны распространяться на весь текст вплоть до последнего элемента
We are absolutely definitely not amused
дает следующий результат:
We are absolutely definitely not amused
Параметры тегов
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 green 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.
Поддерживаемые теги
The following list describes all the styling tags supported by Unity.
Tag | Описание | Шестнадцатиричный код | Notes |
---|---|---|---|
b | Renders the text in boldface. | We are not amused. | |
i | Renders the text in italics. | We are usually not amused. | |
size | Sets the size of the text according to the parameter value, given in pixels. | We are largely 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. |
Название цвета | 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 | … | 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. … The available color names are given in the table below. |
Название цвета | Шестнадцатиричный код | Пример |
---|---|---|
aqua (same as cyan) | #00ffffff | |
black | #000000ff | |
blue | #0000ffff | |
brown | #a52a2aff | |
cyan (same as aqua) | #00ffffff | |
darkblue | #0000a0ff | |
fuchsia (same as magenta) | #ff00ffff | |
green | #008000ff | |
grey | #808080ff | |
lightblue | #add8e6ff | |
lime | #00ff00ff | |
magenta (same as fuchsia) | #ff00ffff | |
maroon | #800000ff | |
navy | #000080ff | |
olive | #808000ff | |
orange | #ffa500ff | |
purple | #800080ff | |
red | #ff0000ff | |
silver | #c0c0c0ff | |
teal | #008080ff | |
white | #ffffffff | |
yellow | #ffff00ff |
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.
We are texturally amused
quad
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.
Editor 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:-
Как сделать текст в юнити
The Text control displays a non-interactive piece of text to the user. This can be used to provide captions or labels for other GUI controls or to display instructions or other text.
A Text element.
Свойства
Свойство: | Функция: |
---|---|
Text | The text displayed by the control. |
Character | |
Font __ | The Font used to display the text. | |Font Style__ | The style applied to the text. The options are Normal, Bold, Italic and Bold And Italic. |
Font Size | The size of the displayed text. |
Line Spacing | The vertical separation between lines of text. |
Rich Text | Should markup elements in the text be interpreted as Rich Text styling? |
Paragraph | |
Alignment | The horizontal and vertical alignment of the text. |
Align by Geometry | Use the extents of glyph geometry to perform horizontal alignment rather than glyph metrics. |
Horizontal Overflow | The method used to handle the situation where the text is too wide to fit in the rectangle. The options are Wrap and Overflow. |
Vertical Overflow | The method used to handle the situation where wrapped text is too tall to fit in the rectangle. The options are Truncate and Overflow. |
Best Fit | Should Unity ignore the size properties and simply try to fit the text to the control’s rectangle? |
Color | Цвет, используемый для отображение текста. |
Material | The Material used to render the text. |
Детали
Some controls (such as Buttons and Toggles have textual descriptions built-in. For controls that have no implicit text (such as Sliders, you can indicate the purpose using a label created with a Text control. Text is also useful for lists of instructions, story text, conversations and legal disclaimers.
The Text control offers the usual parameters for font size, style, etc, and text alignment. When the Rich Text option is enabled, markup elements within the text will be treated as styling information, so you can have just a single word or short section in boldface or in a different color, say (see the page about Rich Text for details of the markup scheme).
GUI Text (Legacy UI Component)
GUI Text displays text of any font you import in screen coordinates.
Please Note: This component relates to legacy methods for drawing UI textures and images to the screen. You should use Unity’s up-to-date UI system instead. This is also unrelated to the IMGUI system.
Свойства
Свойство: | Функция: |
---|---|
Text | The string to display. |
Anchor | The point at which the Text shares the position of the Transform. |
Alignment | How multiple lines are aligned within the GUIText. |
Pixel Offset | Offset of the text relative to the position of the GUIText in the screen. |
Line Spacing | How much space will be in-between lines of Text. |
Tab Size | How much space will be inserted for a tab (‘\t’) character. As a multiplum of the space character offset. |
Font | The Font to use when rendering the text. |
Material | Reference to the Material containing the characters to be drawn. If set, this property overrides the one in the Font asset. |
Font Size | The font size to use. Set to 0 to use the default font size. Only applicable for dynamic fonts. |
Font Style | The font style to use. (Normal, Bold, Italic or Bold and Italic). Only applicable for dynamic fonts. |
Pixel Correct | If enabled, all Text characters will be drawn in the size of the imported font texture. If disabled, the characters will be resized based on the Transform’s Scale. |
Rich Text | If enabled, allows HTML-style tags for text formatting. |
Детали
GUI Texts are used to print text onto the screen in 2D. The Camera has to have a GUI Layer attached in order to render the text. Cameras include a GUI Layer by default, so don’t remove it if you want to display a GUI Text. GUI Texts are positioned using only the X and Y axes. Rather than being positioned in World Coordinates, GUI Texts are positioned in Screen Coordinates, where (0,0) is the bottom-left and (1,1) is the top-right corner of the screen. To add a GUIText component in Unity 5.0, first use GameObject->Create Empty to create an empty game object, then use the Component->Rendering->GUIText option to add the GUIText component to the newly created game object. If the text isn’t visible when you press Play, check that the transform has suitable position, typically (0.5, 0.5, 0.0).
To import a font see the Font page.
To use Rich Text see the Rich Text page.
Pixel Correct
By default, GUI Texts are rendered with Pixel Correct enabled. This makes them look crisp and they will stay the same size in pixels independent of the screen resolution.
«Обогащенный» текст (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.
Формат разметки
Система разметки текста в Unity была создана на основе HTML, однако при этом строгая совместимость со стандартным HTML не подразумевается. Основная идея заключается в том, что фрагменты текста можно заключать в пару согласующихся друг с другом тегов:-
We are not amused
Размеченный фрагмент текста (включая оборачивающие его теги) также называется элементом.
Вложенные элементы
Существует возможность применять более одного стиля к фрагменту текста при помощи «вложения» одних элементов в другие.
We are definitely not amused
Тег i задает фрагменту курсивный стиль шрифта, отображающийся на экране следующим образом:
We are definitely not amused
Обратите внимание, что порядок закрывающих тегов противоположен порядку открывающих. Причину этого проще понять, если вы примете во внимание тот факт, что внутренние теги не обязательно должны распространяться на весь текст вплоть до последнего элемента
We are absolutely definitely not amused
дает следующий результат:
We are absolutely definitely not amused
Параметры тегов
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 green 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.
Поддерживаемые теги
The following list describes all the styling tags supported by Unity.
Tag | Описание | Шестнадцатиричный код | Notes |
---|---|---|---|
b | Renders the text in boldface. | We are not amused. | |
i | Renders the text in italics. | We are usually not amused. | |
size | Sets the size of the text according to the parameter value, given in pixels. | We are largely 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. |
Название цвета | 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 | … | 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. … The available color names are given in the table below. |
Название цвета | Шестнадцатиричный код | Пример |
---|---|---|
aqua (same as cyan) | #00ffffff | |
black | #000000ff | |
blue | #0000ffff | |
brown | #a52a2aff | |
cyan (same as aqua) | #00ffffff | |
darkblue | #0000a0ff | |
fuchsia (same as magenta) | #ff00ffff | |
green | #008000ff | |
grey | #808080ff | |
lightblue | #add8e6ff | |
lime | #00ff00ff | |
magenta (same as fuchsia) | #ff00ffff | |
maroon | #800000ff | |
navy | #000080ff | |
olive | #808000ff | |
orange | #ffa500ff | |
purple | #800080ff | |
red | #ff0000ff | |
silver | #c0c0c0ff | |
teal | #008080ff | |
white | #ffffffff | |
yellow | #ffff00ff |
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.
We are texturally amused
quad
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.
Editor 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:-