docs.unity3d.com
    Show / Hide Table of Contents

    Class Touchscreen

    A multi-touch surface.

    Inheritance
    Object
    InputControl
    InputDevice
    Pointer
    Touchscreen
    Inherited Members
    Pointer.position
    Pointer.delta
    Pointer.radius
    Pointer.pressure
    Pointer.press
    InputDevice.InvalidDeviceId
    InputDevice.description
    InputDevice.enabled
    InputDevice.canRunInBackground
    InputDevice.added
    InputDevice.remote
    InputDevice.native
    InputDevice.updateBeforeRender
    InputDevice.deviceId
    InputDevice.lastUpdateTime
    InputDevice.wasUpdatedThisFrame
    InputDevice.allControls
    InputDevice.valueType
    InputDevice.valueSizeInBytes
    InputDevice.ReadValueFromBufferAsObject(Void*, Int32)
    InputDevice.ReadValueFromStateAsObject(Void*)
    InputDevice.ReadValueFromStateIntoBuffer(Void*, Void*, Int32)
    InputDevice.CompareValue(Void*, Void*)
    InputDevice.OnAdded()
    InputDevice.OnConfigurationChanged()
    InputDevice.ExecuteCommand<TCommand>(TCommand)
    InputDevice.ExecuteCommand(InputDeviceCommand*)
    InputControl.name
    InputControl.displayName
    InputControl.shortDisplayName
    InputControl.path
    InputControl.layout
    InputControl.variants
    InputControl.device
    InputControl.parent
    InputControl.children
    InputControl.usages
    InputControl.aliases
    InputControl.stateBlock
    InputControl.noisy
    InputControl.synthetic
    InputControl.Item[String]
    InputControl.ToString()
    InputControl.EvaluateMagnitude()
    InputControl.EvaluateMagnitude(Void*)
    InputControl.WriteValueFromBufferIntoState(Void*, Int32, Void*)
    InputControl.WriteValueFromObjectIntoState(Object, Void*)
    InputControl.TryGetChildControl(String)
    InputControl.TryGetChildControl<TControl>(String)
    InputControl.GetChildControl(String)
    InputControl.GetChildControl<TControl>(String)
    InputControl.RefreshConfigurationIfNeeded()
    InputControl.RefreshConfiguration()
    InputControl.m_StateBlock
    InputControl.currentStatePtr
    InputControl.previousFrameStatePtr
    InputControl.defaultStatePtr
    InputControl.noiseMaskPtr
    InputControl.stateOffsetRelativeToDeviceRoot
    Namespace: UnityEngine.InputSystem
    Syntax
    public class Touchscreen : Pointer, IInputStateCallbackReceiver, IEventMerger, ICustomDeviceReset
    Remarks

    Touchscreen is somewhat different from most other device implementations in that it does not usually consume input in the form of a full device snapshot but rather consumes input sent to it in the form of events containing a TouchState each. This is unusual as TouchState uses a memory format different from Format. However, when a Touchscreen sees an event containing a TouchState, it will handle that event on a special code path.

    This allows Touchscreen to decide on its own which control in touches to store a touch at and to perform things such as tap detection (see tap and tapCount) and primary touch handling (see primaryTouch).

    // Create a touchscreen device.
    var touchscreen = InputSystem.AddDevice<Touchscreen>();
    
    // Send a touch to the device.
    InputSystem.QueueStateEvent(touchscreen,
        new TouchState
        {
            phase = TouchPhase.Began,
            // Must have a valid, non-zero touch ID. Touchscreen will not operate
            // correctly if we don't set IDs properly.
            touchId = 1,
            position = new Vector2(123, 234),
            // Delta will be computed by Touchscreen automatically.
        });

    Note that this class presents a fairly low-level touch API. When working with touch from script code, it is recommended to use the higher-level Touch API instead.

    Properties

    current

    The touchscreen that was added or updated last or null if there is no touchscreen connected to the system.

    Declaration
    public static Touchscreen current { get; }
    Property Value
    Type Description
    Touchscreen

    Current touch screen.

    primaryTouch

    Synthetic control that has the data for the touch that is deemed the "primary" touch at the moment.

    Declaration
    public TouchControl primaryTouch { get; protected set; }
    Property Value
    Type Description
    TouchControl

    Control tracking the screen's primary touch.

    Remarks

    This touch duplicates touch data from whichever touch is deemed the primary touch at the moment. When going from no fingers down to any finger down, the first finger to touch the screen is deemed the "primary touch". It stays the primary touch until the last finger is released.

    Note that unlike the touch from which it originates, the primary touch will be kept ongoing for as long as there is still a finger on the screen. Put another way, phase of primaryTouch will only transition to Ended once the last finger has been lifted off the screen.

    touchControlArray

    Declaration
    protected TouchControl[] touchControlArray { get; set; }
    Property Value
    Type Description
    TouchControl[]

    touches

    Array of all TouchControls on the device.

    Declaration
    public ReadOnlyArray<TouchControl> touches { get; protected set; }
    Property Value
    Type Description
    ReadOnlyArray<TouchControl>

    All TouchControls on the screen.

    Remarks

    By default, a touchscreen will allocate 10 touch controls. This can be changed by modifying the "Touchscreen" layout itself or by derived layouts. In practice, this means that this array will usually have a fixed length of 10 entries but it may deviate from that.

    Methods

    FinishSetup()

    Perform final initialization tasks after the control hierarchy has been put into place.

    Declaration
    protected override void FinishSetup()
    Overrides
    Pointer.FinishSetup()
    Remarks

    This method can be overridden to perform control- or device-specific setup work. The most common use case is for looking up child controls and storing them in local getters.

    public class MyDevice : InputDevice
    {
        public ButtonControl button { get; private set; }
        public AxisControl axis { get; private set; }
    
        protected override void OnFinishSetup()
        {
            // Cache controls in getters.
            button = GetChildControl("button");
            axis = GetChildControl("axis");
        }
    }

    MakeCurrent()

    Make this the current device of its type.

    Declaration
    public override void MakeCurrent()
    Overrides
    Pointer.MakeCurrent()
    Remarks

    This method is called automatically by the input system when a device is added or when input is received on it. Many types of devices have .current getters that allow querying the last used device of a specific type directly (for example, see current).

    There is one special case, however, related to noise. A device that has noisy controls (i.e. controls for which noisy is true) may receive input events that contain no meaningful user interaction but are simply just noise from the device. A good example of this is the PS4 gamepad which has a built-in gyro and may thus constantly feed events into the input system even if not being actually in use. If, for example, an Xbox gamepad and PS4 gamepad are both connected to a PC and the user is playing with the Xbox gamepad, the PS4 gamepad would still constantly make itself current by simply flooding the system with events. Hence why by default, noise on .current getters will be filtered out and a device will only see MakeCurrent getting called if there input was detected on non-noisy controls.

    See Also
    current
    current
    current
    current

    OnNextUpdate()

    Declaration
    protected void OnNextUpdate()

    OnRemoved()

    Called by the system when the device is removed from devices.

    Declaration
    protected override void OnRemoved()
    Overrides
    Pointer.OnRemoved()
    Remarks

    This is called after the device has already been removed.

    See Also
    devices
    Removed
    OnRemoved()

    OnStateEvent(InputEventPtr)

    Called whenever a new state event is received.

    Declaration
    protected void OnStateEvent(InputEventPtr eventPtr)
    Parameters
    Type Name Description
    InputEventPtr eventPtr

    Explicit Interface Implementations

    IInputStateCallbackReceiver.GetStateOffsetForEvent(InputControl, InputEventPtr, ref UInt32)

    Declaration
    bool IInputStateCallbackReceiver.GetStateOffsetForEvent(InputControl control, InputEventPtr eventPtr, ref uint offset)
    Parameters
    Type Name Description
    InputControl control
    InputEventPtr eventPtr
    UInt32 offset
    Returns
    Type Description
    Boolean
    Implements
    IInputStateCallbackReceiver.GetStateOffsetForEvent(InputControl, InputEventPtr, ref UInt32)

    IInputStateCallbackReceiver.OnNextUpdate()

    Declaration
    void IInputStateCallbackReceiver.OnNextUpdate()
    Implements
    IInputStateCallbackReceiver.OnNextUpdate()

    IInputStateCallbackReceiver.OnStateEvent(InputEventPtr)

    Declaration
    void IInputStateCallbackReceiver.OnStateEvent(InputEventPtr eventPtr)
    Parameters
    Type Name Description
    InputEventPtr eventPtr
    Implements
    IInputStateCallbackReceiver.OnStateEvent(InputEventPtr)

    Extension Methods

    InputControlExtensions.FindInParentChain<TControl>(InputControl)
    InputControlExtensions.IsPressed(InputControl, Single)
    InputControlExtensions.IsActuated(InputControl, Single)
    InputControlExtensions.ReadValueAsObject(InputControl)
    InputControlExtensions.ReadValueIntoBuffer(InputControl, Void*, Int32)
    InputControlExtensions.ReadDefaultValueAsObject(InputControl)
    InputControlExtensions.ReadValueFromEventAsObject(InputControl, InputEventPtr)
    InputControlExtensions.WriteValueFromObjectIntoEvent(InputControl, InputEventPtr, Object)
    InputControlExtensions.WriteValueIntoState(InputControl, Void*)
    InputControlExtensions.WriteValueIntoState<TValue>(InputControl, TValue, Void*)
    InputControlExtensions.WriteValueIntoEvent<TValue>(InputControl, TValue, InputEventPtr)
    InputControlExtensions.CopyState(InputDevice, Void*, Int32)
    InputControlExtensions.CopyState<TState>(InputDevice, out TState)
    InputControlExtensions.CheckStateIsAtDefault(InputControl)
    InputControlExtensions.CheckStateIsAtDefault(InputControl, Void*, Void*)
    InputControlExtensions.CheckStateIsAtDefaultIgnoringNoise(InputControl)
    InputControlExtensions.CheckStateIsAtDefaultIgnoringNoise(InputControl, Void*)
    InputControlExtensions.CompareStateIgnoringNoise(InputControl, Void*)
    InputControlExtensions.CompareState(InputControl, Void*, Void*, Void*)
    InputControlExtensions.CompareState(InputControl, Void*, Void*)
    InputControlExtensions.HasValueChangeInState(InputControl, Void*)
    InputControlExtensions.HasValueChangeInEvent(InputControl, InputEventPtr)
    InputControlExtensions.GetStatePtrFromStateEvent(InputControl, InputEventPtr)
    InputControlExtensions.ResetToDefaultStateInEvent(InputControl, InputEventPtr)
    InputControlExtensions.FindControlsRecursive<TControl>(InputControl, IList<TControl>, Func<TControl, Boolean>)
    Back to top
    Terms of use
    Copyright © 2023 Unity Technologies — Terms of use
    • Legal
    • Privacy Policy
    • Cookies
    • Do Not Sell or Share My Personal Information
    • Your Privacy Choices (Cookie Settings)
    "Unity", Unity logos, and other Unity trademarks are trademarks or registered trademarks of Unity Technologies or its affiliates in the U.S. and elsewhere (more info here). Other names or brands are trademarks of their respective owners.
    Generated by DocFX on 18 October 2023