Class ScrollTextArea
A pixel-accurate scrolling text area with iOS-style momentum physics and elastic overshoot bounce, intended for log viewers, chat windows, and any output display that needs smooth continuous scrolling.
Inherited Members
Namespace: Scylla.Core.Util.UI
Assembly: ScyllaCore.dll
Syntax
[RequireComponent(typeof(RectTransform))]
public sealed class ScrollTextArea : MonoBehaviour
Remarks
ScrollTextArea manages a ScrollTextBuffer (ring buffer of text
lines), a ScrollPhysics object (momentum and elastic bounce), and a
TextMeshProUGUI rendering component, all wired together inside a clipped
RectMask2D viewport.
Scrolling is pixel-based: the visible window is determined by a fractional pixel offset into the content, enabling sub-line smooth scrolling. Line snapping (EnableLineSnapping) and viewport alignment (AdjustViewportToLineHeight) are optional features that constrain the scroll position to line boundaries.
Input handling is external. Call HandleDragStart(Vector2), HandleDrag(Vector2),
and HandleDragEnd(Vector2?) to connect pointer events from an
IPointerDownHandler / IDragHandler / IPointerUpHandler
wrapper. Mouse-wheel scrolling can be wired through ApplyScrollVelocity(float).
Text selection is opt-in via SelectionEnabled. Call HandleSelectionInput(Vector2, bool, bool, bool, bool) every frame to drive line-based selection. Selected text can be retrieved with SelectedText or copied to the clipboard via CopySelectionToClipboard().
Event publishing (scroll, drag-start, drag-end, and buffer events) is disabled by default for performance and can be enabled via SetEventPublishing(bool).
Fields
DEFAULT_BUFFER_CAPACITY
Default buffer capacity (maximum lines).
Declaration
public const int DEFAULT_BUFFER_CAPACITY = 1000
Field Value
| Type | Description |
|---|---|
| int |
DEFAULT_DRAG_SENSITIVITY
Default drag scroll sensitivity multiplier.
Declaration
public const float DEFAULT_DRAG_SENSITIVITY = 1
Field Value
| Type | Description |
|---|---|
| float |
DEFAULT_FONT_SIZE
Default font size for the text area.
Declaration
public const float DEFAULT_FONT_SIZE = 14
Field Value
| Type | Description |
|---|---|
| float |
DEFAULT_PADDING
Default padding around the text content (in pixels).
Declaration
public const float DEFAULT_PADDING = 10
Field Value
| Type | Description |
|---|---|
| float |
DEFAULT_SCROLLBAR_AUTO_HIDE_DELAY
Default delay before scrollbar auto-hide starts in seconds.
Declaration
public const float DEFAULT_SCROLLBAR_AUTO_HIDE_DELAY = 0.75
Field Value
| Type | Description |
|---|---|
| float |
DEFAULT_SCROLLBAR_FADE_DURATION
Default duration for scrollbar fade animation in seconds.
Declaration
public const float DEFAULT_SCROLLBAR_FADE_DURATION = 0.3
Field Value
| Type | Description |
|---|---|
| float |
DEFAULT_SCROLLBAR_MARGIN
Default margin between scrollbar and viewport edge in pixels.
Declaration
public const float DEFAULT_SCROLLBAR_MARGIN = 2
Field Value
| Type | Description |
|---|---|
| float |
DEFAULT_SCROLLBAR_MIN_THUMB_HEIGHT
Minimum height for the scrollbar thumb in pixels.
Declaration
public const float DEFAULT_SCROLLBAR_MIN_THUMB_HEIGHT = 20
Field Value
| Type | Description |
|---|---|
| float |
DEFAULT_SCROLLBAR_THUMB_COLOR
Default scrollbar thumb color (semi-transparent white).
Declaration
public static readonly Color DEFAULT_SCROLLBAR_THUMB_COLOR
Field Value
| Type | Description |
|---|---|
| Color |
DEFAULT_SCROLLBAR_TRACK_COLOR
Default scrollbar track color (fully transparent).
Declaration
public static readonly Color DEFAULT_SCROLLBAR_TRACK_COLOR
Field Value
| Type | Description |
|---|---|
| Color |
DEFAULT_SCROLLBAR_WIDTH
Default scrollbar width in pixels.
Declaration
public const float DEFAULT_SCROLLBAR_WIDTH = 6
Field Value
| Type | Description |
|---|---|
| float |
Properties
AdjustViewportToLineHeight
Gets or sets whether viewport height adjusts to be a multiple of line height.
Declaration
public bool AdjustViewportToLineHeight { get; set; }
Property Value
| Type | Description |
|---|---|
| bool |
AutoScroll
Gets or sets whether auto-scroll is enabled. When true, scrolls to bottom only if already at bottom when content is added.
Declaration
public bool AutoScroll { get; set; }
Property Value
| Type | Description |
|---|---|
| bool |
Buffer
Gets the underlying text buffer that stores and manages all content lines. Use this to access line data, content height, and buffer-level scroll bounds directly.
Declaration
public ScrollTextBuffer Buffer { get; }
Property Value
| Type | Description |
|---|---|
| ScrollTextBuffer |
BufferCapacity
Gets or sets the buffer capacity. Setting this value will recreate the buffer, clearing any existing content.
Declaration
public int BufferCapacity { get; set; }
Property Value
| Type | Description |
|---|---|
| int |
ContentHeight
Gets the total content height in pixels.
Declaration
public float ContentHeight { get; }
Property Value
| Type | Description |
|---|---|
| float |
DecelerationRate
Gets or sets the deceleration rate for momentum scrolling.
Declaration
public float DecelerationRate { get; set; }
Property Value
| Type | Description |
|---|---|
| float |
DragSensitivity
Gets or sets the drag scroll sensitivity.
Declaration
public float DragSensitivity { get; set; }
Property Value
| Type | Description |
|---|---|
| float |
ElasticDuration
Gets or sets the elastic bounce-back duration.
Declaration
public float ElasticDuration { get; set; }
Property Value
| Type | Description |
|---|---|
| float |
ElasticStrength
Gets or sets the elastic strength factor.
Declaration
public float ElasticStrength { get; set; }
Property Value
| Type | Description |
|---|---|
| float |
EnableDragScroll
Gets or sets whether drag-to-scroll is enabled.
Declaration
public bool EnableDragScroll { get; set; }
Property Value
| Type | Description |
|---|---|
| bool |
EnableLineSnapping
Gets or sets whether line snapping is enabled. When true, scroll position snaps to line boundaries when scrolling stops.
Declaration
public bool EnableLineSnapping { get; set; }
Property Value
| Type | Description |
|---|---|
| bool |
Font
Gets or sets the font asset.
Declaration
public TMP_FontAsset Font { get; set; }
Property Value
| Type | Description |
|---|---|
| TMP_FontAsset |
FontSize
Gets or sets the font size.
Declaration
public float FontSize { get; set; }
Property Value
| Type | Description |
|---|---|
| float |
ForceScrollToEnd
Gets or sets whether to always scroll to end when new content is added. When true, always scrolls to bottom regardless of current scroll position. This takes precedence over AutoScroll.
Declaration
public bool ForceScrollToEnd { get; set; }
Property Value
| Type | Description |
|---|---|
| bool |
HasSelection
Gets whether there is an active (non-empty) selection.
Declaration
public bool HasSelection { get; }
Property Value
| Type | Description |
|---|---|
| bool |
IsAtBottom
Gets whether the view is at the bottom (newest content visible).
Declaration
public bool IsAtBottom { get; }
Property Value
| Type | Description |
|---|---|
| bool |
IsAtTop
Gets whether the view is at the top (oldest content visible).
Declaration
public bool IsAtTop { get; }
Property Value
| Type | Description |
|---|---|
| bool |
IsDragging
Gets whether the user is currently dragging.
Declaration
public bool IsDragging { get; }
Property Value
| Type | Description |
|---|---|
| bool |
IsElasticBouncing
Gets whether elastic bounce animation is currently active.
Declaration
public bool IsElasticBouncing { get; }
Property Value
| Type | Description |
|---|---|
| bool |
IsMomentumScrolling
Gets whether momentum scrolling is currently active.
Declaration
public bool IsMomentumScrolling { get; }
Property Value
| Type | Description |
|---|---|
| bool |
IsScrolling
Gets whether any scrolling animation is active.
Declaration
public bool IsScrolling { get; }
Property Value
| Type | Description |
|---|---|
| bool |
LineCount
Gets the current line count in the buffer.
Declaration
public int LineCount { get; }
Property Value
| Type | Description |
|---|---|
| int |
LineHeight
Gets the calculated line height in pixels.
Declaration
public float LineHeight { get; }
Property Value
| Type | Description |
|---|---|
| float |
LineSpacing
Gets or sets the line spacing as a percentage adjustment. 0 = 100% of base line height (no adjustment). Positive values increase spacing, negative values decrease.
Declaration
public float LineSpacing { get; set; }
Property Value
| Type | Description |
|---|---|
| float |
MaxScrollPosition
Gets the maximum scroll position in pixels.
Declaration
public float MaxScrollPosition { get; }
Property Value
| Type | Description |
|---|---|
| float |
Padding
Gets or sets the padding around the text content (in pixels).
Declaration
public float Padding { get; set; }
Property Value
| Type | Description |
|---|---|
| float |
Physics
Gets the scroll physics handler that drives momentum, elastic bounce, and drag-based scrolling. Use this to read or manipulate the scroll position directly when the higher-level scroll methods are not sufficient.
Declaration
public ScrollPhysics Physics { get; }
Property Value
| Type | Description |
|---|---|
| ScrollPhysics |
RectTransform
Gets the RectTransform of this component.
Declaration
public RectTransform RectTransform { get; }
Property Value
| Type | Description |
|---|---|
| RectTransform |
ScrollPosition
Gets the current scroll position in pixels.
Declaration
public float ScrollPosition { get; }
Property Value
| Type | Description |
|---|---|
| float |
ScrollbarAutoHide
Gets or sets whether the scrollbar auto-hides when not scrolling.
Declaration
public bool ScrollbarAutoHide { get; set; }
Property Value
| Type | Description |
|---|---|
| bool |
ScrollbarAutoHideDelay
Gets or sets the delay before scrollbar auto-hide starts in seconds.
Declaration
public float ScrollbarAutoHideDelay { get; set; }
Property Value
| Type | Description |
|---|---|
| float |
ScrollbarEnabled
Gets or sets whether the scrollbar is enabled. When enabled, a visual scrollbar indicator appears on the right side of the viewport.
Declaration
public bool ScrollbarEnabled { get; set; }
Property Value
| Type | Description |
|---|---|
| bool |
ScrollbarFadeDuration
Gets or sets the duration for scrollbar fade animation in seconds.
Declaration
public float ScrollbarFadeDuration { get; set; }
Property Value
| Type | Description |
|---|---|
| float |
ScrollbarMinThumbHeight
Gets or sets the minimum scrollbar thumb height in pixels.
Declaration
public float ScrollbarMinThumbHeight { get; set; }
Property Value
| Type | Description |
|---|---|
| float |
ScrollbarThumbColor
Gets or sets the scrollbar thumb color.
Declaration
public Color ScrollbarThumbColor { get; set; }
Property Value
| Type | Description |
|---|---|
| Color |
ScrollbarTrackColor
Gets or sets the scrollbar track color. Set alpha to 0 for an invisible track.
Declaration
public Color ScrollbarTrackColor { get; set; }
Property Value
| Type | Description |
|---|---|
| Color |
ScrollbarWidth
Gets or sets the scrollbar width in pixels.
Declaration
public float ScrollbarWidth { get; set; }
Property Value
| Type | Description |
|---|---|
| float |
SelectedText
Gets the currently selected text content.
Declaration
public string SelectedText { get; }
Property Value
| Type | Description |
|---|---|
| string |
Selection
Gets the current selection range.
Declaration
public TextSelectionRange Selection { get; }
Property Value
| Type | Description |
|---|---|
| TextSelectionRange |
SelectionColor
Gets or sets the selection highlight color.
Declaration
public Color SelectionColor { get; set; }
Property Value
| Type | Description |
|---|---|
| Color |
SelectionEnabled
Gets or sets whether text selection is enabled. When enabled, users can select text by holding a modifier key (e.g., Shift) while clicking and dragging. Without the modifier, drag scrolling operates normally.
Declaration
public bool SelectionEnabled { get; set; }
Property Value
| Type | Description |
|---|---|
| bool |
TextColor
Gets or sets the text color.
Declaration
public Color TextColor { get; set; }
Property Value
| Type | Description |
|---|---|
| Color |
TextComponent
Gets the TextMeshProUGUI component used for rendering.
Declaration
public TextMeshProUGUI TextComponent { get; }
Property Value
| Type | Description |
|---|---|
| TextMeshProUGUI |
ViewportHeight
Gets the viewport height in pixels.
Declaration
public float ViewportHeight { get; }
Property Value
| Type | Description |
|---|---|
| float |
Methods
AppendLine(string)
Appends a single line of text to the buffer and updates the scroll bounds.
Scrolls to the bottom if ForceScrollToEnd is true, or if
AutoScroll is true and the view was already at the bottom.
Declaration
public void AppendLine(string text)
Parameters
| Type | Name | Description |
|---|---|---|
| string | text | The text to append. May be |
AppendLines(params string[])
Appends multiple lines of text to the buffer in a single batch and updates the scroll bounds. Scrolls to the bottom using the same auto-scroll rules as AppendLine(string).
Declaration
public void AppendLines(params string[] lines)
Parameters
| Type | Name | Description |
|---|---|---|
| string[] | lines | The lines to append. The method is a no-op if this array is |
ApplyScrollVelocity(float)
Applies velocity for momentum-based scrolling. Use this to hand off continuous scrolling to the physics system.
Declaration
public void ApplyScrollVelocity(float velocity)
Parameters
| Type | Name | Description |
|---|---|---|
| float | velocity | The velocity in pixels per second. |
Clear()
Clears all content from the buffer and resets the scroll position to the top.
Declaration
public void Clear()
ClearSelection()
Clears the current selection.
Declaration
public void ClearSelection()
CopySelectionToClipboard()
Copies selected text to the system clipboard.
Declaration
public void CopySelectionToClipboard()
ForceRefresh()
Immediately recalculates display metrics (viewport size, line height, scroll
bounds) and redraws the text content without waiting for the next Update.
Use this when external layout changes have resized the component and the next
frame's update is too late.
Declaration
public void ForceRefresh()
HandleDrag(Vector2)
Updates the drag with a new screen position.
Declaration
public void HandleDrag(Vector2 screenPosition)
Parameters
| Type | Name | Description |
|---|---|---|
| Vector2 | screenPosition | The current screen position. |
HandleDragEnd(Vector2?)
Ends the drag operation.
Declaration
public void HandleDragEnd(Vector2? screenPosition = null)
Parameters
| Type | Name | Description |
|---|---|---|
| Vector2? | screenPosition | Optional final screen position for accurate velocity calculation. |
HandleDragStart(Vector2)
Starts a drag operation at the specified screen position.
Declaration
public void HandleDragStart(Vector2 screenPosition)
Parameters
| Type | Name | Description |
|---|---|---|
| Vector2 | screenPosition | The screen position where the drag started. |
HandleSelectionInput(Vector2, bool, bool, bool, bool)
Handles selection input. Call every frame when selection is enabled. Selection is line-based: clicking and dragging with modifier key selects full lines. When modifier key is not held, drag scrolling should be used instead.
Declaration
public bool HandleSelectionInput(Vector2 mousePosition, bool leftButtonPressed, bool leftButtonPressedThisFrame, bool leftButtonReleasedThisFrame, bool modifierPressed)
Parameters
| Type | Name | Description |
|---|---|---|
| Vector2 | mousePosition | The current mouse position in screen coordinates. |
| bool | leftButtonPressed | Whether the left mouse button is currently pressed. |
| bool | leftButtonPressedThisFrame | Whether the left mouse button was pressed this frame. |
| bool | leftButtonReleasedThisFrame | Whether the left mouse button was released this frame. |
| bool | modifierPressed | Whether the selection modifier key (e.g., Shift) is held. |
Returns
| Type | Description |
|---|---|
| bool | True if selection is being handled and drag scroll should be suppressed. |
ScrollBy(float)
Scrolls by the specified number of pixels (immediate, no animation).
Declaration
public void ScrollBy(float pixels)
Parameters
| Type | Name | Description |
|---|---|---|
| float | pixels | The amount to scroll in pixels. |
ScrollTo(float, bool)
Scrolls to the specified pixel position.
Declaration
public void ScrollTo(float position, bool animate = false)
Parameters
| Type | Name | Description |
|---|---|---|
| float | position | The target scroll position in pixels. |
| bool | animate | Whether to animate the scroll. |
ScrollToBottom(bool)
Scrolls to the bottom (newest content).
Declaration
public void ScrollToBottom(bool animate = false)
Parameters
| Type | Name | Description |
|---|---|---|
| bool | animate | Whether to animate the scroll. |
ScrollToTop(bool)
Scrolls to the top (oldest content).
Declaration
public void ScrollToTop(bool animate = false)
Parameters
| Type | Name | Description |
|---|---|---|
| bool | animate | Whether to animate the scroll. |
SelectAll()
Selects all text in the buffer.
Declaration
public void SelectAll()
SetEventPublishing(bool)
Enables or disables Scylla event publishing for both the scroll area and its buffer.
When enabled, scroll position changes and drag events are published via
ScyllaEvents. Event publishing is disabled by default for performance.
Declaration
public void SetEventPublishing(bool enabled)
Parameters
| Type | Name | Description |
|---|---|---|
| bool | enabled |
|
StopScrolling()
Stops all scrolling animations and momentum.
Declaration
public void StopScrolling()
TryGetLineIndexFromScreenPoint(Vector2, out int)
Attempts to get the logical line index at a screen position. Used for line-based selection.
Declaration
public bool TryGetLineIndexFromScreenPoint(Vector2 screenPos, out int lineIndex)
Parameters
| Type | Name | Description |
|---|---|---|
| Vector2 | screenPos | The screen position. |
| int | lineIndex | The logical line index if successful. |
Returns
| Type | Description |
|---|---|
| bool | True if the conversion was successful. |