Search Unity

Bug [UQuery] Query returning empty object, have to wait a frame for it to somehow turn normal

Discussion in 'UI Toolkit' started by matijas_05, Jun 25, 2019.

  1. matijas_05

    matijas_05

    Joined:
    May 8, 2017
    Posts:
    11
    When this code runs
    Code (CSharp):
    1. m_ReferenceType= m_Root.Q<EnumField>();
    the m_ReferenceType variable is fed with the values in the screenshot. In order for the variable to be fed with normal values I have to utilise the EditorCoroutines package - run the code above and wait a frame. Is this a bug or am I just misunderstanding how the UQuery system is supposed to work?

    Unity version - 2019.3.0a6.
     

    Attached Files:

    Last edited: Jun 25, 2019
  2. antoine-unity

    antoine-unity

    Unity Technologies

    Joined:
    Sep 10, 2015
    Posts:
    780
    Hello,

    This shouldn't be needed generally although some specialied elements like Listview and PropertyField may create their hierarchy after one frame.

    Can you share a bit more about the setup of our window?

    Thanks
     
  3. matijas_05

    matijas_05

    Joined:
    May 8, 2017
    Posts:
    11
    This is the UXML file from which my custom editor is created:
    Code (CSharp):
    1. <UXML
    2.     xmlns="UnityEngine.UIElements"
    3.     xmlns:ed="UnityEditor.UIElements"
    4.     xmlns:t="Tankmania.EditorExtensions">
    5.  
    6.     <IMGUIContainer name="script-field" />
    7.     <TextField label="Component Name" binding-path="componentName" />
    8.     <ed:EnumField label="Reference Type" binding-path="referenceType" />
    9.  
    10.     <t:IndentScope name="var-reference-container" indent-level="1">
    11.         <TextField label="Variable Name" binding-path="varName" />
    12.     </t:IndentScope>
    13.  
    14.     <t:IndentScope name="method-reference-container" indent-level="1">
    15.         <TextField label="Method Name" binding-path="methodName" />
    16.         <ed:PropertyField name="methodParameters" binding-path="methodParameters" />
    17.     </t:IndentScope>
    18.  
    19.     <VisualElement name="validate-btn">
    20.         <t:Space />
    21.         <Button text="Validate" />
    22.     </VisualElement>
    23. </UXML>
     
  4. antoine-unity

    antoine-unity

    Unity Technologies

    Joined:
    Sep 10, 2015
    Posts:
    780
    This looks like a very normal usage. What is
    m_Root
    ? The only thing I can think of is that the parent of
    rootVisualElement
    is sometimes not bound inside of
    EditorWindow.OnEnable()
    so if the uQuery starts from there it could fail.

    If that's not it then it looks like a legit bug.
     
  5. matijas_05

    matijas_05

    Joined:
    May 8, 2017
    Posts:
    11
    m_Root
    is the root element to which every other VisualElement is added.
    Code (CSharp):
    1. m_Root = new VisualElement();
    2. VisualTreeAsset tree = AssetDatabase.LoadAssetAtPath<VisualTreeAsset>(THIS_FOLDER_PATH + "/ComponentReferenceHandlerEditorTree.uxml");
    3.             tree.CloneTree(m_Root);
     
    Last edited: Jun 25, 2019
  6. uDamian

    uDamian

    Unity Technologies

    Joined:
    Dec 11, 2017
    Posts:
    1,231
    The query here works fine, as far as I can tell. But if you query right after you called CloneTree() you'll get elements with NaN or zero sizes. This is because the layout/styling passes have not yet run over your elements. You need to wait at least one frame.

    What you can do is register for the GeometryChangeEvent on your found EnumField. This event is usually sent when the element is resized but it is also sent the first time an element is initialized with size and style.

    Worth also noting that the EnumField you found is perfectly valid, apart from its size/styles. You can still set or read its actual value.
     
  7. matijas_05

    matijas_05

    Joined:
    May 8, 2017
    Posts:
    11
    Thanks!