Access an enum in a custom class in custom editor

Hi everyone. My custom class is

[System.Serializable]
public class Property
{
    public enum properties
    {
        AddCalories,
        AddWater,
        AddVitaminA,
        AddVitaminB1,
        AddVitaminB2,
        AddVitaminB3,
        AddVitaminB5,
        AddVitaminB6,
        AddVitaminB12,
        AddVitaminC,
        AddVitaminD,
        AddVitaminE,
        AddVitaminK,
        AddCalcium,
        AddChromium,
        AddIron,
        AddZinc,
        AddRadiation
    }
    public properties property;
    public float value;

}

Then I have a game object with a script which has an array of this class “Property”.
The script is very simple:

public class HeartBeat_FoodItem : MonoBehaviour {
    public Property[] properties;
}

The thing I want to do is detect whenever a certain property in the enum is selected (in the inspector), so a label could appear in that specific area (below the value float field).
73109-inspector.jpg

The script for the editor is this:

using UnityEngine;
using UnityEditor;

[CustomEditor(typeof(HeartBeat_FoodItem))]
public class HeartBeat_FoodItemEditor : Editor
{
    public override void OnInspectorGUI()
    {
        serializedObject.Update();

        SerializedProperty list = serializedObject.FindProperty("properties");
        EditorGUILayout.PropertyField(list, true); //true means includeChildren=true, which allows me to expand and collapse the array.

        EditorGUILayout.HelpBox("The value is measured in µg (micrograms).", MessageType.Info);

        serializedObject.ApplyModifiedProperties();
    }
}

I hope I provided enough information. Thanks in advance.

One option is use the Unity - Scripting API: EditorGUILayout.EnumPopup function to draw and get the enum value. In this case you would use the Editor’s target, rather than using the serializedObject.

Alternatively, you can get the integer value of the serializedProperty, with Unity - Scripting API: SerializedProperty.intValue, then explicitly cast the int into to your enum type.

P.S. I wouldn’t actually name stuff “property”, or “properties”. They might not be reserved words, but they are used often in unity.

Glurth’s answer helped me, so here’s a piece of sample code I came up with based on Glurth’s answer. Hope it helps someone.

Here is the editor code:

[CustomEditor(typeof(item))]//customizes the item class in the inspector
public class itemCustomInspector : Editor
{
    public override void OnInspectorGUI()
    {
        item itemScript = (item)target;
        itemScript.type = (itemType)EditorGUILayout.EnumPopup("Item Type:", itemScript.type); //this is how to use the enumpopup that he mentioned.
    }
}

Here is the code from the script that will have its inspector modified

public enum itemType { sword, axe, boots, breastplate, helmet, leggings, shield };

public class item : MonoBehaviour
{        
    public itemType type;
}

Necro since top google result.

From Glurth:

“Alternatively, you can get the
integer value of the
serializedProperty”

To elaborate, if you wish to use the serialized property route, you can do:

serializedObject.Update();
 
SerializedProperty myEnum = serializedObject.FindProperty("enumName");
EditorGUILayout.PropertyField(myEnum);
 
if (myEnum.intValue == (int)EnumType.ElementOne) {
    // do something
} 
if (myEnum.intValue == (int)EnumType.ElementTwo) { 
    // do something else etc... }

serializedObject.ApplyModifiedProperties();