Search Unity

  1. Megacity Metro Demo now available. Download now.
    Dismiss Notice
  2. Unity support for visionOS is now available. Learn more in our blog post.
    Dismiss Notice

GUI.SetNextControlName ignored after initial values

Discussion in 'Immediate Mode GUI (IMGUI)' started by BenMeijering, Jul 27, 2012.

  1. BenMeijering

    BenMeijering

    Joined:
    Dec 1, 2011
    Posts:
    8
    Hi everyone,

    I am writing an asset editor by deriving from the Editor class and came across the following issue.

    Example
    Code (csharp):
    1.  
    2.     private bool m_Postfix = true;
    3.    
    4.     public override void OnInspectorGUI()
    5.     {
    6.         if (Event.current.type == EventType.Layout)
    7.         {
    8.             m_Postfix = !m_Postfix;
    9.         }
    10.        
    11.         string postfix="";
    12.         if (m_Postfix)
    13.         {
    14.             postfix = "_TRUE";
    15.         }
    16.         else
    17.         {
    18.             postfix = "_FALSE";
    19.         }
    20.        
    21.         GUI.SetNextControlName("A" + postfix);
    22.         GUILayout.TextField("A");
    23.         GUI.SetNextControlName("B" + postfix);
    24.         GUILayout.TextField("B");
    25.        
    26.         Debug.Log(GUI.GetNameOfFocusedControl());
    27.     }
    28.  
    Now by selecting the asset for which this editor is shown and by shifting focus back and forth by clicking on the two textfields, the only two values which are logged are "B_TRUE" and "A_FALSE".
    It seems that somehow the controlname becomes fixed.

    In the actual project I am working on I have a sequence of textfields representing data in an array, next to each texfield is a delete button by which you can remove an element from the array, After I deleted an element I noticed that the focused control name (GUI.GetNameOfFocusedControl()) did not correspond to the array element that currently had focus, but rather to an array element which had been deleted.

    Does anyone have any suggestions on how to solve this problem?
    And is this actually a bug, or am I just using it wrong ?

    Any help would be appreciated.

    Cheers,

    Ben
     
    Last edited: Jul 27, 2012
  2. ar0nax

    ar0nax

    Joined:
    May 26, 2011
    Posts:
    485
    I am having the same problem with 2 functions in GUI, they exclude each other, in one 2 controls with names: name1, name2... next function 3 controls with names: name3, name4, name5... there's a function which allows me to switch between the 2 functions, and when i switch the names of controls end up mixed.
     
  3. Michael-Ryan

    Michael-Ryan

    Joined:
    Apr 10, 2009
    Posts:
    184
    I recently reported a bug on this issue. My guess is that the control name is associated with the control's internal ControlID, and that each ControlID can only be associated with a name once. Future calls to assign a new name are ignored. This becomes an issue when you have a dynamic GUI that re-uses ControlIDs.

    See this thread for details.
     
  4. Orbcreation

    Orbcreation

    Joined:
    May 4, 2010
    Posts:
    231
    Is there any news on this thread?
    Or has anyone made a good workaround?
     
  5. unimechanic

    unimechanic

    Joined:
    Jan 9, 2013
    Posts:
    155