- Home /
How do Text Fields work? (Backend)
Recently I have been making my own GUI module in python, and I have been modelling it after the one used in Unity. I just love the way everything is put together in Unity! So I have gotten to the stage where I have to pass 2 values into my text field function, in order to save both the text and weather or not the field is active, but Unity seems to do this automatically???
How does it work? How does Unity keep track of which text field is active, when I can add any text field anywhere at any time...
If backend questions arn't supposed to be here, please just tell me and I'll delete the question :)
Thank you, Benproductions1
Ps: I'm not looking for code, just an explanation of how the Unity GUI text field works in the backend :)
EDIT: Let me clear this up a little bit.
I am asking, how unity manages to do text fields by only supplying the text and not an active
value AKA: Why is it text = GUILayout.TextField(text);
and not text = GUILayout.TextField(text, ref active);
Thankn you
Answer by Loius · Mar 14, 2013 at 01:17 AM
Aha! Now I gotcha.
The OnGUI function runs 'in steps', at least twice per Update (Layout and Repaint steps). The GUI functions do different things during different steps, to the point where it's possible that I've needed to manually 'wait until step X' in some particularly tricky GUI nonsense.
You can check if you're in Layout mode (the only check I've needed) inside OnGUI with:
(Event.current.type == EventType.Layout)
=> in Layout step
Unity calls OnGUI frequently with different event types. The logically-first step is Layout, in which the GUI* functions create GUI elements (which are a complex class of their own) and assign them IDs. Next is Repaint, in which the elements are drawn.
I don't know exactly how Unity handles the active control, but I'm reasonably certain that it simply keeps track of its internal ID of the active control, and changes that based on user input. The elements are recreated every GUI call (unless Unity caches them with some magiclogic), but if they're the same elements they have the same ID and so you keep the same active control.
You can sort-of see the results of this if you have a GUI setup with several text fields, and you remove one or more while entering text - the entered text magically transfers to other fields as Unity gets confused about them (I have yet to experience any functional defect from this behaviour; as far as I can tell it's all cosmetic).
yea, I've already gotten that far, unity does cache the GUI, that much is obvious through some GUI functions. I understand the workflow of the GUI in unity, I was only wondering about text fields... I need to check out if unity behaves wierdly, when I randomly show more or less text fields before the one I have selected... If there is some "magic Logic" behind this all, I really want to know what it is :) Thanks anyways, I was hoping unity used something other than just giving each element and id and kepping track of the active one... $$anonymous$$agic logic is always cool! Benproductions1
I know there's the funky text fields, and I know I haven't come across any logical errors that can't be solved by only changing the layout of your GUI during the layout step, and I know I use convoluted sentences a lot. :)
I hope you get a more-exact answer, I'm kinda curious now.
Ok, I just did some testing and it seems you are correct, there is no magic logic behind the text fields, when Unity goes through the OnGUI
function, it assigns each a special id, then keeps track of that id. If you suddenly have a text field before another text field that you are focused on, the focus shifts between them.
I have also found that you can give each GUI element your own special name, which will negate the bad behaviour previously mentioned.
Thank you for your answer, my own GUI library (In python) is going quite well, now that this has been cleared up XD
Your answer
Follow this Question
Related Questions
Unity Scale One Text Field GUI 2 Answers
Lock a textarea/textfield for editing 1 Answer
A node in a childnode? 1 Answer
Custom-made GUI Textfield 1 Answer
GUILayout.Label - why messages are appearing on each other? 0 Answers