Author Topic: NGUI custom input settings  (Read 3861 times)

joduffy

  • Newbie
  • *
  • Thank You
  • -Given: 0
  • -Receive: 0
  • Posts: 28
    • View Profile
NGUI custom input settings
« on: April 25, 2014, 03:56:53 AM »
Hi,

I want to create a custom settings menu with NGUI to allow the user to change the standard controls to whatever they like.

Is there a way in NGUI to list all the keycode so I can then store it in a player pref.

I know I could do it through an input text field, but im worried if they type the wrong keycode in and break the controls.

Thanks for your time guys.

ArenMook

  • Administrator
  • Hero Member
  • *****
  • Thank You
  • -Given: 337
  • -Receive: 1171
  • Posts: 22,128
  • Toronto, Canada
    • View Profile
Re: NGUI custom input settings
« Reply #1 on: April 25, 2014, 09:22:51 AM »
Keycode of what? NGUI's default input only reacts to arrow keys, enter and tab keys.

joduffy

  • Newbie
  • *
  • Thank You
  • -Given: 0
  • -Receive: 0
  • Posts: 28
    • View Profile
Re: NGUI custom input settings
« Reply #2 on: April 25, 2014, 07:52:07 PM »
What I want to do is allow custom controls like in league of legends or first person shooting games.

So:

Forward - w
Back - s
Left - a
Right - d

But allow the user to click on the NGUI text field and press a key on the keyboard. Then the keycode for that key is stored in the NGUI textfield. So not having the user type out words/message but just allow them to enter one key and display the results.

Does this make sense? So if they press the spacebar. The keycode would then be put in the textfield. I could then store the textfield as a string in player prefs which I could then use to update the controls for the game.

ArenMook

  • Administrator
  • Hero Member
  • *****
  • Thank You
  • -Given: 337
  • -Receive: 1171
  • Posts: 22,128
  • Toronto, Canada
    • View Profile
Re: NGUI custom input settings
« Reply #3 on: April 26, 2014, 02:55:07 PM »
Check UICamera.inputHasFocus before procssing your Input.GetKey logic. Although if you don't have the latest Pro version, inputHasFocus is bugged in 3.5.8. You need to fix it by changing UICamera's line 396 from:
  1. selectedObject = null;
to:
  1. selectedObject = currentTouch.pressed;

joduffy

  • Newbie
  • *
  • Thank You
  • -Given: 0
  • -Receive: 0
  • Posts: 28
    • View Profile
Re: NGUI custom input settings
« Reply #4 on: April 28, 2014, 06:00:53 AM »
Hmm,

I did try and use this:

  1. Input.anyKey

But it doesn't seem to work. I try and store the results of a key press with:
  1. storeResult = Input.inputString;

I did find this link:

http://issuetracker.unity3d.com/issues/input-dot-inputstring-works-differently-on-windows-and-on-mac

So maybe there is a bug in Unity itself. The only other option I can see is to use events.

  1. using UnityEngine;
  2. using System.Collections;
  3.  
  4. public class Example : MonoBehaviour {
  5.     void OnGUI() {
  6.         Event e = Event.current;
  7.         if (e.isKey)
  8.             Debug.Log("Detected a keyboard event!");
  9.        
  10.     }
  11. }
  12.  

I noticed you do something similar in UIInput.cs
  1.        
  2. /// <summary>
  3. /// Unfortunately Unity 4.3 and earlier doesn't offer a way to properly process events outside of OnGUI.
  4. /// </summary>
  5.  
  6. void OnGUI ()
  7. {
  8.         if (isSelected && Event.current.rawType == EventType.KeyDown)
  9.                 ProcessEvent(Event.current);
  10. }
  11.  

I think I am just tripping myself up. What I am trying to do is get it working with playmaker and NGUI. But it might be easier to piggy back off of NGUI via a C# script. Would the easiest solution be to duplicate the UIInput.cs file. Edit the contents, so that when a user presses a key. The child UILabel.cs file is updated with the key name. Multiple key presses would also just change the key name being displayed in the UILabel. Thus allowing me to have a custom input script for not only this game but further ones.

Am I on the right track?

Once the key is displayed correctly in the UILable. I can then retrieve it, store the value in a string in player prefs. Then using the keycode method in my code, I can check against the strings stored value when a key is pressed for custom controls.

ArenMook

  • Administrator
  • Hero Member
  • *****
  • Thank You
  • -Given: 337
  • -Receive: 1171
  • Posts: 22,128
  • Toronto, Canada
    • View Profile
Re: NGUI custom input settings
« Reply #5 on: April 29, 2014, 02:31:23 PM »
If you want to display the last pressed key, just create a function that will listen for UInput's onChange notification. Or better yet -- set up UIInput.onValidate to a function. Inside the function you will be receiving each pressed character.