cInput is a popular input plugin available from the Unity Asset Store. After spending some time adapting NGUI to accept axis/button input from cInput, I thought others may find the following guide useful. The modifications noted below will allow the UIButtonKeys script to function properly with cInput.
NGUI looks for input from the vertical/horizontal axis and a submit/cancel button combination. These are typically set on the UICamera in the 'Axes and Keys' displayed in the inspector. We will need to bypass the standard system input calls and route these to cInput.
The following modifications will need to be made to the
UICamera.cs script.
First, modify the
GetDirection method so this evaluates your cInput axis instead of the system-defined axis.
static int GetDirection (string axis) {
float time = RealTime.time;
string cInputAxisName = (axis == "Vertical") ? "YourVerticalAxisName" : "YourHorizontalAxisName";
if (mNextEvent < time) {
float val = cInput.GetAxisRaw(cInputAxisName);
//remaining method code is unchanged
Next, you will need to modify how the submitKey and cancelKey input is provided in the
ProcessOthers method. We will comment out lines referencing the submitKey and replace with calls to cInput:
Here are the
submitKey changes:
//Note line commented below is replaced by the following
//if (submitKey0 != KeyCode.None && Input.GetKeyDown(submitKey0))
if (cInput.GetKeyDown("YourSubmitButtonName")) {
currentKey = submitKey0;
submitKeyDown = true;
}
//Note line commented below is replaced by the following
//if (submitKey0 != KeyCode.None && Input.GetKeyUp(submitKey0))
if (cInput.GetKeyUp("YourSubmitButtonName")) {
currentKey = submitKey0;
submitKeyUp = true;
}
After making the above changes, you will need to remove the related lines pertaining to submitKey1 that follow the above codeNow we will make the same change for the cancel button. Here are the
cancelKey changes:
//Note line commented below is replaced by the following
//if (cancelKey0 != KeyCode.None && Input.GetKeyDown(cancelKey0))
if (cInput.GetKeyDown("YourCancelButtonName")) {
currentKey = cancelKey0;
currentScheme = ControlScheme.Controller;
Notify(mCurrentSelection, "OnKey", KeyCode.Escape);
}
As with the submitKey tweaks, you will want to remove the cancelKey1 entry that follows the lines above.After implementing these changes, your cInput controls should drive the menu navigation through UIButtonKeys.
It should be noted that changing the vertical axis name to anything other than "Vertical" in the inspector would break the vertical axis input. This is pretty easy to work around and can be permanently fixed by restricting the scope of the axis/button variables. A few adjustments need to be made to the UICameraEditor.cs script to accommodate these changes so I omitted from above for simplicity.
Hope this is helpful.