docs.unity3d.com
Search Results for

    Show / Hide Table of Contents

    Class UndoStack

    An undo stack maintains a stack of commands that have been applied in your application.

    New commands are pushed on the stack using Push(UndoCommand). Commands can be undone and redone using Undo() and Redo().

    UndoStack keeps track of the current command. This is the command which will be executed by the next call to Redo(). The index of this command is returned by index. The state of the edited object can be rolled forward or back using index. If the top-most command on the stack has already been redone, index is equal to count.

    UndoStack provides *command compression*, command *macros*, and supports the concept of a *clean state*.

    Inheritance
    object
    UndoStack
    Inherited Members
    object.Equals(object)
    object.Equals(object, object)
    object.GetHashCode()
    object.GetType()
    object.MemberwiseClone()
    object.ReferenceEquals(object, object)
    object.ToString()
    Namespace: Unity.AppUI.Undo
    Assembly: Unity.AppUI.Undo.dll
    Syntax
    public class UndoStack

    Constructors

    UndoStack()

    Default constructor.

    Declaration
    public UndoStack()

    Properties

    this[int]

    Gets the command at the given index.

    Declaration
    public UndoCommand this[int idx] { get; }
    Parameters
    Type Name Description
    int idx

    The index of the command.

    Property Value
    Type Description
    UndoCommand

    The command at the given index, or null if the index is out of range.

    canRedo

    Weather the redo stack is empty.

    Declaration
    public bool canRedo { get; }
    Property Value
    Type Description
    bool

    canUndo

    Weather the undo stack is empty.

    Declaration
    public bool canUndo { get; }
    Property Value
    Type Description
    bool

    cleanIndex

    The index of the clean state. -1 if there is no clean state.

    Declaration
    public int cleanIndex { get; }
    Property Value
    Type Description
    int

    commands

    The whole commands list.

    Declaration
    public IEnumerable<UndoCommand> commands { get; }
    Property Value
    Type Description
    IEnumerable<UndoCommand>

    count

    The number of commands in the stack. Macros are counted as a single command.

    Declaration
    public int count { get; }
    Property Value
    Type Description
    int

    index

    The index of the last command in the undo stack.

    Declaration
    public int index { get; set; }
    Property Value
    Type Description
    int

    isClean

    Weather the undo stack is in a clean state. The clean state is useful when the application supports saving and restoring the state of an object.

    Declaration
    public bool isClean { get; }
    Property Value
    Type Description
    bool

    lastCommand

    The last command in the undo stack.

    Declaration
    public UndoCommand lastCommand { get; }
    Property Value
    Type Description
    UndoCommand

    memorySize

    The total memory size of the undo stack.

    Declaration
    public ulong memorySize { get; }
    Property Value
    Type Description
    ulong

    undoLimit

    The maximum number of memory units that can be stored in the undo stack.

    Declaration
    public ulong undoLimit { get; set; }
    Property Value
    Type Description
    ulong

    Methods

    BeginMacro(string)

    Begins composition of a macro command with the given text description.

    An empty command described by the specified text is pushed on the stack. Any subsequent commands pushed on the stack will be appended to the macro command's children until EndMacro() is called.

    While a macro is being composed, the stack is disabled. This means that:
    - Events are not emitted.
    - canRedo and canUndo will always return false.
    - Calling Undo() or Redo() has no effect.
    The stack is re-enabled when the macro is ended.

    Declaration
    public void BeginMacro(string name)
    Parameters
    Type Name Description
    string name

    The name of the macro command.

    Remarks

    Nested calls to BeginMacro(string) are supported, but every call to BeginMacro(string) must have a corresponding call to EndMacro().

    Examples
    undoStack.BeginMacro("Insert Red Text");
    undoStack.Push(new InsertText(...));
    undoStack.Push(new SetRedText(...));
    undoStack.EndMacro();
    var insertRedText = new UndoCommand("Insert Text");
    new InsertText(..., insertRedText);
    new SetRedText(..., insertRedText);
    undoStack.Push(insertRedText);
    See Also
    EndMacro()

    Clear()

    Clears the undo and redo stacks.

    Declaration
    public void Clear()

    EndMacro()

    Ends composition of a macro command.
    If this is the outermost macro in a set nested macros, this function emits indexChanged once for the entire macro command.

    Declaration
    public void EndMacro()
    Exceptions
    Type Condition
    InvalidOperationException

    Thrown if no macro is being composed.

    See Also
    BeginMacro(string)

    Push(UndoCommand)

    Pushes the given command on the undo stack.

    Declaration
    public void Push(UndoCommand command)
    Parameters
    Type Name Description
    UndoCommand command

    The command to push.

    Remarks

    If the current command is a macro command, the pushed command is added to the macro.

    Redo()

    Calls Redo() on the last command in the redo stack.

    Declaration
    public void Redo()

    ResetClean()

    Sets the clean state to -1.

    Declaration
    public void ResetClean()

    SetClean()

    Sets the clean state to the last command in the undo stack.

    Declaration
    public void SetClean()

    Undo()

    Calls Undo() on the last command in the undo stack.

    Declaration
    public void Undo()

    Events

    cleanStateChanged

    Emitted when the clean state changes.

    Declaration
    public event Action cleanStateChanged
    Event Type
    Type Description
    Action

    indexChanged

    Emitted the current Command index changes.

    Declaration
    public event Action<int> indexChanged
    Event Type
    Type Description
    Action<int>
    In This Article
    Back to top
    Copyright © 2024 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)