Search Unity

How to get controlID of specific control?

Discussion in 'Editor & General Support' started by BeautifulRiver, Jun 3, 2018.

  1. BeautifulRiver

    BeautifulRiver

    Joined:
    Sep 19, 2016
    Posts:
    47
    I created a window in the editor window. There are some buttons, toggles and other controllers inside my window. How do I get the controlID of the controls? I searched Unity document and found the API GUIUtility.GetControlID() may be used in this regard. I found many examples of the usage as follows:

    GUIUtility.GetControlID(FocusType.Passive);

    But how does this method 'know' which control I am interested in? How do I specify which control I am interested in?
     
  2. Madgvox

    Madgvox

    Joined:
    Apr 13, 2014
    Posts:
    1,317
    GetControlID returns an id for you to use for your own controls. It doesn't search for and return the id of other controls. There is no such thing in IMGUI, nor would one make much sense, as there is no persistent state intrinsic to IMGUI. Internally, Unity GUI methods use GetControlID themselves to get a controlID for the control. The best you can do is get the last used control id, which unfortunately has no public API.

    Usually, you can get the last control id used if you use this snippet directly after calling whatever GUI function:

    Code (CSharp):
    1. someBool = GUILayout.Toggle( someBool, "Some Bool" );
    2. var lastControlID = GUIUtility.GetControlID( FocusType.Passive ) - 1;
    But it's not the most reliable method, depending on the control. Alternatively, you can get the internal lastControlID field directly through reflection:

    Code (CSharp):
    1. var type = typeof( EditorGUIUtility );
    2. var field = type.GetField( "s_LastControlID", BindingFlags.Static | BindingFlags.NonPublic );
    3. var lastControlID = (int)field.GetValue( null );
    However, I have a feeling that since you don't fully understand how controls work in IMGUI (perfectly understandable, the whole system is fairly obtuse), you probably are approaching whatever problem you have from the wrong direction. If you explain what you're trying to do, I could help point you in the right direction.
     
    Last edited: Jun 3, 2018
    RomanSpai and BeautifulRiver like this.
  3. BeautifulRiver

    BeautifulRiver

    Joined:
    Sep 19, 2016
    Posts:
    47
    Hi @Madgvox

    Thanks for the explanation! Indeed I was trying to understand IMGUI by Googling but failed to find a good overview.

    My original idea is to save the controlID of various controls at startup, and use the numbers to determine which control is active using GUIUtility.hotControl(). May be this approach is wrong. My mental model of how IMGUI works is probably wrong. Do you know where can I read more about IMGUI?
     
  4. Madgvox

    Madgvox

    Joined:
    Apr 13, 2014
    Posts:
    1,317
    Getting controlIds at startup is simply not possible because they don't exist. If you do need to get the control then you'll need to save the control ids directly after creating them, like in my first example. For what purpose do you want to get the current active control?

    Honestly I'm not entirely sure if there's a single place that will give you an overall picture of how IMGUI works in unity. I learned the hard way over years of diving into the documentation, the decompiled source, and many various comment threads.
     
    Jamesika and BeautifulRiver like this.
  5. BeautifulRiver

    BeautifulRiver

    Joined:
    Sep 19, 2016
    Posts:
    47
    @Madgvox Thanks for the suggestion. I may rewrite the whole code.