Author Topic: UIInput - detecting changes to text.  (Read 7860 times)

Snowmane

  • Guest
UIInput - detecting changes to text.
« on: May 11, 2012, 06:17:21 PM »
Disclaimer:  I am a new Unity user and by extension also a new NGUI user so if I say something stupid . . . oops!  ;)

Anyway, I have an Input and whenever the user changes the text string in the input, I want to be able to validate the text in the label.  I tried to key this off of the OnInput message, but that message only sends the key that was pressed, not the new value of the text in the label.  And if you try to get the text from the label, it still holds the text value from before the user typed the key.

I took a look at UIInput and I modified the OnInput method like so:

  1.         void OnInput (string input)
  2.         {
  3.                 if (selected && enabled && gameObject.active)
  4.                 {
  5.                         // Mobile devices handle input in Update()
  6.                         if (Application.platform == RuntimePlatform.Android) return;
  7.                         if (Application.platform == RuntimePlatform.IPhonePlayer) return;
  8.  
  9.                         foreach (char c in input)
  10.                         {
  11.                                 if (c == '\b')
  12.                                 {
  13.                                         // Backspace
  14.                                         if (mText.Length > 0) mText = mText.Substring(0, mText.Length - 1);
  15.  
  16.                                         //NEW MESSAGE                  
  17.                                         gameObject.SendMessage("OnInputChanged",  mText, SendMessageOptions.DontRequireReceiver);
  18.                                 }
  19.                                 else if (c == '\r' || c == '\n')
  20.                                 {
  21.                                         // Enter
  22.                                         gameObject.SendMessage("OnSubmit", SendMessageOptions.DontRequireReceiver);
  23.                                         selected = false;
  24.                                         return;
  25.                                 }
  26.                                 else
  27.                                 {
  28.                                         // All other characters get appended to the text
  29.                                         mText += c;
  30.                        
  31.                                         //NEW MESSAGE
  32.                                         gameObject.SendMessage("OnInputChanged", mText, SendMessageOptions.DontRequireReceiver);
  33.                                 }
  34.                         }
  35.  
  36.                         // Ensure that we don't exceed the maximum length
  37.                         UpdateLabel();
  38.                 }
  39.         }

And now I can handle the OnInputChanged message to validate my inputs.  Is there a better way to do this?  If not, I'd suggest this code change be added to the next release of NGUI.

ArenMook

  • Administrator
  • Hero Member
  • *****
  • Thank You
  • -Given: 337
  • -Receive: 1171
  • Posts: 22,128
  • Toronto, Canada
    • View Profile
Re: UIInput - detecting changes to text.
« Reply #1 on: May 11, 2012, 08:55:23 PM »
UIInput.text will always give you the latest text, whether you're modifying it or not. You shouldn't be accessing the label. That said, it looks like a worthwhile addition in itself -- opens up the possibility of input validation. I'll add it.