Hey there,
i am once again confused and have a (basic) UiToolkit question.
I want to create a Editor only panel. Therefore I created a class TypeSettingsPanel
and inheritted from VisualElement
and I also created a uxml for it.
I try to Q<> my TypeSettingsPanel
class from my EditorWindow to pass data to it and set it up correctly. But for some reason I can’t … all my aproaches return null. Any idea why that could be ? I was under the assumption I can Q all classes which inherit from VisualElement
. If not, how can I get it?
uxml
<ui:UXML xmlns:ui="UnityEngine.UIElements" xmlns:uie="UnityEditor.UIElements" editor-extension-mode="True">
<ui:VisualElement name="typeSettingsPanel" style="flex-grow: 1; flex-shrink: 0; overflow: hidden;">
// more unrelated stuff here I guess
</ui:VisualElement>
</ui:UXML>
My Custom Visual Element
using Packages.GSK_CoreUtils.Editor.Ui;
using UnityEngine.UIElements;
public class TypeSettingsPanel : VisualElement
{
[UnityEngine.Scripting.Preserve]
public new class UxmlFactory : UxmlFactory<TypeSettingsPanel> { }
public TypeSettingsPanel()
{
}
}
My EditorWindowClass:
public class MyEditorWindow: EditorWindow
{
[SerializeField] private VisualTreeAsset _visualTreeAsset = default;
[SerializeField] private VisualTreeAsset _typeSettingsPanelTemplate = default;
private TypeSettingsPanel _typeSettingsPanel;
[MenuItem("Window/MyEditorWindow")]
public static void Show()
{
MyEditorWindow wnd = GetWindow<MyEditorWindow>();
wnd.titleContent = new GUIContent("MyEditorWindow");
}
public void CreateGUI()
{
VisualElement root = rootVisualElement;
VisualElement labelFromUXML = _visualTreeAsset.Instantiate();
root.Add(labelFromUXML);
// This works
TemplateContainer existingTemplateContainer = rootVisualElement.Q<TemplateContainer>("TypeSettingsPanel");
// this returns also null
TypeSettingsPanel typeSettingsPanel2 = existingTemplateContainer.Q<TypeSettingsPanel>();
// this works
VisualElement typeSettingsPanel3 = rootVisualElement.Q<VisualElement>("typeSettingsPanel");
// this cast returns null
TypeSettingsPanel typeSettingsPanel4 = typeSettingsPanel3 as TypeSettingsPanel;
// this returns null
var settingsPanel = rootVisualElement.Q<TypeSettingsPanel>();
var newTemplateContainer = _typeSettingsPanelTemplate.Instantiate();
root.Add(newTemplateContainer);
// this returns also null
var typeSettingsPanel = newTemplateContainer.Q<TypeSettingsPanel>();
}
}
I enriched the code with comments which lines return null or not for clarification. Any idea what I do wrong ?
Thanks a lot for your help!
Cxyda