Author Topic: Guide: NGUI and cInput2  (Read 3555 times)

wtraxler

  • Newbie
  • *
  • Thank You
  • -Given: 0
  • -Receive: 1
  • Posts: 1
    • View Profile
Guide: NGUI and cInput2
« on: January 06, 2014, 12:17:16 PM »
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.

  1. static int GetDirection (string axis) {
  2.                 float time = RealTime.time;
  3.  
  4.                 string cInputAxisName = (axis == "Vertical") ? "YourVerticalAxisName" : "YourHorizontalAxisName";
  5.  
  6.                 if (mNextEvent < time) {
  7.                         float val = cInput.GetAxisRaw(cInputAxisName);
  8.  
  9. //remaining method code is unchanged
  10.  

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:

  1. //Note line commented below is replaced by the following
  2. //if (submitKey0 != KeyCode.None && Input.GetKeyDown(submitKey0))
  3. if (cInput.GetKeyDown("YourSubmitButtonName")) {
  4.         currentKey = submitKey0;
  5.         submitKeyDown = true;
  6. }
  7.  
  8. //Note line commented below is replaced by the following
  9. //if (submitKey0 != KeyCode.None && Input.GetKeyUp(submitKey0))
  10. if (cInput.GetKeyUp("YourSubmitButtonName")) {
  11.         currentKey = submitKey0;
  12.         submitKeyUp = true;
  13. }
  14.  
  15.  

After making the above changes, you will need to remove the related lines pertaining to submitKey1 that follow the above code

Now we will make the same change for the cancel button.  Here are the cancelKey changes:

  1. //Note line commented below is replaced by the following
  2. //if (cancelKey0 != KeyCode.None && Input.GetKeyDown(cancelKey0))
  3. if (cInput.GetKeyDown("YourCancelButtonName")) {
  4.         currentKey = cancelKey0;
  5.         currentScheme = ControlScheme.Controller;
  6.         Notify(mCurrentSelection, "OnKey", KeyCode.Escape);
  7. }
  8.  

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.

ArenMook

  • Administrator
  • Hero Member
  • *****
  • Thank You
  • -Given: 337
  • -Receive: 1171
  • Posts: 22,128
  • Toronto, Canada
    • View Profile
Re: Guide: NGUI and cInput2
« Reply #1 on: January 06, 2014, 05:20:41 PM »
Thanks for sharing!

John.Bergman

  • Newbie
  • *
  • Thank You
  • -Given: 0
  • -Receive: 0
  • Posts: 30
    • View Profile
Re: Guide: NGUI and cInput2
« Reply #2 on: February 05, 2014, 01:33:37 AM »
Any chance we can get this as a configuration setting option in NGUI so that we do not have to keep dealing with merging code every time there is a new version of NGUI?

ArenMook

  • Administrator
  • Hero Member
  • *****
  • Thank You
  • -Given: 337
  • -Receive: 1171
  • Posts: 22,128
  • Toronto, Canada
    • View Profile
Re: Guide: NGUI and cInput2
« Reply #3 on: February 05, 2014, 03:03:37 PM »
Configuration setting? If I include this code in NGUI, there will be compile errors unless the users also have cInput.

John.Bergman

  • Newbie
  • *
  • Thank You
  • -Given: 0
  • -Receive: 0
  • Posts: 30
    • View Profile
Re: Guide: NGUI and cInput2
« Reply #4 on: February 06, 2014, 11:28:58 AM »
... Unless you conditional compile it, at least then the change would be in a single location ie, uncommenting a #define.

We use that type of implementation frequently when integrating with external libraries to keep things easily merged together.