docs.unity3d.com
Search Results for

    Show / Hide Table of Contents

    Class CodeEditor

    An editable source-code surface with syntax highlighting.

    Inheritance
    object
    CallbackEventHandler
    Focusable
    VisualElement
    BaseVisualElement
    CodeEditor
    Implements
    IEventHandler
    IResolvedStyle
    ITransform
    ITransitionAnimations
    IExperimentalFeatures
    IVisualElementScheduler
    IContextOverrideElement
    IAdditionalDataHolder
    INotifyValueChanged<string>
    Inherited Members
    BaseVisualElement.contextPrefix
    BaseVisualElement.densityOverride
    BaseVisualElement.themeOverride
    BaseVisualElement.langOverride
    BaseVisualElement.layoutDirectionOverride
    BaseVisualElement.preferredTooltipPlacementOverride
    BaseVisualElement.tooltipDelayMsOverride
    BaseVisualElement.GetLayoutDirectionUssClassName(Dir)
    VisualElement.disabledUssClassName
    VisualElement.SetActivePseudoState(bool)
    VisualElement.SetCheckedPseudoState(bool)
    VisualElement.Focus()
    VisualElement.SendEvent(EventBase)
    VisualElement.SetEnabledFromHierarchy(bool)
    VisualElement.SetEnabled(bool)
    VisualElement.MarkDirtyRepaint()
    VisualElement.IsMarkedForRepaint()
    VisualElement.ContainsPoint(Vector2)
    VisualElement.Overlaps(Rect)
    VisualElement.DoMeasure(float, VisualElement.MeasureMode, float, VisualElement.MeasureMode)
    VisualElement.ToString()
    VisualElement.GetClasses()
    VisualElement.ClearClassList()
    VisualElement.AddToClassList(string)
    VisualElement.RemoveFromClassList(string)
    VisualElement.ToggleInClassList(string)
    VisualElement.EnableInClassList(string, bool)
    VisualElement.ClassListContains(string)
    VisualElement.FindAncestorUserData()
    VisualElement.SetBinding(BindingId, Binding)
    VisualElement.GetBinding(BindingId)
    VisualElement.TryGetBinding(BindingId, out Binding)
    VisualElement.GetBindingInfos()
    VisualElement.GetBindingInfos(List<BindingInfo>)
    VisualElement.HasBinding(BindingId)
    VisualElement.ClearBinding(BindingId)
    VisualElement.ClearBindings()
    VisualElement.GetHierarchicalDataSourceContext()
    VisualElement.GetDataSourceContext(BindingId)
    VisualElement.TryGetDataSourceContext(BindingId, out DataSourceContext)
    VisualElement.TryGetLastBindingToUIResult(in BindingId, out BindingResult)
    VisualElement.TryGetLastBindingToSourceResult(in BindingId, out BindingResult)
    VisualElement.Add(VisualElement)
    VisualElement.Insert(int, VisualElement)
    VisualElement.Remove(VisualElement)
    VisualElement.RemoveAt(int)
    VisualElement.Clear()
    VisualElement.ElementAt(int)
    VisualElement.IndexOf(VisualElement)
    VisualElement.Children()
    VisualElement.Sort(Comparison<VisualElement>)
    VisualElement.BringToFront()
    VisualElement.SendToBack()
    VisualElement.PlaceBehind(VisualElement)
    VisualElement.PlaceInFront(VisualElement)
    VisualElement.RemoveFromHierarchy()
    VisualElement.GetFirstOfType<T>()
    VisualElement.GetFirstAncestorOfType<T>()
    VisualElement.Contains(VisualElement)
    VisualElement.FindCommonAncestor(VisualElement)
    VisualElement.viewDataKey
    VisualElement.userData
    VisualElement.canGrabFocus
    VisualElement.focusController
    VisualElement.disablePlayModeTint
    VisualElement.usageHints
    VisualElement.transform
    VisualElement.scaledPixelsPerPoint
    VisualElement.layout
    VisualElement.contentRect
    VisualElement.paddingRect
    VisualElement.worldBound
    VisualElement.localBound
    VisualElement.worldTransform
    VisualElement.hasActivePseudoState
    VisualElement.hasInactivePseudoState
    VisualElement.hasHoverPseudoState
    VisualElement.hasCheckedPseudoState
    VisualElement.hasEnabledPseudoState
    VisualElement.hasDisabledPseudoState
    VisualElement.hasFocusPseudoState
    VisualElement.hasRootPseudoState
    VisualElement.pickingMode
    VisualElement.name
    VisualElement.enabledInHierarchy
    VisualElement.enabledSelf
    VisualElement.languageDirection
    VisualElement.visible
    VisualElement.generateVisualContent
    VisualElement.dataSource
    VisualElement.dataSourcePath
    VisualElement.dataSourceType
    VisualElement.experimental
    VisualElement.hierarchy
    VisualElement.cacheAsBitmap
    VisualElement.parent
    VisualElement.panel
    VisualElement.visualTreeAssetSource
    VisualElement.this[int]
    VisualElement.childCount
    VisualElement.schedule
    VisualElement.style
    VisualElement.resolvedStyle
    VisualElement.customStyle
    VisualElement.styleSheets
    VisualElement.tooltip
    Focusable.Blur()
    Focusable.focusable
    Focusable.tabIndex
    Focusable.delegatesFocus
    CallbackEventHandler.RegisterCallback<TEventType>(EventCallback<TEventType>, TrickleDown)
    CallbackEventHandler.RegisterCallbackOnce<TEventType>(EventCallback<TEventType>, TrickleDown)
    CallbackEventHandler.RegisterCallback<TEventType, TUserArgsType>(EventCallback<TEventType, TUserArgsType>, TUserArgsType, TrickleDown)
    CallbackEventHandler.RegisterCallbackOnce<TEventType, TUserArgsType>(EventCallback<TEventType, TUserArgsType>, TUserArgsType, TrickleDown)
    CallbackEventHandler.RegisterCallback<TEventType>(EventCallback<TEventType>, CallbackOptions)
    CallbackEventHandler.RegisterCallback<TEventType, TUserArgsType>(EventCallback<TEventType, TUserArgsType>, TUserArgsType, CallbackOptions)
    CallbackEventHandler.UnregisterCallback<TEventType>(EventCallback<TEventType>, TrickleDown)
    CallbackEventHandler.UnregisterCallback<TEventType>(EventCallback<TEventType>, CallbackOptions)
    CallbackEventHandler.UnregisterCallback<TEventType, TUserArgsType>(EventCallback<TEventType, TUserArgsType>, TrickleDown)
    CallbackEventHandler.UnregisterCallback<TEventType, TUserArgsType>(EventCallback<TEventType, TUserArgsType>, CallbackOptions)
    CallbackEventHandler.UnregisterAllRemovableCallbacks()
    CallbackEventHandler.HasTrickleDownHandlers()
    CallbackEventHandler.HasBubbleUpHandlers()
    CallbackEventHandler.ExecuteDefaultActionAtTarget(EventBase)
    CallbackEventHandler.HandleEventBubbleUp(EventBase)
    CallbackEventHandler.HandleEventTrickleDown(EventBase)
    CallbackEventHandler.ExecuteDefaultAction(EventBase)
    CallbackEventHandler.NotifyPropertyChanged(in BindingId)
    object.Equals(object)
    object.Equals(object, object)
    object.GetHashCode()
    object.GetType()
    object.MemberwiseClone()
    object.ReferenceEquals(object, object)
    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
    VisualElement.contentContainer

    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.

    Implements

    IEventHandler
    IResolvedStyle
    ITransform
    ITransitionAnimations
    IExperimentalFeatures
    IVisualElementScheduler
    IContextOverrideElement
    IAdditionalDataHolder
    INotifyValueChanged<T>

    Extension Methods

    VisualElementExtensions.FindNavController(VisualElement)
    RegisterUpdateCallbackExtensions.RegisterUpdateCallback(VisualElement, EventCallback<UpdateEvent>)
    RegisterUpdateCallbackExtensions.UnregisterUpdateCallback(VisualElement, EventCallback<UpdateEvent>)
    VisualElementExtensions.EnableDynamicTransform(VisualElement, bool)
    VisualElementExtensions.EnablePicking(VisualElement, bool)
    VisualElementExtensions.GetChildren<T>(VisualElement, bool)
    VisualElementExtensions.GetContextProvider<T>(VisualElement)
    VisualElementExtensions.GetContextProvider<T>(VisualElement, string)
    VisualElementExtensions.GetContext<T>(VisualElement)
    VisualElementExtensions.GetContext<T>(VisualElement, string)
    VisualElementExtensions.GetExclusiveRootElement(VisualElement)
    VisualElementExtensions.GetLastAncestorOfType<T>(VisualElement)
    VisualElementExtensions.GetPreferredTooltipPlacement(VisualElement)
    VisualElementExtensions.GetSelfContext<T>(VisualElement)
    VisualElementExtensions.GetSelfContext<T>(VisualElement, string)
    VisualElementExtensions.GetTooltipContent(VisualElement)
    VisualElementExtensions.GetTooltipTemplate(VisualElement)
    VisualElementExtensions.HasAncestorOfType<T>(VisualElement, bool)
    VisualElementExtensions.HasAncestorsOfType<T>(VisualElement)
    VisualElementExtensions.IsContextProvider<T>(VisualElement)
    VisualElementExtensions.IsContextProvider<T>(VisualElement, string)
    VisualElementExtensions.IsInvisible(VisualElement)
    VisualElementExtensions.IsOnScreen(VisualElement)
    VisualElementExtensions.ProvideContext<T>(VisualElement, string, T)
    VisualElementExtensions.ProvideContext<T>(VisualElement, T)
    VisualElementExtensions.RegisterContextChangedCallback<T>(VisualElement, string, EventCallback<ContextChangedEvent<T>>)
    VisualElementExtensions.RegisterContextChangedCallback<T>(VisualElement, EventCallback<ContextChangedEvent<T>>)
    VisualElementExtensions.SetPreferredTooltipPlacement(VisualElement, OptionalEnum<PopoverPlacement>)
    VisualElementExtensions.SetTooltipContent(VisualElement, VisualElementExtensions.TooltipContentCallback)
    VisualElementExtensions.SetTooltipTemplate(VisualElement, VisualElement)
    VisualElementExtensions.UnregisterContextChangedCallback<T>(VisualElement, string, EventCallback<ContextChangedEvent<T>>)
    VisualElementExtensions.UnregisterContextChangedCallback<T>(VisualElement, EventCallback<ContextChangedEvent<T>>)
    In This Article
    Back to top
    Copyright © 2026 Unity Technologies — Trademarks and terms of use
    • Legal
    • Privacy Policy
    • Cookie Policy
    • Do Not Sell or Share My Personal Information
    • Your Privacy Choices (Cookie Settings)