Get Enum from SerializedObject

Hey all

Simple question here that I’m failing to find any answers for

Was curious if anyone knows how to fetch an enum value from a SerializedObject. I tried fetching with the boxedValue call once you find the property, but that seems to be failing and spitting out a null.

What are you trying to do with serializedobject?

An enum is just an int value, so just use intValue. It’s not clear what and where you actually need this. Be aware that the SerializedObject / SerializedProperty classes are editor classes and can not be used in any kind of runtime script. If you do you can no longer build your game.

ps: in the recent Unity versions they added the “enumValueFlag” property. However that’s literally just a direct alias for the intValue. Keep in mind, that the enums underlying type can be changed (could be byte, short, “int” which is the default, long, or any other integral type except char). So an enum itself is just a fancy alias for the underlying type with additional constant values. It’s treated like a seperate type which gives you neat type safety. However you can always cast an enum to / from its underlying type, even to values that do not exist in the enum.

3 Likes

Ahh okay, thanks for this. I haven’t worked with Enums in editor scripts before so this is new to me, thanks for the info! :smile:

If this is needed for generic code for getting the value of any enum, then one small additional detail is that SerializedProperty.longValue can be used to also support the rare edge cases where an enum has an underlying type of uint (which is sometimes useful for supporting larger flags values).

1 Like