Search Issue Tracker
By Design
Votes
0
Found in [Package]
1.0.0-preview.14
Issue ID
1320302
Regression
Yes
Child count of VisualElement is 0 when using in CreateInspectorGUI
How to reproduce:
1. Open attached project "UI Toolkit Regression.zip"
2. Open sample scene
3. Select Main Camera
4. See "Grab Params Binding" component
Expected result: There is a name for every checkbox in the component
Actual result: Only the checkboxes are visible since the child count of the VisualElement is 0
Reproducible with - 2020.2.4f1 (1.0.0-preview.14), 2020.3.0f1 (1.0.0-preview.14), 2021.1.0f1 (1.0.0-preview.14), 2021.2.0a9 (1.0.0-preview.14)
Not reproducible with - 2018.4.33f1, 2019.4.22f1, 2020.2.3f1 (1.0.0-preview.14)
Add comment
All about bugs
View bugs we have successfully reproduced, and vote for the bugs you want to see fixed most urgently.
Latest issues
- Crash on DynamicHeapAllocator::CreateTLSFBlock when opening a specific project
- Scene flickers when using Mesh Output with SpriteLit Shader in 2D project
- “Cache Management” text is misaligned in Preferences > Package Manager
- [macOS] network permission shows the name of previously closed app when different app requests network access
- Delta value returned by mouse movementsMonitor is different when changing Screen Resolution
Resolution Note:
This is actually by design. PropertyFields will only contain UI inside once they are bound to a SerializedProperty, because which elements are created depend on that property and cannot be known ahead of time. Binding happens after all CreateInspectorGUI()s have been called by the InspectorWindow. Furthermore, new in 2020.2.4+, binding is now time-sliced and so there is chance that there won't be any UI inside a PropertyField even 1 or 2 frames after the creation of the Inspector.
To reliably know when a PropertyField has been "filled" with UI, it's recommended to use the GeometryChangeEvent. This event is called every time an element changes size, including the _first_ time the element is drawn. You can use the first time its called to know when the PropertyField has first been "rendered" or layed out. You can also use it to check when there is UI inside (after which, it's best to UnregisterCallback<GeometryChangeEvent>()).
I also tried the project attached. The scheduler-based approach works, but it's not ideal. It's always better to drive UI via events (for performance). However, I was able to fix the example's console errors by changing:
properties.ForEach(f => f[1][0][1].visible = (f[0] as Toggle).value);
to:
properties.ForEach(f =>
{
if (f.Q<PropertyField>().childCount > 0)
f.Q<PropertyField>().Q(className: "unity-base-field__input").visible = f.Q<Toggle>().value;
});
inside:
GrabParamsEditor.CreateInspectorGUI()