Search Unity

  1. Unity Asset Manager is now available in public beta. Try it out now and join the conversation here in the forums.
    Dismiss Notice

PropertyField renders enums inside SerializeReference fields as integers

Discussion in '2019.3 Beta' started by menderbug, Nov 11, 2019.

  1. menderbug

    menderbug

    Joined:
    May 14, 2018
    Posts:
    26
    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:

    Code (CSharp):
    1. using System;
    2. using UnityEngine;
    3.  
    4. public enum MyEnum
    5. {
    6.     OneValue,
    7.     AnotherValue
    8. }
    9.  
    10. [Serializable]
    11. public class MySerializableClass
    12. {
    13.     public MyEnum EnumField = MyEnum.AnotherValue;
    14. }
    15.  
    16. public class TestScript : MonoBehaviour
    17. {
    18.     [SerializeField] private MySerializableClass serializedField = new MySerializableClass();
    19.     [SerializeReference] private MySerializableClass serializedReference = new MySerializableClass();
    20. }
    And then create a simple property drawer:

    Code (CSharp):
    1. using UnityEditor;
    2. using UnityEngine;
    3.  
    4.  
    5. [CustomPropertyDrawer(typeof(MySerializableClass))]
    6. public class MySerializableClassDrawer : PropertyDrawer
    7. {
    8.     public override float GetPropertyHeight(SerializedProperty property, GUIContent label)
    9.     {
    10.         return EditorGUI.GetPropertyHeight(property, true);
    11.     }
    12.  
    13.     public override void OnGUI(Rect position, SerializedProperty property, GUIContent label)
    14.     {
    15.         Debug.Log(property.propertyPath);
    16.         EditorGUI.BeginProperty(position, label, property);
    17.  
    18.         EditorGUI.PropertyField(
    19.             position,
    20.             property,
    21.             true);
    22.  
    23.         EditorGUI.EndProperty();
    24.     }
    25. }
    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: https://issuetracker.unity3d.com/is...agging-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
    .
     

    Attached Files:

    Last edited: Nov 12, 2019
    LeonhardP likes this.
  2. menderbug

    menderbug

    Joined:
    May 14, 2018
    Posts:
    26
    Looks like this actually did get fixed in 2019.3.0.11b.
     
  3. chadfranklin47

    chadfranklin47

    Joined:
    Aug 11, 2015
    Posts:
    226
    Weird. I'm getting this in 2021.3.8 LTS using UIElements.PropertyField, though it only happens sometimes. I'll update if I figure out why.
     
    Last edited: Aug 22, 2022
  4. chadfranklin47

    chadfranklin47

    Joined:
    Aug 11, 2015
    Posts:
    226
    I have a SerializedObject I create. Instead of calling serializedObject.Update(), setting it to a new serialized object every time averts the issue for some reason...
     
  5. SF_FrankvHoof

    SF_FrankvHoof

    Joined:
    Apr 1, 2022
    Posts:
    780
    Make sure your enum is set as "Serializable" (System.Serializable) or else Unity might have some issues.
     
  6. SF_FrankvHoof

    SF_FrankvHoof

    Joined:
    Apr 1, 2022
    Posts:
    780
    Unity - Scripting API: SerializedObject.Update (unity3d.com)
     
  7. chadfranklin47

    chadfranklin47

    Joined:
    Aug 11, 2015
    Posts:
    226
    Thanks for the help. I do have my enums set as Serializable, though I am using some other enums I am unable to change. The issue occurs in both cases. About SerializedObject.Update(), I'm aware that it updates the serialized object to the data of the inspected object. What I'm not sure about is why that would cause enum fields to display as integers...
     
    Last edited: Feb 25, 2023