Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 12 Next capture
2021 2022 2023
1 capture
12 Jun 22 - 12 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 JVLVince · May 04, 2018 at 10:21 AM · editorguilayoutguiutility

Why control Id retreated from GUIUtility different from GUIUtility.keyboardControl

Nice day guys,

This is a question about UnityEdior script since I go around google and I can not find out the answer so I ask here, if my question content has existed somewhere please slightly point me and I will delete this question.

My problem is I don't understand how the GUIUtility.keyboardControl work. I create a textArea, I get the control Id of this textArea component, then I compare with the GUIUtility.keyboardControl while I edit text inside this testArea, and I saw that the control Id of my textarea is different from which I got from GUIUtility.keyboardControl (the control ID which I got from GUIUtility.keyboardControl always lesser than my textArea control Id 1 unit). As my expect it's must be equal. Do my understanding wrong?

(I take a look in the sources code of Unity but the GUIUtility.keyboardControl code has been move to native code? maybe ... so I can not find out any hint there)

This is my Test Code:

 private void OnGui() {
             m_textContent = GUILayout.TextArea(m_textContent);
             int controlId = GUIUtility.GetControlID(FocusType.Keyboard);
             m_evt = Event.current;
             if (m_evt.isKey) {
                 Debug.Log("kb Control: " + GUIUtility.keyboardControl + "this id: " + id);
             }
  }

And I got something like: kb Control: 542 this id + 543
Thanks in advanced, Vince

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 Bunny83 · May 04, 2018 at 10:57 AM

Well the main problem here is that the ControlID that you generate is not the same that is generated inside the TextArea method,. When you call GetControlID 3 times in a row you get 3 different IDs back. That's the main point of the control IDs. The important thing is that the control ID generation is reset each time OnGUI is called so the same code will generate the same IDs.


The immediate mode GUI does not have "controls" or "components" in the sense of objects. The "control methods" like TextArea just handle events immediately. So if it's called during a repaint event it will actually render a text input field ad hoc. Have a look at the GUILayout.TextArea method. Inside DoTextField it "generates" a control ID for this control. You can't really access or aquire the control ID as it's only used internally. The reason why we actually have access to "GUIUtility.keyboardControl" is to implement your own controls.


The TextArea is rather complicated since it actually uses a state object, but only a text editor object. It's easier to understand how GUI controls work when you look at a simpler example like the RepeatButton for example. If you want to know more about the IMGUI system, have a look at my GUI crashcourse.


The intended way to identify a certain control is to use GUI.SetNextControlName and then use GUI.GetNameOfFocusedControl to determine which control has the focus. You can also use GUI.FocusControl to focus a named control.

Comment
Add comment · Show 3 · 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 JVLVince · May 04, 2018 at 03:06 PM 0
Share

First of all, tks for the clear explanation, Bunny. every time u answer a question, I'm so sure I will able to get more knowledge. ^^

When you call GetControlID 3 times in a row you get 3 different IDs back.

The important thing is that the control ID generation is reset each time OnGUI is called so the same code will generate the same IDs.

Now I understood, I'll never be able to get the exact control ID of my TextArea. In my Imagination of "ID", it has to absolutely, but after I understood what it's done, It's made me think that the control ID is so relatively. ~_~

The reason why we actually have access to "GUIUtility.keyboardControl" is to implement your own controls.


Yes, I try to customize a TextArea with onValueChange event. That's the reason why I want to get the ID of TextArea. And tks for the hint that you gave me. It helps a lot.

avatar image Bunny83 JVLVince · May 04, 2018 at 03:24 PM 1
Share

Well a quick an dirty "on value changed" check could be done like this:

 string tmp = GUILayout.TextArea(m_textContent);
 if (tmp != m_textContent)
 {
     m_textContent = tmp;
     // changed, yeay ^^
 }

Though it's possible to "guess" the right controlID by allocating your own just before the actual control and assume the control gets the next one. This however does not necessarily work in all cases since there are different GetControlID overloads which take in "hints". Also the implementation could change so the IDs might not be generated in order

avatar image JVLVince Bunny83 · May 05, 2018 at 03:31 PM 0
Share

Tks Bunny.

$$anonymous$$y idea from the beginning is custom a text editor that it's can catch the event when the key is down, and call some check there, then invoke the on value change callback :) but I think with my understanding of unity editor now, the dirty way is the best fit for me T__T, custom a text editor is still out of my reaching. :( swim$$anonymous$$g in unity editor code is so interesting lol.

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

81 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 avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image

Related Questions

serializedObject with EditorGUILayout.Popup in C# 1 Answer

EditorWindow: Use toggle to disable/enable other controls? 1 Answer

How to get proper layout for an objectfield 1 Answer

TextArea tab width 0 Answers

Is there a way to change the text size for a Vector3 PropertyField in a custom Inspector? 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