Version: Unity 6.7 Alpha (6000.7)
Language : English
Choose formatter
Default formatter

Conditional formatter

Select one of several outputs based on the type and value of a placeholder.

The Conditional formatter splits a format into choices and picks one based on the placeholder value. Apply it explicitly with the name cond, or omit the name to apply it implicitly when the format contains a split character. It interprets the value differently depending on its type: numbers, booleans, strings, dates, and time spans each have their own rules.

Separate the choices with the split character. The default is |, but , and ~ are also valid.

Each choice is itself a format, so it can contain nested placeholders. An empty placeholder {} reuses the current value.

Numbers

For a number, the value selects the choice at that index. Non-integer values are rounded down (floored). A negative value, or a value past the last choice, selects the last choice.

Smart String Argument Result
{0:cond:Apple\|Pie\|Orange\|Banana\|No fruit} 0 Apple
{0:cond:Apple\|Pie\|Orange\|Banana\|No fruit} 3 Banana
{0:cond:Apple\|Pie\|Orange\|Banana\|No fruit} -1 No fruit
{0:cond:zero\|one\|two\|three\|other} 4 other

Complex comparisons

You can define more complex comparisons using operators. Use ? to separate the comparison and its choice. The final choice has no comparison and acts as the fallback when none match.

The complex comparison syntax only applies to convertible numeric values.

The following comparison operators are supported:

Operator Description
>= greater than or equal to
> greater than
= equal to
< less than
<= less than or equal to
!= not equal to

Combine comparisons with & for AND or / for OR.

Smart String Argument Result
{0:cond:>10?Greater than 10\|=10?Equal to 10\|Less than 10} 5 Less than 10
{0:cond:>=55?Senior\|>=30?Adult\|>=18?Young adult\|Child} 42 Adult

Booleans

For a boolean, the first choice is used when the value is true and the second when it is false.

Smart String Argument Result
Enabled? {0:cond:Yes\|No}. true Enabled? Yes.
Enabled? {0:cond:Yes\|No}. false Enabled? No.

Strings

For a string, the first choice is used when the value is not null or empty; otherwise the second choice is used.

Smart String Argument Result
The string is {0:cond:not empty\|empty} "Some text" The string is not empty
The string is {0:cond:not empty\|empty} "" The string is empty
Text: {0:cond:{}\|No text} "Hello World" Text: Hello World
Text: {0:cond:{}\|No text} "" Text: No text

DateTime and DateTimeOffset

For a DateTime or DateTimeOffset, the value is compared to the current date in UTC. Use three choices for past, today, and future indices. Use two choices for today-or-past and future.

Smart String Argument Result
My birthday {0:cond:was yesterday\|is today\|will be tomorrow} a past date My birthday was yesterday
My birthday {0:cond:was yesterday\|is today\|will be tomorrow} today My birthday is today
My birthday {0:cond:was yesterday\|is today\|will be tomorrow} a future date My birthday will be tomorrow

TimeSpan

For a TimeSpan, the value is compared to TimeSpan.Zero. Use three choices for negative, zero, and positive indices. Use two choices for negative-or-zero and positive indices.

Smart String Argument Result
The event {0:cond:was {Hours} hours ago\|is now\|will start in {Hours} hours} a negative duration The event was 2 hours ago
The event {0:cond:was {Hours} hours ago\|is now\|will start in {Hours} hours} TimeSpan.Zero The event is now
The event {0:cond:was {Hours} hours ago\|is now\|will start in {Hours} hours} a positive duration The event will start in 3 hours

Other objects

For any other type, the value is compared to null: the first choice is used when it is not null, the second when it is.

Smart String Argument Result
{0:cond:not null\|null} an object not null
{0:cond:not null\|null} null null

Additional resources

Choose formatter
Default formatter