UI Toolbox logoUI Toolbox

CodeBlock

A CodeBlock shows source code, or other text where the spaces matter. It uses a monospace font and keeps the indentation. It has line numbers, a title and a button that copies the code to the clipboard. Use it for a console command in a help screen, for a mod script, or for a save code that the player shares.

Three code blocks: UXML with a file name as the title, C# with a line that scrolls sideways, and two lines with no header

<tb:CodeBlock title="Inventory.uxml" code="&lt;ui:Label text=&quot;12 items&quot; /&gt;&#10;&lt;tb:Rating value=&quot;4&quot; /&gt;" />
var block = new CodeBlock { Title = "C#", Language = CodeLanguage.CSharp, Code = "if (ready)\n\tstart();" };
block.Copied += code => Debug.Log($"Copied {code.Length} characters");
panel.Add(block);
Member Description
Code (code) The code. A line end is \n; the control also reads \r\n. A tab shows as four spaces. The clipboard gets the code with its tabs.
Language (language) The language of the code: None, CSharp, Uxml, Uss or Json. The words of the language get colors. None by default: the code has one color. See Syntax colors.
Title (title) The text above the code, such as a file name or a language. An empty title shows no text.
ShowLineNumbers (show-line-numbers) Shows the number of each line to the left of the code. On by default.
ShowCopyButton (show-copy-button) Shows the copy button. On by default.
LineCount The number of lines of the code.
Copy() Puts the code on the system clipboard, raises Copied and shows a check mark on the button for two seconds.
Copied The event after a copy. The argument is the code.

Tips:

Syntax colors

Set Language and the control gives a color to each part of the code that it knows. The code itself does not change: Code, Copy() and Copied give the code with no colors, and the line numbers stay the same.

<tb:CodeBlock title="Player.cs" language="CSharp" code="var lives = 3; // at the start" />
block.Language = CodeLanguage.Json;
Language The parts with a color
None No part. This is the default.
CSharp Keywords, strings, characters, numbers, comments, and the type name after new, class, struct, enum, interface or record.
Uxml Element names with their brackets, attribute names, attribute values and comments. Use it for other XML too.
Uss Selectors, property names, numbers with their unit, colors such as #ff8800, strings, variables (--name) in a value, and comments.
Json Keys, strings, numbers, and true, false, null.

Each color is a custom property on the block. Set it on .tb-code-block, or on your own class of a block.

Property The part Dark block Light block
--tb-code-keyword A keyword. In JSON: true, false, null. #569CD6 #0000FF
--tb-code-type A type name in C#. A variable in a USS value. #4EC9B0 #267F99
--tb-code-string A string, a character, the value of a UXML attribute. #CE9178 #A31515
--tb-code-comment A comment. --tb-color-text-muted --tb-color-text-muted
--tb-code-number A number. In USS also a color such as #ff8800. #B5CEA8 #098658
--tb-code-tag A UXML element name. A USS selector. #569CD6 #800000
--tb-code-attribute A UXML attribute name, a USS property name, a JSON key. #9CDCFE #E50000

The control has two sets of default colors. It takes the set for a dark block when the background color of the block is dark, and the set for a light block when it is light. So the colors follow the theme. A property that you set replaces the default in the two sets.

.my-code {
    background-color: rgb(40, 42, 54);
    --tb-code-keyword: rgb(255, 121, 198);
    --tb-code-string: rgb(241, 250, 140);
    --tb-code-comment: rgb(98, 114, 164);
}
.my-code .tb-code-block__text { color: rgb(248, 248, 242); }

The words with no color of their own have the color of .tb-code-block__text.

Limits:

Styling

Class Element
tb-code-block The block: --tb-color-background, a border and a radius.
tb-code-block__header The row with the title and the copy button, in --tb-color-surface-alt.
tb-code-block__title The title, bold and small, in --tb-color-text-muted.
tb-code-block__copy The copy button.
tb-code-block__copy--done The copy button for two seconds after a copy, in --tb-color-success.
tb-code-block__copy-icon The icon in the copy button, 14 px. The control draws it in the color of the button. A background-image replaces the drawn icon.
tb-code-block__copy-text The text "Copy" or "Copied" in the copy button. It is off (display: none).
tb-code-block__body The row with the line numbers and the code.
tb-code-block__numbers The line numbers, in --tb-color-text-muted, with a border on the right.
tb-code-block__scroll The horizontal ScrollView of the code.
tb-code-block__text The code: JetBrains Mono, small, white-space: pre.
.tb-code-block { background-color: #101418; border-color: var(--tb-color-accent); }
.tb-code-block__text { color: #9be29b; }

The copy button

The color of the icon is the color of the button:

.tb-code-block .tb-code-block__copy { color: var(--tb-color-accent); }

For your own icon, set an image. Set one for the state after a copy too:

.tb-code-block__copy-icon {
    background-image: url("Icons/clipboard.png");
    -unity-background-image-tint-color: var(--tb-color-text-muted);
}
.tb-code-block__copy--done .tb-code-block__copy-icon {
    background-image: url("Icons/check.png");
    -unity-background-image-tint-color: var(--tb-color-success);
}

For the text and no icon:

.tb-code-block .tb-code-block__copy-text { display: flex; }
.tb-code-block__copy-icon { display: none; }

Leave the icon on to show the icon and the text together.