Author Topic: UIInput  (Read 92409 times)

ArenMook

  • Administrator
  • Hero Member
  • *****
  • Thank You
  • -Given: 337
  • -Receive: 1171
  • Posts: 22,128
  • Toronto, Canada
    • View Profile
UIInput
« on: November 22, 2013, 10:02:08 PM »
Overview

UIInput is a script that makes it possible to type in an area, allowing you to create input fields and editable text boxes.



To create an input field, just add it to any clickable area and choose a Label that it will be modifying.

The most basic setup for an input field would be a sliced Sprite for the background (or a Widget if you want it to be invisible), and a child Label for the foreground. In this example, the sprite would need a Box Collider in order to receive events, and the UIInput script would go onto it as well:

Quote
Clickable Area (UISprite, BoxCollider, UIInput)
- Text (UILabel)

This setup is all that's needed in order to be able to click on the input field and start typing.

You can give your Input Field a Starting Value if you like, and you can also make it automatically save its value by giving it a key in the Saved As field. If specified, the value you typed will be persistent. Most obvious usage of this would be for a Player Name or a Username field.

You can set the inactive text on the input field by modifying the text of the label itself. This includes the label's color. So if you wanted a half-faded out "press here to start typing" text to be visible when the input field has no text in it, enter that text on your label and set the label's color to be half-transparent (or just modify the Inactive color field on the UIInput).

When the input field gets selected, the label's text will be replaced, and its color will be changed to the Active Text color value specified on the UIInput.

You can change the Input Type of your input if you need it to be a Password Field, or if you want there to be auto-correcting behaviour on mobile platforms. The Keyboard Type is also there for mobile platforms, as it controls what kind of keyboard will show up when you select the input field.

If you want the input field to have some basic validation, such as making it accept only Integer values, set it using the Validation option. You can likewise limit the number of characters that the user will be allowed to enter using the Character Limit field. Leaving it at zero removes the character limit.

To allow your user to use Tab to navigate to the next field, drag & drop the next input field into the Select On Tab field.

To change the alignment of your input field, modify it on the Label itself. You can similarly limit your input field to a single line by setting the Max Lines property to 1 on the UILabel. The same goes for all the other options on the label, such as giving it a gradient, a shadow effect, or changing the character spacing.

Note that in most cases you will likely want to leave your label's Overflow handling on ClampContent, as this will allow the input to automatically scroll to the right as the user keeps typing past the original bounds.

To receive a notification of when the user submits the text in the input field, take advantage of its On Submit notification section. You can always retrieve the input field's value within the function you're calling by using UIInput.current.value. After you're done processing the callback you can set the UIInput.current.value to null to clear the input field's text:
  1. public void MySubmitFunction ()
  2. {
  3.     Debug.Log("I typed: " + UIInput.current.value);
  4.     UIInput.current.value = null;
  5. }

Pro-Tip

If you want to be able to select your input field when pressing a specific key, such as hitting "Enter" to start typing in your chat window, attach a UIKeyBinding script to it.



Class Documentation

http://tasharen.com/ngui/docs/class_u_i_input.html

If you have a question regarding this component or would like me to clarify something, just post a reply here.
« Last Edit: December 14, 2013, 07:35:41 PM by ArenMook »

Haim

  • Newbie
  • *
  • Thank You
  • -Given: 0
  • -Receive: 0
  • Posts: 29
    • View Profile
Re: UIInput
« Reply #1 on: December 11, 2013, 03:48:50 PM »
what about move from one input box to the next with TAB button? how can i do that?

ArenMook

  • Administrator
  • Hero Member
  • *****
  • Thank You
  • -Given: 337
  • -Receive: 1171
  • Posts: 22,128
  • Toronto, Canada
    • View Profile
Re: UIInput
« Reply #2 on: December 12, 2013, 05:57:37 AM »
That's specified on the UIInput.

Haim

  • Newbie
  • *
  • Thank You
  • -Given: 0
  • -Receive: 0
  • Posts: 29
    • View Profile
Re: UIInput
« Reply #3 on: December 14, 2013, 09:26:07 AM »
i read this again, don't see any thing about TAB. only ENTER to start type...
i have two UIinpus fields and i want to use TAB to jump from one field to other.

also, the UIinput configuration  in 3.0.7 looks different then what i see in the image here. (not updated?)
the image here missing the "Select on tab" option but i'm not sure how it works.
it asking to attach game object, i attached the parent of both UIinput but nothing happen.

ArenMook

  • Administrator
  • Hero Member
  • *****
  • Thank You
  • -Given: 337
  • -Receive: 1171
  • Posts: 22,128
  • Toronto, Canada
    • View Profile
Re: UIInput
« Reply #4 on: December 14, 2013, 07:30:23 PM »
The only thing missing in the screenshot is the "Select On Tab" field, which is coincidentally what you were asking about. Set it to some game object (like the next input field) and it will be selected when you hit Tab.

If you look at Example 0 that comes with NGUI, the two input areas have each other set in the Select On Tab fields, which lets me tab from one to the other.

I've updated the screenshot.
« Last Edit: December 14, 2013, 07:36:29 PM by ArenMook »

indiefreaks

  • Newbie
  • *
  • Thank You
  • -Given: 0
  • -Receive: 0
  • Posts: 27
    • View Profile
Re: UIInput
« Reply #5 on: December 15, 2013, 07:14:24 PM »
Would love to see some Pro tips about custom validation on a per UIInput field basis ;)

Thanks

zippo227

  • Jr. Member
  • **
  • Thank You
  • -Given: 4
  • -Receive: 0
  • Posts: 55
    • View Profile
Re: UIInput
« Reply #6 on: January 04, 2014, 06:09:26 PM »
I recently updated and noticed that my input is no longer submitting. I want the label to be able to spread across multiple lines (instead of overflowing to the right) but still submit when the user presses Enter. Is there still a way to do that? I notice that now you've changed it to add a \n

ArenMook

  • Administrator
  • Hero Member
  • *****
  • Thank You
  • -Given: 337
  • -Receive: 1171
  • Posts: 22,128
  • Toronto, Canada
    • View Profile
Re: UIInput
« Reply #7 on: January 05, 2014, 09:14:27 AM »
Change the label to use Max Lines of 1.

47giga

  • Newbie
  • *
  • Thank You
  • -Given: 0
  • -Receive: 0
  • Posts: 2
    • View Profile
Re: UIInput
« Reply #8 on: January 06, 2014, 10:01:15 AM »
Hi, I am using NGUI 3.0.7 and don't know how to get new value before submitting it in UIInput.
(The value in the inspector is changed but cannot get it until submit)

ArenMook

  • Administrator
  • Hero Member
  • *****
  • Thank You
  • -Given: 337
  • -Receive: 1171
  • Posts: 22,128
  • Toronto, Canada
    • View Profile
Re: UIInput
« Reply #9 on: January 06, 2014, 10:43:10 AM »
I don't understand what you're asking. UIInput.current.value tells you the value inside your OnSubmit. What does it have to do with "before submitting"?

47giga

  • Newbie
  • *
  • Thank You
  • -Given: 0
  • -Receive: 0
  • Posts: 2
    • View Profile
Re: UIInput
« Reply #10 on: January 06, 2014, 12:31:51 PM »
Thanks. I tried the following line but it caused the following error on Unity.

code:
void Update (){ Debug.Log(UIInput.current.value);}

error:
NullReferenceException: Object reference not set to an instance of an object
myInput.Update () (at Assets/myInput.cs:12)

----
I also tried the following code for onSubmit but it is never called without typing return. I meant it as "before submitting it"
public void valueTest(){Debug.Log(UIInput.current.value);}

----
P.S. I solved my problem by getting the value by the following line instead of UIInput.current.value so it is not problem anymore but I write my solution as FYI.
void Update (){Debug.Log(this.GetComponent<UIInput>().value); }
« Last Edit: January 06, 2014, 01:47:07 PM by 47giga »

ArenMook

  • Administrator
  • Hero Member
  • *****
  • Thank You
  • -Given: 337
  • -Receive: 1171
  • Posts: 22,128
  • Toronto, Canada
    • View Profile
Re: UIInput
« Reply #11 on: January 06, 2014, 06:38:09 PM »
UIInput.current is only valid inside the OnSubmit delegate callback. It will remain null everywhere else.

Ernest

  • Newbie
  • *
  • Thank You
  • -Given: 0
  • -Receive: 1
  • Posts: 40
    • View Profile
Re: UIInput
« Reply #12 on: January 07, 2014, 05:03:24 AM »
So if i want to get the input field value in the onClick event of a button, how do i do?

ArenMook

  • Administrator
  • Hero Member
  • *****
  • Thank You
  • -Given: 337
  • -Receive: 1171
  • Posts: 22,128
  • Toronto, Canada
    • View Profile
Re: UIInput
« Reply #13 on: January 07, 2014, 08:59:22 AM »
Button won't know anything about your input, so it's up to you to find it somehow.

If you hit Enter to submit text in the input field, then UIInput.current will contain the reference of the input field that triggered the OnSubmit event.

Ernest

  • Newbie
  • *
  • Thank You
  • -Given: 0
  • -Receive: 1
  • Posts: 40
    • View Profile
Re: UIInput
« Reply #14 on: January 07, 2014, 10:10:18 AM »
Yep i got that. My issue is, if i have a "Submit" function (doing any process) using UIInput.current.value, i have no problem by setting it in the OnSubmit field in the UIInput inspector (so when i press the return key) but it failed when i set it in the OnClick field in the UIButton inspector (for the reason you exposed). So there is no simple/inspector way to do my input related process by clicking on this button? (I have to get the label value in my "Submit" function instead of UIInput.current.value?) Thanks

EDIT: thanks for the trick below :)
« Last Edit: January 10, 2014, 08:28:30 AM by Ernest »