hierarchyMode inside IMGUIContainer

Hi, I’m using UIToolkit for some behaviour inspectors, but there are some controls that I’m still implementing with IMGUI, so I’m using IMGUIContainer for those. I’ve chased a weird bug with IMGUIContainer and I’ve arrived to the conclusion that, whenever I set hierarchyMode to true inside the onGUIHandler delegate, IMGUI controls such as toggles and popups stop working. They don’t detect clicks.

Has this happen to anyone else? Any ideas on how to solve it or why it happens? If anyone has any thoughts on this, I’d really love to read them. Thank you all very much for your time, really; I appreciate it a lot. The following is an example of the problem; imagine it in a custom editor for a MonoBehaviour.

public override VisualElement CreateInspectorGUI()
{
    VisualElement root = new VisualElement();
    inspectorContainer = new IMGUIContainer(OnGUICallback);
    root.Add(inspectorContainer);
    return root;
}

void OnGUICallback()
{
    //If I remove the following line, things work.
    EditorGUIUtility.hierarchyMode = true;

    EditorGUI.BeginChangeCheck();
    EditorGUILayout.Toggle("Test control", false);
    if (EditorGUI.EndChangeCheck())
        //The following is only called if one clicks the toggle very quickly many times.
        Debug.Log("toggled");
}

I think fixed it. At least for myself. It seems for some reason EditorGUIUtility.labelWidth starts at 120 inside my IMGUI containers. And yet, when hierarchyMode is turned on, the editor draws things like if labelWidth was 0. And yet, it manages clicks like they had the labelWidth that is set. So, at least when I don’t need a particular labelWidth, I can set it to 0 so both the drawn controls and the click detection boxes match.

EDIT
It seems setting the labelWidth, even if it’s to something other than zero, fixes it. I guess something must be initialized from behind the scenes when you set it.

EDIT 2
I read the code from the reference on Github. I don’t see anything that’s would fix this when setting labelWidth. The weirdest part for me is that this only happens in IMGUI Containers. Anyway, this apparently has fixed things for me, and, although it eats me inside a little that I don’t know why this happens, I give up. After a long day and a half on this small thing, I feel too tired to get to the bottom of it. I hope this information helps some one else in the future.

3 Likes