Hi everyone,
The solution is right after the problem.
PROBLEM:
I have 2 C# classes, one for a slot and another for an item:
[System.Serializable]
public class ItemSlot
{
[SerializeField] int amount = 1;
[SerializeField] Item item;
public ItemSlot(Item item, int amount)
{
this.item = item;
this.amount = amount;
}
}
[System.Serializable]
public class Item
{
[SerializeField] ItemData data;
}
I have other classes that derive from the Item class. What I would like to do is having a dropdown with all the different items and when one is picked, show its property drawer.
[CustomPropertyDrawer(typeof(ItemSlot))]
public class ItemSlotPreview : PropertyDrawer
{
public override VisualElement CreatePropertyGUI(SerializedProperty property)
{
VisualElement root = base.CreatePropertyGUI(property);
root.Add(new DropdownField("Item", new List<string>() { "Weapon", "Sword" }, 0));
root.Add(new PropertyField(new Item()));
return root;
}
}
Also in this example, VisualElement root = base.CreatePropertyGUI(property);
gives root as null and when I try the following:
[CustomPropertyDrawer(typeof(ItemSlot))]
public class ItemSlotPreview : PropertyDrawer
{
public override VisualElement CreatePropertyGUI(SerializedProperty property)
{
return base.CreatePropertyGUI(property);
}
}
Here is the result:
SOLUTION:
To avoid reading the entire conversation, here is the answer to that question.
The solution is a bit more complex than with UnityEngine.Object since we cannot use InspectorElement for a simple C# class.
I found the following git repository, which works great, but it does not use VisualElement, which I wanted to use to easily make changes.
@spiney199 provided the following which is 2 simple classes, the attribute + custom inspector that uses VisualElement.