Version: Unity 7.0 Alpha (7000.0)
Language : English
Sub String formatter
Time formatter

Template formatter

Register reusable Smart String templates and apply them by name.

The Template formatter stores named templates so you can reuse a piece of formatting across many Smart Strings. When the formatting needs to change, you edit the template in one place. Apply it explicitly with the name t.

Anatomy of the template (t) formatter syntax.
Anatomy of the template (t) formatter syntax.

Template names are matched using the formatter’s case sensitivity setting. By default, matching is case-insensitive.

Register the templates you create and add them to the formatter so they can be used. The Template formatter can’t auto-detect, so you must always apply it by name. Applying an unregistered template name throws a FormattingException. Refer to Configure Smart Strings for more information on adding to the formatter.

The following example shows how to register templates on the formatter and then add the formatter to a SmartFormatter:

var templates = new TemplateFormatter();
templates.Register("fullName", "{name} {surname}");
templates.Register("initials", "{name:substr(0,1)} {surname:substr(0,1)}");
templates.Register("highlight", "<color=red>{}</color>");

Smart.Default.AddExtensions(templates);

Pass the template name as a formatter option, {0:t(fullName)}, or as the format part, {0:t:fullName}. Both are equivalent.

To reference the current value in scope inside a template, use an empty placeholder {}, as in the highlight template in the table below.

Templates can be nested: a template can apply another template, for example {:t:FIRST} {:t:last}.

Smart String Argument Result
The initials are {0:t(initials)} name = "Lara", surname = "Croft" The initials are L C
Hello {0:t(fullName)} name = "Lara", surname = "Croft" Hello Lara Croft
The name is {name:t(highlight)}. name = "Lara" The name is <color=red>Lara</color>.
The name is {surname:t(highlight)}. surname = "Croft" The name is <color=red>Croft</color>.

Additional resources

Sub String formatter
Time formatter
Copyright © 2023 Unity Technologies
优美缔软件(上海)有限公司 版权所有
"Unity"、Unity 徽标及其他 Unity 商标是 Unity Technologies 或其附属机构在美国及其他地区的商标或注册商标。其他名称或品牌是其各自所有者的商标。
公安部备案号:
31010902002961