Search Unity

Resolved Show/Hide VisualElement Dynamically in UI Toolkit - Unity 2022.1.17f1

Discussion in 'UI Toolkit' started by Obscure045, Dec 2, 2022.

  1. Obscure045

    Obscure045

    Joined:
    Aug 2, 2022
    Posts:
    18
    How To Show And Hide Properties Dynamically using UI Toolkit. For Example, a Boolean "A" has properties "A1" and "A2". If Boolean "A" is true show the properties but if it is false hide the properties.

    I have tried doing it but the inspector doesn't update the UI, as shown in the GIF:
    Box Show Hide UI Inspector.gif

    The Unity Inspector doesn't update in accordance with the bool.
    Below is my code:
    Code (CSharp):
    1. root.Q<VisualElement>("dependantBox").style.display = theBool ? DisplayStyle.Flex : DisplayStyle.None;
    This line of code is inside the function "CreateInspectorGUI". Not Inside the OnInspectorGUI.

    I am using UI Toolkit for the inspector.
     
  2. Obscure045

    Obscure045

    Joined:
    Aug 2, 2022
    Posts:
    18
    I have found the Solution. Previously, I was changing the style in CreateInspectorGUI. It is called only when the Inspector is Created (Unlike OnInspectorGUI). This is what caused the glitch.
    Now, I register a callback to the Button and change the style inside of it.
    like this:
    Code (CSharp):
    1. root.Q<Button>("toggler").clicked += () => {
    2.         VisualElement visualElement = root.Q<VisualElement>("visualElement");
    3.         visualElement.style.display = (visualElement.style.display == DisplayStyle.Flex) ? DisplayStyle.None : DisplayStyle.Flex;
    4. };
    Now it works perfectly.
     
    Janni00 likes this.