Create a custom editor that displays variables depending on enum

I was wondering how to make a custom editor that does exactly what the title says, Im not really looking for someone to give me the code. Im more looking for good tutorials and or documentation on how i might be able to implement this. In my case i have a Item class that houses 4 variables. An eneum of ItemType. ItemData which houses the basic item data. ToolData which houses the basic tool data. and FoodData which again houses all the food data. I want each of these to be shown when the enum is set to the appropriate value. Ex enum ItemType = Food then ItemData and FoodData is shown. Any help is very much appreciated!

No better place to start than the manual: Unity - Manual: Create a Custom Inspector

That said I would avoid enums as they are an anti-pattern. Your code will become a mess if you over-rely on them, and your objects will be filled with redundant data only relevant in certain circumstances.

I would look to take a data-driven approach, along with a “Tell, don’t ask” stance with dealing with item and tool effects. Some approaches can be to outline different types of functionality with interfaces (GetComponent works with interfaces). Or if items are represented by pure C# objects, take advantage of polymorphism, perhaps even emulating a component structure (even using SerializeReference to alloy polymorphic serialisation). Scriptable objects can also be used for pluggable, varying behaviour.

2 Likes