Skip to content
/ XInput Public

A very simple plugin for handling XBox 360 and Xbox One controllers.

License

Notifications You must be signed in to change notification settings

Eldoir/XInput

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

36 Commits
 
 
 
 
 
 
 
 
 
 

Repository files navigation

XInput

You are using Unity3D and you wonder how to handle Xbox 360 and Xbox One controllers very easily? Well, there you go!

XInput gets rid of all the complicated code so that you can focus on the essentials: interaction!

See Examples to find out what XInput is capable of.

Getting Started

For a quick import into an existing project, just get the UnityPackage.

If you want to test the UnityPackage in an empty project, that's what the XInput folder is! :)

To get it working, just go into GameObject -> Input -> XInput. It will create a new GameObject in your scene, named "XInput" and with the XInput script on it. You're good to go!

How to

Examples

Here are some examples of what you can do with XInput:

Controllers
Know if a controller is connected
public class ExampleScript : MonoBehaviour
{
	void Update()
	{
		// Player 1 is connected
		if (XInput.IsControllerConnected())
		{

		}
	}
}
Know if a controller just connected
public class ExampleScript : MonoBehaviour
{
	void Update()
	{
		// Player 1 just connected this frame
		if (XInput.ControllerConnected())
		{

		}
	}
}
Know if a controller just disconnected
public class ExampleScript : MonoBehaviour
{
	void Update()
	{
		// Player 1 just disconnected this frame
		if (XInput.ControllerDisconnected())
		{

		}
	}
}
Buttons
Know if a button is being hold
public class ExampleScript : MonoBehaviour
{
	void Update()
	{
		// Player 1 is holding A
		if (XInput.ButtonHold(XInput.Button.A))
		{

		}
	}
}
Know if a button was pressed
public class ExampleScript : MonoBehaviour
{
	void Update()
	{
		// Player 1 just pressed A this frame
		if (XInput.ButtonPressed(XInput.Button.A))
		{

		}
	}
}
Know if a button was released
public class ExampleScript : MonoBehaviour
{
	void Update()
	{
		// Player 1 just released A this frame
		if (XInput.ButtonReleased(XInput.Button.A))
		{

		}
	}
}
Sticks
Get the direction of a stick
public class ExampleScript : MonoBehaviour
{
	void Update()
	{
		// Get the direction of Player 1 Left Stick as a Vector2
		Debug.Log(XInput.GetStickDirection(XInput.Stick.Left));
	}
}
Know if a stick's direction has changed
public class ExampleScript : MonoBehaviour
{
	void Update()
	{
		// Player 1 just moved her Left Stick in the Up direction
		if (XInput.StickDirectionChanged(Stick.Left) == XInput.Direction.Up)
		{

		}
	}
}
Know if a stick was released
public class ExampleScript : MonoBehaviour
{
	void Update()
	{
		// Player 1 just released her Left Stick
		if (XInput.StickReleased(XInput.Stick.Left))
		{

		}
	}
}
Know if a stick is in the deadzone
public class ExampleScript : MonoBehaviour
{
	void Update()
	{
		// Player 1 has her Left Stick in the deadzone
		if (XInput.StickInDeadZone(Stick.Left))
		{

		}
	}
}
Set a new radius to use to determine whether a stick is in the deadzone
public class ExampleScript : MonoBehaviour
{
	void Start()
	{
		// The new radius must be between [0-1]. If it's out of bounds, it will be clamped anyway.
		XInput.SetDeadZoneRadius(0.2f);
	}
}
Triggers
Get the value of a trigger (between [0-1])
public class ExampleScript : MonoBehaviour
{
	void Update()
	{
		// Get the Left Trigger value for Player 1
		Debug.Log(XInput.GetTriggerValue(XInput.Trigger.Left));
	}
}
Know if a trigger is being hold
public class ExampleScript : MonoBehaviour
{
	void Update()
	{
		// Player 1 is holding her Left Trigger
		if (XInput.TriggerHold(XInput.Trigger.Left))
		{

		}
	}
}
Know if a trigger was pressed
public class ExampleScript : MonoBehaviour
{
	void Update()
	{
		// Player 1 just pressed her Left Trigger this frame
		if (XInput.TriggerPressed(XInput.Trigger.Left))
		{

		}
	}
}
Know if a trigger was released
public class ExampleScript : MonoBehaviour
{
	void Update()
	{
		// Player 1 just released her Left Trigger this frame
		if (XInput.TriggerReleased(XInput.Trigger.Left))
		{

		}
	}
}
Set a new value to use to determine whether a trigger was pressed or released
public class ExampleScript : MonoBehaviour
{
	void Start()
	{
		// The new value must be between [0-1]. If it's out of bounds, it will be clamped anyway.
		XInput.SetTriggerMinValueToConsiderPressedOrReleased(0.9f);
	}
}
DPad
Get the DPad direction
public class ExampleScript : MonoBehaviour
{
	void Update()
	{
		// Player 1 is holding Up on her DPad
		if (XInput.GetDPadDirection() == XInput.Direction.Up)
		{

		}
	}
}
Know if the DPad direction has changed
public class ExampleScript : MonoBehaviour
{
	void Update()
	{
		// Player 1 just moved her DPad in the Up direction
		if (XInput.DPadDirectionChanged() == XInput.Direction.Up)
		{

		}
	}
}
Know if the DPad was released
public class ExampleScript : MonoBehaviour
{
	void Update()
	{
		// Player 1 just released her DPad
		if (XInput.DPadReleased())
		{

		}
	}
}
Vibration
Set a vibration on both triggers
public class ExampleScript : MonoBehaviour
{
	void Update()
	{
		if (somethingHappened)
		{
			// Set a vibration on Player 1 Left and Right Triggers, with power 50% for 1s
			XInput.SetVibration(0.5f, 1f);
		}
	}
}
Set a vibration on a specific trigger
public class ExampleScript : MonoBehaviour
{
	void Update()
	{
		if (somethingHappened)
		{
			// Set a vibration on Player 1 Left Trigger, with power 50% for 1s
			XInput.SetVibration(XInput.Trigger.Left, 0.5f, 1f);
		}
	}
}
Stop the vibration for all controllers
public class ExampleScript : MonoBehaviour
{
	void Update()
	{
		if (somethingHappened)
		{
			XInput.StopAllVibrations();
		}
	}
}
Stop the vibration for a specific controller
public class ExampleScript : MonoBehaviour
{
	void Update()
	{
		if (somethingHappened)
		{
			// Stop vibration for Player 1.
			XInput.StopVibration();
		}
	}
}
Stop the vibration for a specific controller and specific trigger
public class ExampleScript : MonoBehaviour
{
	void Update()
	{
		if (somethingHappened)
		{
			// Stop vibration for Player 1 Left Trigger.
			XInput.StopVibration(XInput.Trigger.Left);
		}
	}
}

Best Code Usage

Actually, you can get information from the controllers in 2 ways :

  • The first, simpler, is to call the methods directly. That's what the examples above show, for the sake of simplicity.
public class ButtonScript : MonoBehaviour
{
	void Update()
	{
		if (XInput.ButtonPressed(XInput.Button.A)) // Player 1 just pressed A this frame
		{
			// Do stuff for player 1
		}
	}
}
  • The second, hardly more complex and much more efficient and professional, consists of registering for events, and to perform actions only when these events occur.
public class ButtonScript : MonoBehaviour
{
	void Start()
	{
		XInput.onButtonPressed += OnButtonPressed; // We listen to the OnButtonPressed event
	}

	void OnButtonPressed(XInput.Button button, int playerIndex)
	{
		if (button == XInput.Button.A && playerIndex == 0) // Player 1 just pressed A
		{
			// Do stuff
		}
	}

	void OnDestroy()
	{
		XInput.onButtonPressed -= OnButtonPressed; // We don't need to listen to the event anymore
	}
}

Here is the list of events you can register in this way:

Controller events
  • OnControllerConnected
  • OnControllerDisconnected
Button events
  • OnButtonPressed
  • OnButtonReleased
Stick events
  • OnStickDirectionChanged
  • OnStickReleased
Trigger events
  • OnTriggerPressed
  • OnTriggerReleased
DPad events
  • OnDPadDirectionChanged
  • OnDPadReleased

Demo Scene

The screenshot below was taken from the Demo Scene, included in the plugin. You can plug in multiple controllers and see their model update when you press buttons!

Demo Scene

Notes

  • Last tested with Unity 2018.4.11f1, from 11 October 2019.
  • XInput only works for Windows! No Mac, Web or Mobile supported.
  • XInput uses a radial method for its deadzone. See this fantastic article to read more about the different deadzone methods.

Authors

  • Arthur Cousseau
  • This plugin is "just" a wrapper around XInputDotNet. However, it does a lot more than that: it provides event-driven programming, and convenient enums.

License

This project is licensed under the MIT License - see the LICENSE.md file for details.

Acknowledgments

  • Thanks to 360Controller, a macOS driver, for providing most of the images used in the demo scene.

About

A very simple plugin for handling XBox 360 and Xbox One controllers.

Resources

License

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published

Languages