Hi everyone,
I'm having an issue with enum values being reflected as their numeric values in the inspector when used in a nested array with Unity. Here's a cut down example of the code I'm trying to use.
public enum EnumType
{
Enum1,
Enum2
}
public class ContainerClass : MonoBehavior
{
[Serializable]
public class Constants
{
[Serializable]
public class SubConstants
{
/*...*/
public EnumType EnumVals[] = null;
}
public SubConstants SubConsts = new SubConstants();
}
[SerializeField]List<Constants> _Attributes = new List<Constants>();
}
So what I'm seeing is that I can create and modify the list of Constants but when I go to edit the array of EnumType in SubConstants, it only shows numeric values and not a drop down box of values. I've also tried making the List an array with no change.
yoyo
2
Seems like the Unity Inspector has a bug with reflected type discovery. Rearranging the nesting of the classes as follows makes it work:
using System;
using System.Collections.Generic;
using UnityEngine;
public enum EnumType
{
Enum1,
Enum2
}
[Serializable]
public class SubConstants
{
/*...*/
public EnumType[] EnumVals = null;
}
public class ContainerClass : MonoBehaviour
{
[Serializable]
public class Constants
{
public SubConstants SubConsts = new SubConstants();
}
[SerializeField]List<Constants> _Attributes = new List<Constants>();
}
In case the above does not work for you. It is possible to work around it using PropertyDrawers
Editor code
[CustomPropertyDrawer(typeof(EnumAttr))]
public class EnumEditor : PropertyDrawer {
public override void OnGUI (Rect position, SerializedProperty prop, GUIContent label) {
System.Enum v = EditorGUI.EnumPopup (position, label, (System.Enum)System.Enum.ToObject ((this.attribute as EnumAttr).enumType, prop.intValue));
prop.intValue = System.Convert.ToInt32 (v);
}
}
Class code
public class EnumAttr : PropertyAttribute {
public System.Type enumType;
public EnumAttr (System.Type enumType) {
this.enumType = enumType;
}
}
Then for a field which is an enum
[EnumAttr(typeof(Side))]
public Side side = Side.Side1;