Restricting options in EnumField

It is often the case within enums that you may have options that are used internally to represent invalid/error states, but would never be a valid selection to set in a prefab or piece of configuration data. Therefore, when building a custom editor window and/or custom inspector for use by non-programmers, I would like to restrict the choices in some way - either grey out the options that I do not want to be selectable in that custom editor, or leave them out entirely.

I don’t see any way to do that with EnumField as it exists right now. I was thinking about creating a custom control using a Dropdown, but I’m not aware of a way to set that up cleanly in a way that I can use the binding path to trivially bind that control to the enum variable - the connection between the model and the view is going to be a lot messier than with something as simple as an EnumField.

Is there anything I’m missing? Does anyone know of a relatively easy way to restrict the options in an EnumField, or to replicate the functionality of an EnumField in a custom control that also allows for filtering the options displayed, while still allowing simple binding to an enum variable?

After spending a while digging through documentation about custom controls, as well as through the decompiled code for EnumField, it seems like it will be possible to create a custom control that extends BaseField<Enum> and binds nicely to an enum variable in the same way that EnumField does.

However, it appears that (perhaps for performance reasons?) EnumField pre-caches an EnumData struct with data about the particular Enum type that it is working with, and all of that uses a set of EnumDataUtility helper functionality that is internal to Unity.

I’m going to continue trying to find a way to either replicate or avoid the need for these helper functions, but this definitely seems like an excessive number of hoops to jump through when all I want is to filter out some options from an EnumField but still have it set its bound field when an option is chosen.

I’m looking at exactly this same issue myself! Annoying that EnumField does not have any equivalent of the checkEnabled callback on EnumPopup.

You could mark those Enum values with the [Obsolete] attribute for a quick and dirty way to hide them.

Or you could use a PopupField or PopupField. The PopupField has a choices property that you can use to specify which options are available.

The [Obsolete] approach only works if you want to hide the same options in every context, though.

The PopupField approach does work, but only if you want to completely re-implement everything that EnumField does under the hood. I ended up creating an extended version of EnumField that uses reflection to rip out the unsupported data types in SetValueWithoutNotify(). Pretty hacky but works and doesn’t take too many lines of code.