- Home /
UIElements EnumField data binding
Hi! I'm currently making a custom editor window using the new UIElements API. Unfortunately there is not a lot of content right now to do research on this topic and the Unity documentation it's still a bit vague on some specific topics. So I'm hoping I can find someone here who has encountered this issue as well.
The problem I have is that I'm trying to bind an Enum, that is contained inside a scriptable object, to an EnumField (The one that ends up inheriting from VisualElement).
The documentation says that I have to create a SerializedObject out of my ScriptableObject and then bind that either to a parent VisualElement of the EnumField, or to the field itself using the Bind method. So that's what I did but I'm getting this warning and I find it a bit weird.
Field type UnityEditor.UIElements.EnumField is not compatible with Enum property "area"
How is my Enum property not compatible with EnumField? The weird thing is that I also have a TextField binded to a string property (on the same ScriptableObject of course) and that works just fine.
Here's the code that I'm using:
//Binding code -----------------------------------
//nodeData is an object of my custom type that derives from ScriptableObject
var so = new SerializedObject(nodeData);
propertyFieldsContainer.Bind(so); //Code line that issues the warning
stringField.value = so.FindProperty("id").stringValue; //Works as expected
//The enum field draws correctly (with the default value of the enum) but it is unresponsive
areaField.value = (MapArea) so.FindProperty("area").enumValueIndex;
//Scriptable Object code ------------------------
//It just contains these 2 members
public string id;
public MapArea area; //MapArea is the enum type
I also tried binding with the field itself but it is the same result. Is this a bug or am I doing something wrong? Please, someone help me!
Edit: Forgot to mention. Before the binding code I'm correctly assigning the bindingPath of those two property fields, so that is not the problem.
Turns out, support for binding EnumField to custom enums just got introduced in Unity 2019.3 (alpha version) and I'm working on v 2019.1 so that's why this fails. I filed a bug, and seems like it will be added to 2019.1 and 2019.2.
You can read about it in this forum post:
https://forum.unity.com/threads/enumfield-is-not-compatible-with-enum-property.692767/