BLACKISH DEV BLOG Support
 

 

Making InControl work with NGUI

If you’ve ever worked with gamepad controls in Unity you know that it’s not a lot of fun. Unity’s Input-system is a bit of a mess to beginn with, but in addition the button mappings on all the different controllers don’t match up. (Just compare the PS3 controller mapping to the Xbox 360 Controller mapping for example)

Luckily Patrick over at Gallant Games has written InControl to fix the mess. And it works like a charm! (It’s open source, but he’s done such a great job that you should support him by getting it on the AssetStore here, like I did!)

Adding joystick controls to my game could have been a week of pain and misery, but with InControl it was done casually in one evening.

But now onwards to the actual gist of this post!

The only hurdle was making InControl work with NGUI. NGUI allows you to control your interface with keyboard and joystick simply by adding UIButtonKeys components to your widgets and defining which other element should be activated if you press up/down/left/right. All you need to do to make NGUI use InControl instead of looking for the “Vertical” and “Horizontal” axes (as it does by default) is to modify UICamera.cs like this:

Add this to the very top:

using InControl;

Look for the GetDirection method and replace

float val = Input.GetAxis(axis);

with this:

float val = 0f;

InputDevice activeDevice = InputManager.ActiveDevice;
if(axis == "Horizontal") {
	val = activeDevice.LeftStickX.Value + activeDevice.RightStickX.Value + activeDevice.DPadLeft.Value * -1f + activeDevice.DPadRight.Value;
} else if(axis == "Vertical") {
	val = activeDevice.LeftStickY.Value + activeDevice.RightStickY.Value + activeDevice.DPadUp.Value + activeDevice.DPadDown.Value * -1f;
}

Done! Now you can control your GUI with the D-Pad as well as the left and right sticks of any supported controller!

PS: One little problem: Remember that you’ll have to apply this change whenever you update NGUI. If anyone has an idea for a better solution than changing the actual UICamera script, please let me know! I wanted to solve it with inheritance, but since I can’t override a static method…

Bookmark the permalink. Follow any comments here with the RSS feed for this post. Post a comment or leave a trackback: Trackback URL.

2 Comments

    Mathias Sørensen on March 11, 2014 at 20:56 | Permalink

    Nice it works, thanks so much.
    Can you tell me how I make it accept x/a pressed as an enter. so i can press my selected menu item?

    col000r on June 24, 2014 at 14:19 | Permalink

    Check out custom profiles, like this one here (it’s included as an example)

Post a Comment

Your email is never published nor shared. Required fields are marked *

*
*

You may use these HTML tags and attributes: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <strike> <strong>