Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 13 Next capture
2021 2022 2023
1 capture
13 Jun 22 - 13 Jun 22
sparklines
Close Help
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
  • Asset Store
  • Get Unity

UNITY ACCOUNT

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account
  • Blog
  • Forums
  • Answers
  • Evangelists
  • User Groups
  • Beta Program
  • Advisory Panel

Navigation

  • Home
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
    • Blog
    • Forums
    • Answers
    • Evangelists
    • User Groups
    • Beta Program
    • Advisory Panel

Unity account

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account

Language

  • Chinese
  • Spanish
  • Japanese
  • Korean
  • Portuguese
  • Ask a question
  • Spaces
    • Default
    • Help Room
    • META
    • Moderators
    • Topics
    • Questions
    • Users
    • Badges
  • Home /
avatar image
1
Question by Benproductions1 · Mar 13, 2013 at 11:01 PM · guitextfieldbackendmagic

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

Comment
Add comment
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users

1 Reply

· Add your reply
  • Sort: 
avatar image
1
Best Answer

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).

Comment
Add comment · Show 4 · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image Benproductions1 · Mar 14, 2013 at 09:58 PM 0
Share

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

avatar image Loius · Mar 14, 2013 at 11:15 PM 0
Share

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.

avatar image Benproductions1 · Mar 14, 2013 at 11:21 PM 0
Share

I hope so too :)

avatar image Benproductions1 · Apr 07, 2013 at 01:46 AM 0
Share

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

Hint: You can notify a user about this post by typing @username

Up to 2 attachments (including images) can be used with a maximum of 524.3 kB each and 1.0 MB total.

Follow this Question

Answers Answers and Comments

11 People are following this question.

avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image

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


Enterprise
Social Q&A

Social
Subscribe on YouTube social-youtube Follow on LinkedIn social-linkedin Follow on Twitter social-twitter Follow on Facebook social-facebook Follow on Instagram social-instagram

Footer

  • Purchase
    • Products
    • Subscription
    • Asset Store
    • Unity Gear
    • Resellers
  • Education
    • Students
    • Educators
    • Certification
    • Learn
    • Center of Excellence
  • Download
    • Unity
    • Beta Program
  • Unity Labs
    • Labs
    • Publications
  • Resources
    • Learn platform
    • Community
    • Documentation
    • Unity QA
    • FAQ
    • Services Status
    • Connect
  • About Unity
    • About Us
    • Blog
    • Events
    • Careers
    • Contact
    • Press
    • Partners
    • Affiliates
    • Security
Copyright © 2020 Unity Technologies
  • Legal
  • Privacy Policy
  • Cookies
  • Do Not Sell My Personal Information
  • Cookies Settings
"Unity", Unity logos, and other Unity trademarks are trademarks or registered trademarks of Unity Technologies or its affiliates in the U.S. and elsewhere (more info here). Other names or brands are trademarks of their respective owners.
  • Anonymous
  • Sign in
  • Create
  • Ask a question
  • Spaces
  • Default
  • Help Room
  • META
  • Moderators
  • Explore
  • Topics
  • Questions
  • Users
  • Badges