I have created a custom DropdownField which seems to work. Except that I get a strange error:
Field type Editor.Elements.PetDropdownField is not compatible with PPtr<$Pet> property “pet”
UnityEditor.InspectorWindow:RedrawFromNative ()
and the DropdownField does not seem to bind to the object.
DropdownField
using System.Collections.Generic;
using Scripts;
using UnityEngine;
using UnityEngine.UIElements;
namespace Editor.Elements
{
public class PetDropdownField : PopupField<Pet>
{
public new class UxmlTraits : BaseField<Pet>.UxmlTraits { }
public new class UxmlFactory : UxmlFactory<PetDropdownField, UxmlTraits> { }
private static List<Pet> Choices() => new(Resources.LoadAll<Pet>("Pets"));
public PetDropdownField() : base(
"Pets",
Choices(),
-1,
s => s?.name ?? "unknown",
s => s?.name ?? "unknown"
) { }
}
}
Scriptable Objects
using UnityEngine;
namespace Scripts
{
[CreateAssetMenu(fileName="new Owner", menuName="Owner")]
public class Owner : ScriptableObject
{
[SerializeField] private Pet pet;
}
}
namespace Scripts
{
[CreateAssetMenu(fileName="new Pet", menuName = "Pet")]
public class Pet : ScriptableObject
{
}
}
Editor
using Scripts;
using UnityEditor;
using UnityEngine;
using UnityEngine.UIElements;
namespace Editor
{
[CustomEditor(typeof(Owner))]
public class OwnerEditor : UnityEditor.Editor
{
[SerializeField] private VisualTreeAsset template;
public override VisualElement CreateInspectorGUI() => template.CloneTree();
}
}
<ui:UXML xmlns:ui="UnityEngine.UIElements" xmlns:uie="UnityEditor.UIElements" xsi="http://www.w3.org/2001/XMLSchema-instance" engine="UnityEngine.UIElements" editor="UnityEditor.UIElements" noNamespaceSchemaLocation="../../../UIElementsSchema/UIElements.xsd" editor-extension-mode="False">
<Editor.Elements.PetDropdownField label="Pet" binding-path="pet" />
</ui:UXML>
Does anyone know why this control does not bind?