Author Topic: Gather input fields data from one button click  (Read 4211 times)

Briksins

  • Newbie
  • *
  • Thank You
  • -Given: 2
  • -Receive: 0
  • Posts: 5
    • View Profile
Gather input fields data from one button click
« on: March 24, 2014, 07:25:11 PM »
Hello

I'm very new to NGUI, literally just started. However im good programmer. I Watched few video tutorial and played around with all examples scenes in the NGUI pack and found it very cool, however one main and major thing is missing in example or video tutorial - it is how to deal with input and other GUI elements from the code.
How to gather input fields data?

Lets assume I have registration form with 5 fields.
Each field has "On Submit" and "On Change" event listener
Final 'Register' Button - has "On Click" event listener

How to gather all inputs from all fields from "On Click" event?
From what i got first in my mind - i see few scenarios:

1) Find each input field as GO, get it components and gather data - that's very over complicated and compute intensive i would say, dont like an idea of searching objects in entire scene, especially if i have tons of them.
2) Attach script which dealing with button "On Click" event to each text field and try to collect or to be more clear build up final values trough "On change" event by appending value -  I'm not sure yet would i be able to differentiate between input fields in "On change" event, and all this still feels over complicated
3) Create static script for each field and then collect the data - easy implementation, but too many scripts in project
4) not even sure, but im sure there should be easy, nice and clean way of doing so

Could you guys please explain to me what is the best and common way to gather data from input fields trough button click event?


While I already create new topic ill use that chance and ask one more question :)
Is gui elements dynamic in relation to screen resolution?
For example if ill make bottom slide bar which fit at the very bottom and fill full screen width, will it be automatically scaled to the window size?
for example my Web Build has resolution set to 1024 x something, my Android SGS4 is 1080 x 1920 , my PC is opposite of SGS4 - 1920x1080
Would this bottom bar continue to consume entire screen width and proportional high to different resolutions dynamically? or I will have to handle it and scale gui items from the code?

NGUI is truly amazing but oriented more to the Unity Editor, rather then dynamic creation from the code as far as i see? am i right? or horribly mistaken? however if it dynamically scales depending on build target screen resolution I probably even dont need to have it the code as it is already dynamic, i could design then in Editor once :)

Anyway - thank you guys very much in advance           

Briksins

  • Newbie
  • *
  • Thank You
  • -Given: 2
  • -Receive: 0
  • Posts: 5
    • View Profile
Re: Gather input fields data from one button click
« Reply #1 on: March 25, 2014, 05:55:48 PM »
Topic UP!
Still no answer? c'mon guys give me your advice :)

ArenMook

  • Administrator
  • Hero Member
  • *****
  • Thank You
  • -Given: 337
  • -Receive: 1171
  • Posts: 22,128
  • Toronto, Canada
    • View Profile
Re: Gather input fields data from one button click
« Reply #2 on: March 26, 2014, 02:52:18 AM »
All NGUI events set a 'current' value in the class they originated from.

For example, when you click on a button and it triggers your remote click function, UIButton.current will tell you which button triggered the callback. UIInput.current will give you a reference to the input field that called the OnSubmit or OnChange functions, etc.

So to print the submitted value in the input field:
  1. void MySubmitFunc ()
  2. {
  3.     Debug.Log(UIInput.current.value);
  4. }
You could have figured this out just by browsing the NGUI's documentation:

Input: http://www.tasharen.com/forum/index.php?topic=6752.0
Button: http://www.tasharen.com/forum/index.php?topic=6708.0

If you have a few input fields, then it's up to you to use them from scripts. It's common practice to have a "manager" script that has public references to all your input fields that you drag & drop in inspector, and to have buttons and input fields call functions inside that script. Since the script is aware of all the input fields, getting their values is as simply as someInput.value.