docs.unity3d.com
Search Results for

    Show / Hide Table of Contents

    Use a custom class to create a layout

    You can create your own InputDevice class and state layouts in C# to create a custom layout as follows:

    Warning

    It looks like the sample you are looking for does not exist.

    To create an instance of your device, register it as a layout and then instantiate it:

    Warning

    It looks like the sample you are looking for does not exist.

    Use a custom class to control a complex controller

    This example workflow uses the same technique as the previous example, but provides more detail by using the PS4 DualShock controller as a more complex device to set up.

    The following example assumes that the Input System doesn't already have a custom layout for the PS4 DualShock controller, and that you want to add such a layout.

    In this example, you want to expose the controller as a gamepad and you roughly know the HID data format used by the Device.

    If you don't know the format of a given HID you want to support:

    1. Plug the device into your computer
    2. Open the Input Debugger (Window > Analysis > Input Debugger)
    3. Open both the debugger view for the device and the window showing the HID descriptor
    4. Go through the controls one by one, refer to the debug view, and correlate that to the controls in the HID descriptor.

    You can also double-click individual events and compare the raw data coming in from the device. If you select two events in the event trace, you can then right-click them and choose Compare to open a window that shows only the differences between the two events.

    Define the input data

    The first step is to describe in detail what format that input data for the device comes in, and the InputControl instances that should read out individual pieces of information from that data.

    The HID input reports from the PS4 controller look approximately like this:

    
    struct PS4InputReport
    {
        byte reportId;             // #0
        byte leftStickX;           // #1
        byte leftStickY;           // #2
        byte rightStickX;          // #3
        byte rightStickY;          // #4
        byte dpad : 4;             // #5 bit #0 (0=up, 2=right, 4=down, 6=left)
        byte squareButton : 1;     // #5 bit #4
        byte crossButton : 1;      // #5 bit #5
        byte circleButton : 1;     // #5 bit #6
        byte triangleButton : 1;   // #5 bit #7
        byte leftShoulder : 1;     // #6 bit #0
        byte rightShoulder : 1;    // #6 bit #1
        byte leftTriggerButton : 2;// #6 bit #2
        byte rightTriggerButton : 2;// #6 bit #3
        byte shareButton : 1;      // #6 bit #4
        byte optionsButton : 1;    // #6 bit #5
        byte leftStickPress : 1;   // #6 bit #6
        byte rightStickPress : 1;  // #6 bit #7
        byte psButton : 1;         // #7 bit #0
        byte touchpadPress : 1;    // #7 bit #1
        byte padding : 6;
        byte leftTrigger;          // #8
        byte rightTrigger;         // #9
    }
    
    

    You can translate this into a C# struct:

    Warning

    It looks like the sample you are looking for does not exist.

    Create an InputDevice instance to represent your device

    Next, you need an InputDevice to represent your device. Because you're dealing with a gamepad, you must create a new subclass of Gamepad.

    For simplicity, this example ignores the fact that there is a DualShockGamepad class that the actual DualShockGamepadHID is based on.

    Warning

    It looks like the sample you are looking for does not exist.

    Register your device

    The last step is to register your new type of device and set up the Input System so that when a PlayStation 4 controller is connected, the Input System generates your custom Device instead of using the default HID fallback.

    This requires a call to InputSystem.RegisterLayout<T>, giving it an InputDeviceMatcher that matches the description for a PlayStation 4 DualShock HID. In theory, you can place this call anywhere, but the best point for registering layouts is generally during startup. Doing so ensures that your custom layout is visible to the Unity Editor and therefore exposed, for example, in the Input Control picker.

    You can insert your registration into the startup sequence by modifying the code for your DualShock4GamepadHID device as follows:

    Warning

    It looks like the sample you are looking for does not exist.

    Your custom layout now picks up any device that matches the manufacturer and product name strings, or the vendor and product IDs in its HID descriptor. The Input System now represents a DualShock4GamepadHID device instance.

    For more information, refer to the device matching documentation.

    In This Article
    Back to top
    Copyright © 2026 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)