docs.unity3d.com
Search Results for

    Show / Hide Table of Contents

    Class InputStateHistory

    Record a history of state changes applied to one or more controls.

    Inheritance
    object
    InputStateHistory
    InputStateHistory<TValue>
    Implements
    IDisposable
    IEnumerable<InputStateHistory.Record>
    IEnumerable
    IInputStateChangeMonitor
    Namespace: UnityEngine.InputSystem.LowLevel
    Assembly: Unity.InputSystem.dll
    Syntax
    public class InputStateHistory : IDisposable, IEnumerable<InputStateHistory.Record>, IEnumerable, IInputStateChangeMonitor
    Remarks

    This class makes it easy to track input values over time. It will automatically retain input state up to a given maximum history depth (historyDepth). When the history is full, it will start overwriting the oldest entry each time a new history record is received.

    The class listens to changes on the given controls by adding change monitors (IInputStateChangeMonitor) to each control.

    Examples
    // Track all stick controls in the system.
    var history = new InputStateHistory<Vector2>("*/<Stick>");
    foreach (var control in history.controls)
        Debug.Log("Capturing input on " + control);
    
    // Start capturing.
    history.StartRecording();
    
    // Perform a couple artificial value changes.
    Gamepad.current.leftStick.QueueValueChange(new Vector2(0.123f, 0.234f));
    Gamepad.current.leftStick.QueueValueChange(new Vector2(0.234f, 0.345f));
    Gamepad.current.leftStick.QueueValueChange(new Vector2(0.345f, 0.456f));
    InputSystem.Update();
    
    // Every value change will be visible in the history.
    foreach (var record in history)
        Debug.Log($"{record.control} changed value to {record.ReadValue()}");
    
    // Histories allocate unmanaged memory and must be disposed of in order to not leak.
    history.Dispose();

    Constructors

    InputStateHistory(IEnumerable<InputControl>)

    Creates a new InputStateHistory class to record state changes for a specified controls.

    Declaration
    public InputStateHistory(IEnumerable<InputControl> controls)
    Parameters
    Type Name Description
    IEnumerable<InputControl> controls

    Controls to monitor.

    Remarks

    Creates a new InputStateHistory to record a history of state changes for the specified controls.

    See Also
    InputStateHistory.Record

    InputStateHistory(int)

    Creates a new InputStateHistory class to record all control state changes.

    Declaration
    public InputStateHistory(int maxStateSizeInBytes)
    Parameters
    Type Name Description
    int maxStateSizeInBytes

    Maximum size of control state in the record entries. Controls with larger state will not be recorded.

    Remarks

    Creates a new InputStateHistory to record a history of control state changes.

    New controls are automatically added into the state history if their state is smaller than the threshold.

    See Also
    InputStateHistory.Record

    InputStateHistory(string)

    Creates a new InputStateHistory class to record state changes for a specified control.

    Declaration
    public InputStateHistory(string path)
    Parameters
    Type Name Description
    string path

    Control path to identify which controls to monitor.

    Remarks

    Creates a new InputStateHistory to record a history of state changes for the specified controls.

    Examples
    // Track all stick controls in the system.
    var history = new InputStateHistory("*/<Stick>");
    See Also
    InputStateHistory.Record

    InputStateHistory(InputControl)

    Creates a new InputStateHistory class to record state changes for a specified control.

    Declaration
    public InputStateHistory(InputControl control)
    Parameters
    Type Name Description
    InputControl control

    Control to monitor.

    Remarks

    Creates a new InputStateHistory to record a history of state changes for the specified control.

    See Also
    InputStateHistory.Record

    Properties

    Count

    Total number of state records currently captured in the history.

    Declaration
    public int Count { get; }
    Property Value
    Type Description
    int
    Remarks

    Number of records in the collection.

    This will always be at most historyDepth. To record a change use RecordStateChange(InputControl, InputEventPtr).

    See Also
    InputStateHistory.Record

    this[int]

    Returns an entry in the state history at the given index.

    Declaration
    public InputStateHistory.Record this[int index] { get; set; }
    Parameters
    Type Name Description
    int index

    Index into the array.

    Property Value
    Type Description
    InputStateHistory.Record
    Remarks

    Returns a InputStateHistory.Record entry from the state history at the given index.

    Exceptions
    Type Condition
    IndexOutOfRangeException

    index is less than 0 or greater than Count.

    See Also
    InputStateHistory.Record

    controls

    List of input controls the state history will be recording for.

    Declaration
    public ReadOnlyArray<InputControl> controls { get; }
    Property Value
    Type Description
    ReadOnlyArray<InputControl>
    Remarks

    The list of input controls the state history will be recording for is specified on construction of the InputStateHistory

    See Also
    InputStateHistory.Record

    extraMemoryPerRecord

    Size of additional data storage to allocate per record.

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

    Additional custom data can be stored per record up to the size of this value. To retrieve a pointer to this memory use GetUnsafeExtraMemoryPtr() Used by history

    Exceptions
    Type Condition
    ArgumentException

    value is negative.

    See Also
    InputStateHistory.Record

    historyDepth

    Maximum number of records that can be recorded in the history.

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

    Upper limit on number of records. A fixed size memory block of unmanaged memory will be allocated to store history records. When the history is full, it will start overwriting the oldest entry each time a new history record is received.

    Exceptions
    Type Condition
    ArgumentException

    value is negative.

    See Also
    InputStateHistory.Record

    onRecordAdded

    Optional delegate to perform when a record is added to the history array.

    Declaration
    public Action<InputStateHistory.Record> onRecordAdded { get; set; }
    Property Value
    Type Description
    Action<InputStateHistory.Record>
    Remarks

    Can be used to fill in the extra memory with custom data using GetUnsafeExtraMemoryPtr()

    See Also
    InputStateHistory.Record

    onShouldRecordStateChange

    Optional delegate to decide whether the state change should be stored in the history.

    Declaration
    public Func<InputControl, double, InputEventPtr, bool> onShouldRecordStateChange { get; set; }
    Property Value
    Type Description
    Func<InputControl, double, InputEventPtr, bool>
    Remarks

    Can be used to filter out some events to focus on recording the ones you are most interested in.

    If the callback returns true, a record will be added to the history If the callback returns false, the event will be ignored and not recorded.

    See Also
    InputStateHistory.Record

    updateMask

    Specify which player loop positions the state history will be monitored for.

    Declaration
    public InputUpdateType updateMask { get; set; }
    Property Value
    Type Description
    InputUpdateType
    Remarks

    The state history will only be monitored for the specified player loop positions. Editor is excluded from this list

    Exceptions
    Type Condition
    ArgumentException

    valueWhen an invalid mask is provided (e.g. None).

    See Also
    InputStateHistory.Record

    version

    Current version stamp. Every time a record is stored in the history, this is incremented by one.

    Declaration
    public uint version { get; }
    Property Value
    Type Description
    uint
    Remarks

    Version stamp that indicates the number of mutations. To record a change use RecordStateChange(InputControl, InputEventPtr).

    See Also
    InputStateHistory.Record

    Methods

    AddRecord(Record)

    Add a record to the input state history.

    Declaration
    public InputStateHistory.Record AddRecord(InputStateHistory.Record record)
    Parameters
    Type Name Description
    InputStateHistory.Record record

    Record to add.

    Returns
    Type Description
    InputStateHistory.Record

    The newly added record from the history array (as a copy is made).

    Remarks

    Add a record to the input state history. Allocates an entry in the history array and returns this copy of the original data passed to the function.

    See Also
    InputStateHistory.Record

    AllocateRecord(out int)

    Allocate a new record in the input state history.

    Declaration
    protected InputStateHistory.RecordHeader* AllocateRecord(out int index)
    Parameters
    Type Name Description
    int index

    The index of the newly created record

    Returns
    Type Description
    RecordHeader*

    The header of the newly created record

    Remarks

    Allocate a new record in the input state history.

    See Also
    InputStateHistory.Record

    Clear()

    Clear the history record.

    Declaration
    public void Clear()
    Remarks

    Clear the history record. Resetting the list to empty.

    This won't clear controls that have been added on the fly.

    See Also
    InputStateHistory.Record

    Destroy()

    Destroy the state history records.

    Declaration
    protected void Destroy()
    Remarks

    Deletes the state history records.

    See Also
    InputStateHistory.Record

    Dispose()

    Dispose of the state history records.

    Declaration
    public void Dispose()
    Remarks

    Stops recording and cleans up the state history

    See Also
    InputStateHistory.Record

    ~InputStateHistory()

    InputStateHistory destructor.

    Declaration
    protected ~InputStateHistory()
    See Also
    InputStateHistory.Record

    GetEnumerator()

    Enumerate all state history records.

    Declaration
    public IEnumerator<InputStateHistory.Record> GetEnumerator()
    Returns
    Type Description
    IEnumerator<InputStateHistory.Record>

    An enumerator going over the state history records.

    Remarks

    Enumerate all state history records.

    See Also
    GetEnumerator()

    GetRecord(int)

    Retrieve a record from the input state history.

    Declaration
    protected InputStateHistory.RecordHeader* GetRecord(int index)
    Parameters
    Type Name Description
    int index

    Record index into the input state history records buffer.

    Returns
    Type Description
    RecordHeader*

    The record header for the specified index

    Remarks

    Retrieve a record from the input state history by Record index.

    Exceptions
    Type Condition
    InvalidOperationException

    When the buffer is no longer valid as it has been disposed.

    ArgumentOutOfRangeException

    If the index is out of range of the history depth.

    See Also
    InputStateHistory.Record

    ReadValueAsObject(RecordHeader*)

    Read the control's final, processed value from the given state and return the value as an object.

    Declaration
    protected object ReadValueAsObject(InputStateHistory.RecordHeader* data)
    Parameters
    Type Name Description
    RecordHeader* data

    The record header to query.

    Returns
    Type Description
    object

    The value of the control associated with the record header.

    Remarks

    This method allocates GC memory and should not be used during normal gameplay operation.

    Exceptions
    Type Condition
    InvalidOperationException

    When the specified value is not present.

    See Also
    InputStateHistory.Record

    ReadValue<TValue>(RecordHeader*)

    Returns value from the control in the specified record header.

    Declaration
    protected TValue ReadValue<TValue>(InputStateHistory.RecordHeader* data) where TValue : struct
    Parameters
    Type Name Description
    RecordHeader* data

    The record header to query.

    Returns
    Type Description
    TValue

    The value from the record.

    Type Parameters
    Name Description
    TValue

    The type of the value being read

    Exceptions
    Type Condition
    InvalidOperationException

    When the record is no longer value or the specified type is not present.

    See Also
    InputStateHistory.Record

    RecordIndexToUserIndex(int)

    Remap a records internal index to an index from the start of the recording in the circular buffer.

    Declaration
    protected int RecordIndexToUserIndex(int index)
    Parameters
    Type Name Description
    int index

    Record index (from the start of the record array).

    Returns
    Type Description
    int

    An index relative to the start of the recording in the circular buffer.

    Remarks

    Remap a records internal index, which is relative to the start of the record buffer, to an index relative to the start of the recording in the circular buffer.

    See Also
    InputStateHistory.Record

    RecordStateChange(InputControl, void*, double)

    Record a state change for a specific control.

    Declaration
    public InputStateHistory.Record RecordStateChange(InputControl control, void* statePtr, double time)
    Parameters
    Type Name Description
    InputControl control

    The control to record the state change for.

    void* statePtr

    The current state data to record.

    double time

    Time stamp to apply (overriding the event timestamp)

    Returns
    Type Description
    InputStateHistory.Record

    The newly added record.

    Remarks

    Record a state change for a specific control. Will call the onRecordAdded delegate after adding the record. Note this does not call the onShouldRecordStateChange delegate.

    See Also
    InputStateHistory.Record

    RecordStateChange(InputControl, InputEventPtr)

    Record a state change for a specific control.

    Declaration
    public InputStateHistory.Record RecordStateChange(InputControl control, InputEventPtr eventPtr)
    Parameters
    Type Name Description
    InputControl control

    The control to record the state change for.

    InputEventPtr eventPtr

    The current event data to record.

    Returns
    Type Description
    InputStateHistory.Record

    The newly added record.

    Remarks

    Record a state change for a specific control. Will call the onRecordAdded delegate after adding the record. Note this does not call the onShouldRecordStateChange delegate.

    See Also
    InputStateHistory.Record

    StartRecording()

    Start recording state history for the specified controls.

    Declaration
    public void StartRecording()
    Remarks

    Start recording state history for the controls specified in the InputStateHistory constructor.

    Examples
    using (var allTouchTaps = new InputStateHistory("<Touchscreen>/touch*/tap"))
    {
        allTouchTaps.StartRecording();
        allTouchTaps.StopRecording();
    }
    See Also
    InputStateHistory.Record

    StopRecording()

    Stop recording state history for the specified controls.

    Declaration
    public void StopRecording()
    Remarks

    Stop recording state history for the controls specified in the InputStateHistory constructor.

    Examples
    using (var allTouchTaps = new InputStateHistory("<Touchscreen>/touch*/tap"))
    {
        allTouchTaps.StartRecording();
        allTouchTaps.StopRecording();
    }
    See Also
    InputStateHistory.Record

    UserIndexToRecordIndex(int)

    Remap an index from the start of the recording in the circular buffer to a records internal index.

    Declaration
    protected int UserIndexToRecordIndex(int index)
    Parameters
    Type Name Description
    int index

    An index relative to the start of the recording in the circular buffer.

    Returns
    Type Description
    int

    Record index (from the start of the record array).

    Remarks

    Remap an index relative to the start of the recording in the circular buffer, to a records internal index, which is relative to the start of the record buffer.

    See Also
    InputStateHistory.Record

    Implements

    IDisposable
    IEnumerable<T>
    IEnumerable
    IInputStateChangeMonitor

    See Also

    InputStateHistory.Record

    Did you find this page useful? Please give it a rating:

    Thanks for rating this page!

    Report a problem on this page

    What kind of problem would you like to report?

    • This page needs code samples
    • Code samples do not work
    • Information is missing
    • Information is incorrect
    • Information is unclear or confusing
    • There is a spelling/grammar error on this page
    • Something else

    Thanks for letting us know! This page has been marked for review based on your feedback.

    If you have time, you can provide more information to help us fix the problem faster.

    Provide more information

    You've told us this page needs code samples. If you'd like to help us further, you could provide a code sample, or tell us about what kind of code sample you'd like to see:

    You've told us there are code samples on this page which don't work. If you know how to fix it, or have something better we could use instead, please let us know:

    You've told us there is information missing from this page. Please tell us more about what's missing:

    You've told us there is incorrect information on this page. If you know what we should change to make it correct, please tell us:

    You've told us this page has unclear or confusing information. Please tell us more about what you found unclear or confusing, or let us know how we could make it clearer:

    You've told us there is a spelling or grammar error on this page. Please tell us what's wrong:

    You've told us this page has a problem. Please tell us more about what's wrong:

    Thank you for helping to make the Unity documentation better!

    Your feedback has been submitted as a ticket for our documentation team to review.

    We are not able to reply to every ticket submitted.

    In This Article
    • Constructors
      • InputStateHistory(IEnumerable<InputControl>)
      • InputStateHistory(int)
      • InputStateHistory(string)
      • InputStateHistory(InputControl)
    • Properties
      • Count
      • this[int]
      • controls
      • extraMemoryPerRecord
      • historyDepth
      • onRecordAdded
      • onShouldRecordStateChange
      • updateMask
      • version
    • Methods
      • AddRecord(Record)
      • AllocateRecord(out int)
      • Clear()
      • Destroy()
      • Dispose()
      • ~InputStateHistory()
      • GetEnumerator()
      • GetRecord(int)
      • ReadValueAsObject(RecordHeader*)
      • ReadValue<TValue>(RecordHeader*)
      • RecordIndexToUserIndex(int)
      • RecordStateChange(InputControl, void*, double)
      • RecordStateChange(InputControl, InputEventPtr)
      • StartRecording()
      • StopRecording()
      • UserIndexToRecordIndex(int)
    • Implements
    • See Also
    Back to top
    Copyright © 2025 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)