docs.unity3d.com
Search Results for

    Show / Hide Table of Contents

    Struct GamepadState

    Default state layout for gamepads.

    Implements
    IInputStateTypeInfo
    Inherited Members
    ValueType.Equals(object)
    ValueType.GetHashCode()
    ValueType.ToString()
    Namespace: UnityEngine.InputSystem.LowLevel
    Assembly: Unity.InputSystem.dll
    Syntax
    public struct GamepadState : IInputStateTypeInfo
    Remarks

    Be aware that unlike some other devices such as Mouse or Touchscreen, gamepad devices tend to have wildly varying state formats, i.e. forms in which they internally store their input data. In practice, even on the same platform gamepads will often store their data in different formats. This means that GamepadState will often not be the format in which a particular gamepad (such as XInputController, for example) stores its data.

    If your gamepad data is arriving in a different format, you should extend the "Gamepad" layout and customize its Controls.

    A real-world example of this is the Xbox Controller on macOS, which is supported through HID. Its layout looks like this:

    {
        "name" : "XboxGamepadOSX",
        "extend" : "Gamepad",
        "format" : "HID",
        "device" : { "interface" : "HID", "product" : "Xbox.*Controller" },
        "controls" : [
            { "name" : "leftShoulder", "offset" : 2, "bit" : 8 },
            { "name" : "rightShoulder", "offset" : 2, "bit" : 9 },
            { "name" : "leftStickPress", "offset" : 2, "bit" : 14 },
            { "name" : "rightStickPress", "offset" : 2, "bit" : 15 },
            { "name" : "buttonSouth", "offset" : 2, "bit" : 12 },
            { "name" : "buttonEast", "offset" : 2, "bit" : 13 },
            { "name" : "buttonWest", "offset" : 2, "bit" : 14 },
            { "name" : "buttonNorth", "offset" : 2, "bit" : 15 },
            { "name" : "dpad", "offset" : 2 },
            { "name" : "dpad/up", "offset" : 0, "bit" : 8 },
            { "name" : "dpad/down", "offset" : 0, "bit" : 9 },
            { "name" : "dpad/left", "offset" : 0, "bit" : 10 },
            { "name" : "dpad/right", "offset" : 0, "bit" : 11 },
            { "name" : "start", "offset" : 2, "bit" : 4 },
            { "name" : "select", "offset" : 2, "bit" : 5 },
            { "name" : "xbox", "offset" : 2, "bit" : 2, "layout" : "Button" },
            { "name" : "leftTrigger", "offset" : 4, "format" : "BYTE" },
            { "name" : "rightTrigger", "offset" : 5, "format" : "BYTE" },
            { "name" : "leftStick", "offset" : 6, "format" : "VC2S" },
            { "name" : "leftStick/x", "offset" : 0, "format" : "SHRT", "parameters" : "normalize,normalizeMin=-0.5,normalizeMax=0.5" },
            { "name" : "leftStick/y", "offset" : 2, "format" : "SHRT", "parameters" : "invert,normalize,normalizeMin=-0.5,normalizeMax=0.5" },
            { "name" : "rightStick", "offset" : 10, "format" : "VC2S" },
            { "name" : "rightStick/x", "offset" : 0, "format" : "SHRT", "parameters" : "normalize,normalizeMin=-0.5,normalizeMax=0.5" },
            { "name" : "rightStick/y", "offset" : 2, "format" : "SHRT", "parameters" : "invert,normalize,normalizeMin=-0.5,normalizeMax=0.5" }
        ]
    }

    The same principle applies if some buttons on your Device are swapped, for example. In this case, you can remap their offsets.

    Constructors

    GamepadState(params GamepadButton[])

    Create a gamepad state with the given buttons being pressed.

    Declaration
    public GamepadState(params GamepadButton[] buttons)
    Parameters
    Type Name Description
    GamepadButton[] buttons

    Buttons to put into pressed state.

    Exceptions
    Type Condition
    ArgumentNullException

    buttons is null.

    See Also
    Gamepad

    Fields

    buttons

    Button bit mask.

    Declaration
    public uint buttons
    Field Value
    Type Description
    uint

    Button bit mask.

    See Also
    GamepadButton
    buttonSouth
    buttonNorth
    buttonWest
    buttonSouth
    leftShoulder
    rightShoulder
    startButton
    selectButton
    leftStickButton
    rightStickButton

    leftStick

    Left stick position. Each axis goes from -1 to 1 with 0 being center position.

    Declaration
    public Vector2 leftStick
    Field Value
    Type Description
    Vector2

    Left stick position.

    See Also
    leftStick

    leftTrigger

    Position of the left trigger. Goes from 0 (not pressed) to 1 (fully pressed).

    Declaration
    public float leftTrigger
    Field Value
    Type Description
    float

    Position of left trigger.

    See Also
    leftTrigger

    rightStick

    Right stick position. Each axis from -1 to 1 with 0 being center position.

    Declaration
    public Vector2 rightStick
    Field Value
    Type Description
    Vector2

    Right stick position.

    See Also
    rightStick

    rightTrigger

    Position of the right trigger. Goes from 0 (not pressed) to 1 (fully pressed).

    Declaration
    public float rightTrigger
    Field Value
    Type Description
    float

    Position of right trigger.

    See Also
    rightTrigger

    Properties

    Format

    Declaration
    public static FourCC Format { get; }
    Property Value
    Type Description
    FourCC
    See Also
    Gamepad

    format

    State format tag for GamepadState.

    Declaration
    public FourCC format { get; }
    Property Value
    Type Description
    FourCC

    Returns "GPAD".

    See Also
    Gamepad

    Methods

    WithButton(GamepadButton, bool)

    Set the specific buttons to be pressed or unpressed.

    Declaration
    public GamepadState WithButton(GamepadButton button, bool value = true)
    Parameters
    Type Name Description
    GamepadButton button

    A gamepad button.

    bool value

    Whether to set button to be pressed or not pressed in buttons.

    Returns
    Type Description
    GamepadState

    GamepadState with a modified buttons mask.

    See Also
    Gamepad

    Implements

    IInputStateTypeInfo

    See Also

    Gamepad

    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
      • GamepadState(params GamepadButton[])
    • Fields
      • buttons
      • leftStick
      • leftTrigger
      • rightStick
      • rightTrigger
    • Properties
      • Format
      • format
    • Methods
      • WithButton(GamepadButton, bool)
    • Implements
    • See Also
    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)