Coder Social home page Coder Social logo

unityinputsystem's Introduction

Unity Input System

This is a custom input system for Unity with runtime rebindable keybindings and a very slick API. It currently has support for keyboard/mouse (using Unity's builtin input) and Xbox-like gamepads (via SDL_GameController and Windows.Gaming.Input for UWP).

Usage in Unity

Currently, you can use KeyboardInputComponent, XboxControllerInputComponent, or AnyInputComponent. AnyComponent will automatically swap between any supported input method. This is very useful for singleplayer games.

XboxControllerInputComponent can also allow input from either a specific Xbox controller OR from any. This simply requires setting the PlayerIndex or AnyController fields respectively.

Simply attach the desired input component to a game object, set up your keybindings to be accessible (as shown in the static Binds class below), and read input from your classes.

Local multiplayer can be implemented by simply adding an XboxControllerInputComponent to each GameObject and setting PlayerIndex to the correct value (and adding a KeyboardInputComponent if that is to be supported).

Note on Platform Support

This system uses a custom native layer (available here) interacting with a slightly modified XInputDotNet to provide cross-platform gamepad support for non-UWP platforms. UWP uses the Windows.Gaming.Input library directly.

The library has been tested on 64-bit Windows, UWP (including Xbox One), and Mac OSX. However, it should be compatible with all platform supported by SDL_GameController and CMake. Support for WebGL is planned.

Example

// Binds.cs
using UnityEngine;
using InputSystem.KeyboardMouse;
using InputSystem.Components;
using InputSystem.XboxGamepad;

public static class Binds
{
    public static InputSystem.VectorKeybind MoveBind = new InputSystem.VectorKeybind("move", "This Is A Pretty Name", "Here you can describe what the key will do");
    public static InputSystem.VectorKeybind AimBind = new InputSystem.VectorKeybind("aim"); // above params aren't necessary
    public static InputSystem.ButtonKeybind JumpButton = new InputSystem.ButtonKeybind("jump");
    public static InputSystem.ButtonKeybind SprintButton;   // in fact, you don't have to assign a string ID if you don't want to

    [InputSystem.Attributes.BindingGenerationMethod]
    static void GenerateBindings()
    {
        // Set up defaults if we haven't loaded in
        if (!MoveBind.Bind.AnyBindings)
        {
            MoveBind.Bind
                // Assign the keyboard keys WASD to map to a vector
                .SetKeyboardVector(KeyCode.W, KeyCode.A, KeyCode.S, KeyCode.D)
                // And assign the left stick to map to a vector
                .SetXboxStick(XboxStick.LeftStick);
        }

        if (!AimBind.Bind.AnyBindings)
        {
            AimBind.Bind
                // Use mouse look
                .SetUseMouse()
                // Use the right stick
                .SetXboxStick(XboxStick.RightStick);
        }

        if (!JumpButton.Bind.AnyBindings)
        {
            JumpButton.Bind
                .SetKeyboardMouseButton(KeyCode.Space)
                .SetXboxButton(XboxButton.A);
        }

        if (!SprintButton.Bind.AnyBindings)
        {
            SprintButton.Bind
                .SetKeyboardMouseButton(KeyCode.LeftShift)
                .SetXboxButton(XboxButton.LeftStick);
        }
    }
}

...
// MoveSprite.cs
using UnityEngine;
using InputSystem.KeyboardMouse;
using InputSystem.Components;
using InputSystem.XboxGamepad;
public class MoveSprite : MonoBehaviour
{
    // Lazily loading the input controller
    private InputComponent _inp;
    public InputComponent Input
    {
        get
        {
            if (!_inp)
            {
                _inp = GetComponent<InputComponent>();
            }
            return _inp;
        }
    }

    void Update()
    {
        // If we have a valid input controller...
        if (Input)
        {
            float mult = 1;
            
            // If we are holding down the sprint button (shift or left stick by default)
            if (Input.GetButtonHeld(Binds.SprintButton))
            {
                // up our speed
                mult *= 2;
            }
            
            // Move with wasd + mouse OR left/right stick
            transform.position += (Vector3)((Input.GetVector(Binds.MoveBind) + Input.GetVector(Binds.AimBind)) * Time.deltaTime) * mult;
        }

        // If we press the jump key (space or A)...
        if ((Input && Input.GetButtonPressed(Binds.JumpButton)))
        {
            // reset our position (because that's what a jump does, ofc)
            transform.position = Vector3.zero;
        }
    }
}

unityinputsystem's People

Contributors

shadowndacorner avatar

Stargazers

 avatar

Watchers

James Cloos avatar  avatar  avatar

unityinputsystem's Issues

Remove 4 gamepad limit

This was set up when the project was only targeting XInput, since it only supports 4 gamepads. Once XInput is removed in favor of SDL, that limitation will no longer exist (and is already artificial for UWP).

Shouldn't be a huge refactor, just need to replace the constant 4's with a property/constant/whatever (depending on the platform).

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.