Essential Odin attributes part 2- [MinMaxSlider()]

A common thing in game development is getting a random number between a minimum and maximum.

Maybe something has a min and max attack damage, attack range, reaction time.

Or maybe you need to spawn a min and max number of things for a procedural level.

For all of these cases, we need to control the min and max. Which is where our Odin attribute  [MinMaxSlider()]  comes in.

MinMax is a set of two values stored in a Vector2, where x is the min value and y is the max value. So, the variable we declare has to be of type Vector2, and we pass the absolute minimum and maximum for the slider into the attribute as floats. So here, we’re making a slider between 10 and 15 for the damage range:

…which turns into this in the inspector:

And there you go, a neat way of managing ranges in your projects.

Essential Odin attributes part 1 – [Button()]

Extending the Unity editor makes it a real delight to work in.

Any component can have its own interface and interactions in the editor. Think of any time when you had to spend time dragging references, resetting values, matching string values, forgetting about an unassigned value, or you just needed the scene to change to reflect some new values you’ve set in the component.

These are all common things we need to do but are manual and error-prone. So lets have a look at what we can do.

Lets say we have a serializable class that holds game state…

…and to save, we write it to PlayerPrefs as a JSON string.

During testing, we might need to reset state, or set it to something specific, like unlocking all levels or weapons or something.

We could write a custom inspector to draw a button and call this reset function, but we want to iterate fast and make more of these small “utility” buttons to do other things. So we can’t afford to be making and maintaining inspectors for everything!

Enter Odin, an asset that turns the Unity inspector up to 11 with a collection of useful attributes. In this case, lets use the  [Button()] attribute. Here’s what it looks like:

With just that one line, we can add control over our gamestate in edit time and play time.

Odin inspector

Done.

We can just as easily add cheats, scene manipulation, level loading, testing, and anything else with the same ease. I’ll be posting more examples of what the package can do, but sometimes the simplest things make it well worth the price tag!