Hi. I’m doing a custom editor window and I’m trying to bind an EnumField to an enum property of my custom enum type. But I get the warning:
Field type UnityEditor.UIElements.EnumField is not compatible with Enum property “myEnum”
The enum field works but it isn’t bound so my data object does not update and so when I hit play on the editor, the field loses the information and is set to default. In contrast, the text field works as expected since it retains the correct value.
This is my code.
//DataObject.cs
//Object for binding
public class DataObject : ScriptableObject
{
public MyEnum myEnum;
public string myString;
void OnEnable()
{
hideFlags = HideFlags.HideAndDontSave;
if (myString == null)
myString = "";
}
}
//TestWindow.cs
public enum MyEnum
{
Foo1,
Foo2,
Foo3
}
public class TestWindow : EditorWindow
{
[SerializeField]
private DataObject dataObject;
private TextField textField;
private EnumField enumField;
[MenuItem("Tools/Test Window")]
private static void ShowEditor()
{
TestWindow window = GetWindow<TestWindow>();
window.titleContent = new GUIContent("Test Window");
}
void OnEnable()
{
textField = new TextField("Text");
textField.bindingPath = "myString";
textField.style.width = StyleKeyword.Auto;
textField.style.height = 20;
rootVisualElement.Add(textField);
enumField = new EnumField("Enum",MyEnum.Foo1);
enumField.bindingPath = "myEnum";
enumField.style.width = StyleKeyword.Auto;
enumField.style.height = 20;
rootVisualElement.Add(enumField);
if (dataObject == null)
{
dataObject = CreateInstance<DataObject>();
dataObject.myString = "default string";
textField.value = dataObject.myString;
}
else
{
textField.value = dataObject.myString;
enumField.value = dataObject.myEnum;
}
var so = new SerializedObject(dataObject);
//The same thing happens if I try to bind to the field directly instead
rootVisualElement.Bind(so);
}
}
I found this post EnumField does not work for custom Enum type
But they solved the issue on UXML. Is this somehow related to my problem, where I’m trying to do the binding by code? What am I missing? Or is this a bug?