When you render a field that’s serialised via SerializeReference with EditorGUI.PropertyField, all the enums inside that object show up as integers. Minimal example to reproduce this:
using System;
using UnityEngine;
public enum MyEnum
{
OneValue,
AnotherValue
}
[Serializable]
public class MySerializableClass
{
public MyEnum EnumField = MyEnum.AnotherValue;
}
public class TestScript : MonoBehaviour
{
[SerializeField] private MySerializableClass serializedField = new MySerializableClass();
[SerializeReference] private MySerializableClass serializedReference = new MySerializableClass();
}
And then create a simple property drawer:
using UnityEditor;
using UnityEngine;
[CustomPropertyDrawer(typeof(MySerializableClass))]
public class MySerializableClassDrawer : PropertyDrawer
{
public override float GetPropertyHeight(SerializedProperty property, GUIContent label)
{
return EditorGUI.GetPropertyHeight(property, true);
}
public override void OnGUI(Rect position, SerializedProperty property, GUIContent label)
{
Debug.Log(property.propertyPath);
EditorGUI.BeginProperty(position, label, property);
EditorGUI.PropertyField(
position,
property,
true);
EditorGUI.EndProperty();
}
}
See attached screenshot for the result.
I’m going to report this as a bug as well (because I think it is one?). But in the meantime, does anyone have a workaround? Manually rendering the property field isn’t really an option in my actual project since the enums show up at various depths in various objects.
I tried writing a custom property drawer just for that enum type, but that actually fails at property.enumValueIndex, suggesting to me that enums don’t actually get serialised as enums inside serialised references?
EDIT: Retrieving the enumValueIndex via property.intValue does work for both serialised fields and serialised references. But as a workaround, it still means I’ll need to implement one property drawer for each enum type I’ve got (probably quite doable with a generic base class, but still a pain).
EDIT2: Corresponding bug report: http://fogbugz.unity3d.com/default.asp?1197948
EDIT3; Turns out this bug is already known and currently marked as WONTFIX: Unity Issue Tracker - Enum fields are shown as int fields in the Inspector window when tagging them with SerializeReference attribute. I’m still interested in workarounds that don’t involve having a custom property drawer for every enum type, and avoiding ever using property.enumValueIndex.
