Creating a Simple Property Drawer

Properties drawers can be used to customize the UI of specific elements, or the entire Inspector from a serializable class.
In this tutorial, we will use a custom Attribute in order to modify the way that a string value is rendered on the inspector.

Start by creating a class called StringToEnumAttribute that derivates from PropertyAttribute

When working with custom attributes, we must specify where it can be used. Add the [AttributeUsage] method for this:

This makes it only available on fields.

Create another class inside an Editor folder, this one called StringToEnumDrawer that derivates from PropertyDrawer.
This class will be responsible for rendering the custom inspector

At this point, if you create a variable using this attribute, you will see it is no longer rendered.
This happens because you are overriding the default GUI.

So we need to make sure that the Attribute knows what we want to render.
We can used it to store as many values as we want, int this case we will store a array of strings.

Now, we can use the OnGUI function from the drawer class, to define how its values must be rendered.

To finish, just update your attribute to see it being rendered as a enum field:

If you like it, please consider supporting me!
This way I can make this kind of content more often 💙 

By becoming a patron, you'll instantly unlock access to 1 exclusive post
2
Images
By becoming a patron, you'll instantly unlock access to 1 exclusive post
2
Images