Configure Smart Strings
Set up a project-wide default formatter and customize Smart Strings formatting in code.
Read time 3 minutesLast updated 5 days ago
Set up a project-wide default formatter and customize Smart Strings formatting in code.
A Smart String is formatted by a , which holds an ordered list of sources and formatters. You configure the formatter once for the whole project in a settings asset, or build a custom formatter in code and call it directly.
SmartFormatterSet a project-wide default formatter
Create a Smart Strings settings asset to define the default formatter for the project and include it in player builds:
- Open > Smart Strings.Project Settings
?
- Select Create and choose where to save the asset.
- Edit the Smart Formatter field on the asset to add, remove, or reorder sources and formatters.
The asset is a ScriptableObject. Its property is initialized with . The active asset is stored as an EditorBuildSettings config object and added to the player's preloaded assets at build time, so it ships in the player and registers itself at startup.
SmartStringsSettingsSmartFormatterSmart.CreateDefaultSmartFormat()For details on the asset's properties, refer to Smart Strings settings.
Customize the formatter in code
Smart.DefaultSmartFormatterSmart.FormatSmart.CreateDefaultSmartFormat()Smart.Default[ThreadStatic]To start from a fresh formatter with the core extensions registered, call :
Smart.CreateDefaultSmartFormat()var formatter = Smart.CreateDefaultSmartFormat();
Add, remove, and reorder extensions
Sources and formatters are tried in list order, so the order changes which one handles a placeholder. Configure the lists on a with these methods:
SmartFormatterMethod | Description |
|---|---|
| Adds source extensions. Well-known sources are inserted at their recommended position; others go at the end. A type that's already present is skipped. |
| Adds formatter extensions, with the same insertion and deduplication rules. |
| Inserts a single extension at an explicit position in the list. |
| Removes the extension of type |
| Returns the extension of type |
Refer to for API reference. An extension that implements both and is registered in both lists automatically. The and methods return the same , so you can chain calls.
Smart.FormatISourceIFormatterAddExtensionsInsertExtensionSmartFormatter// Remove a source and a formatter. formatter.RemoveSourceExtension<DictionarySource>(); formatter.RemoveFormatterExtension<PluralLocalizationFormatter>(); // Add a custom source at the front so it's tried first. formatter.InsertExtension(0, new MySource()); // Look up an existing extension. var properties = formatter.GetSourceExtension<PropertiesSource>();
Format a Smart String
Call the static to format with . Pass an IFormatProvider (such as a CultureInfo) to control culture-dependent formatting:
Smart.FormatSmart.Default// "Total: 1,234.5" Smart.Format("Total: {0}", 1234.5); // Format with a specific culture. Smart.Format(CultureInfo.GetCultureInfo("de-DE"), "Total: {0}", 1234.5);
The class adds convenience methods that use the same semantics as :
SmartExtensionsSmart.FormatMethod | Description |
|---|---|
| Formats the string as a template and returns the result. |
| Appends the formatted result to the |
| Appends the formatted result followed by a line terminator. |
| Writes the formatted result to the |
| Writes the formatted result followed by a line terminator. |
var result = "Hello {Name}.".FormatSmart(new Character { Name = "Lara" }); var sb = new StringBuilder(); sb.AppendSmart("Score: {0}", 42);
To write directly into a or without allocating an intermediate string, call . The convenience methods above wrap an around the target for you.
StringBuilderTextWriterSmartFormatter.FormatInto(IOutput, ...)IOutput