When this code runs
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.
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
This is the UXML file from which my custom editor is created:
<UXML
xmlns="UnityEngine.UIElements"
xmlns:ed="UnityEditor.UIElements"
xmlns:t="Tankmania.EditorExtensions">
<IMGUIContainer name="script-field" />
<TextField label="Component Name" binding-path="componentName" />
<ed:EnumField label="Reference Type" binding-path="referenceType" />
<t:IndentScope name="var-reference-container" indent-level="1">
<TextField label="Variable Name" binding-path="varName" />
</t:IndentScope>
<t:IndentScope name="method-reference-container" indent-level="1">
<TextField label="Method Name" binding-path="methodName" />
<ed:PropertyField name="methodParameters" binding-path="methodParameters" />
</t:IndentScope>
<VisualElement name="validate-btn">
<t:Space />
<Button text="Validate" />
</VisualElement>
</UXML>
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.
m_Root
is the root element to which every other VisualElement is added.
m_Root = new VisualElement();
VisualTreeAsset tree = AssetDatabase.LoadAssetAtPath<VisualTreeAsset>(THIS_FOLDER_PATH + "/ComponentReferenceHandlerEditorTree.uxml");
tree.CloneTree(m_Root);
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.