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
4
Question by Michael-Ryan · Nov 15, 2012 at 07:05 AM · guireflectionguiutility

How can I get the last assigned ControlID?

Ideally, without using reflection, I would like a way to get the last ControlID assigned to a control. I'm dealing with a dynamic GUI with lots of TextFields and other complex controls, so I don't really want to rebuild every GUI control just so I can use GetControlID() and assign it to the controls myself via GUIStyle.Draw().

Apparently, assigning control name using GUI.SetNextControlName() and GUI.GetNameOfFocusedControl() do not function correctly with dynamic GUIs.

Is there any way to get the ControlID of the last control drawn? I'm looking for something like this:

 value = EditorGUILayout.TextField(value, style, options);
 int controlID = GUIUtility.GetLastControlID();

It seems like this may not possible, but perhaps I'm missing something in the API documentation.

How about using reflection?

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

2 Replies

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

Answer by numberkruncher · Oct 04, 2013 at 01:14 AM

Yes this would be a really useful function!

Here is a dirty trick which I have used in the past:

 int nextControlID = GUIUtility.GetControlID(FocusType.Passive) + 1;
 value = EditorGUILayout.TextField(value, style, options);

This may not always be 100% reliable, so be sure to test thoroughly!

Please vote for feature here:

http://feedback.unity3d.com/unity/all-categories/1/hot/active/4449--guiutilitygetlastcontrolid

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 Michael-Ryan · Oct 04, 2013 at 01:33 AM 0
Share

Thanks! Actually, I already requested the feature here: http://feedback.unity3d.com/unity/all-categories/1/hot/active/guiutilitygetlastcontrolid

Hopefully they can add that to the API.

avatar image numberkruncher · Oct 04, 2013 at 01:37 AM 0
Share

ah okay, I've added a +1 comment to it for you :)

avatar image Michael-Ryan · Oct 04, 2013 at 01:42 AM 0
Share

$$anonymous$$any thanks!

avatar image Jeff-Kesselman · Nov 11, 2013 at 08:33 PM 0
Share

This IS dirty, but its the only thing I've found that actually works :/

Gonna go +1 you

avatar image
3

Answer by Nevermind · Nov 27, 2013 at 11:55 AM

You can also cheat with a bit of reflection:

         public static FieldInfo LastControlIdField=typeof(EditorGUI).GetField("lastControlID", BindingFlags.Static|BindingFlags.NonPublic);
         public static int GetLastControlId(){
             if(LastControlIdField==null){
                 Debug.LogError("Compatibility with Unity broke: can't find lastControlId field in EditorGUI");
                 return 0;
             }
             return (int)LastControlIdField.GetValue(null);
         }

This only works in editor with EditorGUI.* calls, though: in-game GUI does not seem to store the last control ID anywhere.

Comment
Add comment · Show 1 · 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 marsonmao · Jun 24, 2015 at 03:27 AM 2
Share

the trick is great but need some update: typeof(EditorGUIUtility).GetField("s_LastControlID", BindingFlags.Static | BindingFlags.NonPublic);, the variable is now in EditorGUIUtility and name changed to s_LastControlID.

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

12 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

Related Questions

GUIUtility.hotControl on windows 0 Answers

Get ControlID of simple GUI button 4 Answers

GUI Rotation and Depth problem 1 Answer

Position jitter when using GUIUtility.RotateAroundPivot 0 Answers

Reflection and Specular Highlights: how to turn on/off? help pls! 3 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