Class CodeEditor
An editable source-code surface with syntax highlighting.
Implements
Inherited Members
Namespace: Unity.AppUI.UI
Assembly: Unity.AppUI.dll
Syntax
[UxmlElement]
public class CodeEditor : BaseVisualElement, IEventHandler, IResolvedStyle, ITransform, ITransitionAnimations, IExperimentalFeatures, IVisualElementScheduler, IContextOverrideElement, IAdditionalDataHolder, INotifyValueChanged<string>
Remarks
This is CodeBlock's highlighting over TextArea's editing. Neither half is reimplemented: the TextArea owns focus, caret, selection, undo and the runtime context menu, and the same TextMate grammar/theme pair that colours a CodeBlock colours this.
How the two layers stay aligned. UI Toolkit has no editable rich text — a TextField renders its value raw — so, exactly as on the web, the colours live on a separate element layered against a text input whose own glyphs are turned transparent. Both layers are styled by one shared USS rule so their font, size, padding and wrapping cannot drift apart; changing one without the other misaligns the caret and nothing errors.
What is deliberately missing. The registry keeps the highlight layer's scroll offset in sync with the textarea's on every scroll event, because a DOM textarea is a fixed box that scrolls its own content. Here the editor grows with its content instead — autoResize is on, both layers are full height, and there is no independent scroll offset to keep in sync. Put the editor in a ScrollView if the document can outgrow the space. Porting the sync would have been dead code.
Degrading. When no grammar and theme resolve — which includes every player build, since
grammars are looked up from asset GUIDs, and every project that does not define
APPUI_ENABLE_SYNTAX_HIGHLIGHTING — the overlay stays empty and the input keeps its own
visible text. The editor is fully usable either way, which mirrors the registry's behaviour when
its highlighter chunk fails to load.
Setting language adds a appui-code-editor--lang-<value> class, so
language-to-grammar mappings are declared in USS the same way CodeBlock declares
them, through the --codeblock-grammar and --codeblock-theme custom properties.
Examples
A Lua script editor.
var editor = new CodeEditor
{
language = "lua",
placeholder = "-- your script here",
value = "function update(dt)\n print(dt)\nend\n",
};
editor.RegisterValueChangedCallback(evt => Save(evt.newValue));
parentElement.Add(editor);
In UXML.
<appui:CodeEditor language="csharp" placeholder="// code" indent-size="4" />
Constructors
CodeEditor()
Default constructor.
Declaration
public CodeEditor()
CodeEditor(string)
Constructs an editor holding a predefined source. No change event is sent.
Declaration
public CodeEditor(string value)
Parameters
| Type | Name | Description |
|---|---|---|
| string | value | The initial source. |
Fields
defaultIndentSize
The indent width used when nothing else is asked for.
Declaration
public const int defaultIndentSize = 2
Field Value
| Type | Description |
|---|---|
| int |
highlightUssClassName
The styling class of the highlight layer drawn over the input.
Declaration
public const string highlightUssClassName = "appui-code-editor__highlight"
Field Value
| Type | Description |
|---|---|
| string |
highlightedUssClassName
The styling class toggled while a highlighted rendering is actually being displayed.
Declaration
public const string highlightedUssClassName = "appui-code-editor--highlighted"
Field Value
| Type | Description |
|---|---|
| string |
inputUssClassName
The styling class of the editable input layer.
Declaration
public const string inputUssClassName = "appui-code-editor__input"
Field Value
| Type | Description |
|---|---|
| string |
languageVariantUssClassNamePrefix
Prefix of the per-language modifier class added when language is set.
Declaration
public const string languageVariantUssClassNamePrefix = "appui-code-editor--lang-"
Field Value
| Type | Description |
|---|---|
| string |
ussClassName
The CodeEditor main styling class.
Declaration
public const string ussClassName = "appui-code-editor"
Field Value
| Type | Description |
|---|---|
| string |
Properties
contentContainer
Children are managed internally — this element exposes no content container.
Declaration
public override VisualElement contentContainer { get; }
Property Value
| Type | Description |
|---|---|
| VisualElement |
Overrides
indentSize
How many spaces the Tab key inserts. Two by default, matching the registry. Zero or less leaves Tab as a focus-navigation key.
Declaration
[CreateProperty]
[UxmlAttribute]
public int indentSize { get; set; }
Property Value
| Type | Description |
|---|---|
| int |
isHighlighted
Whether a highlighted rendering is currently being displayed. False in a player build, and false whenever no grammar and theme pair resolves for the current language.
Declaration
public bool isHighlighted { get; }
Property Value
| Type | Description |
|---|---|
| bool |
isReadOnly
Whether the source can be edited. A read-only editor still highlights, still scrolls and still lets the reader select and copy — and it leaves Tab alone, so it does not trap focus.
Declaration
[CreateProperty]
[UxmlAttribute]
public bool isReadOnly { get; set; }
Property Value
| Type | Description |
|---|---|
| bool |
language
The grammar identifier, for instance "csharp" or "lua". Adds a
appui-code-editor--lang-<value> class so USS can map it to a grammar. An
unmapped identifier renders as plain text.
Declaration
[CreateProperty]
[UxmlAttribute]
public string language { get; set; }
Property Value
| Type | Description |
|---|---|
| string |
placeholder
The text shown while the editor is empty.
Declaration
[CreateProperty]
[UxmlAttribute]
public string placeholder { get; set; }
Property Value
| Type | Description |
|---|---|
| string |
textArea
The editing surface. Exposed so a caller can reach TextArea features this element does not re-expose, such as maxLength or validateValue.
Declaration
public TextArea textArea { get; }
Property Value
| Type | Description |
|---|---|
| TextArea |
value
The source code being edited.
Declaration
[CreateProperty]
[UxmlAttribute]
public string value { get; set; }
Property Value
| Type | Description |
|---|---|
| string |
Methods
InsertIndent()
Inserts one indent at the caret, replacing the selection if there is one. Does nothing in a read-only editor, and nothing when indentSize is zero or less.
Declaration
public void InsertIndent()
SetValueWithoutNotify(string)
Sets the source without sending a change event.
Declaration
public void SetValueWithoutNotify(string newValue)
Parameters
| Type | Name | Description |
|---|---|---|
| string | newValue | The new source. |