gasgiant / Markup-Attributes

A Unity Editor extension for customizing inspector layout with attributes.
MIT License
286 stars 11 forks source link

[Request/Question] How to make a "Required" attribute #4

Open 3ternal opened 2 years ago

3ternal commented 2 years ago

I've been using Markup Attributes for a while now, and it's been great. I still have Odin in my project, but the only time I ever use it is for the Required attribute: https://odininspector.com/attributes/required-attribute

Are there any plans to add something like this to Markup Attributes? If not, is there any way for us to create our own attributes?

gasgiant commented 2 years ago

Hi! There is no easy way to create your own attributes right now. I thought that you could do something like this with a decorator drawer https://docs.unity3d.com/ScriptReference/DecoratorDrawer.html , but you'll need access to the SerializedProperty and I don't think you can get it there.

The easy way to achieve this would be to write a custom property drawer https://docs.unity3d.com/ScriptReference/PropertyDrawer.html that draws the property as is, and if it's null adds a warning below. This won't work for the properties that have some another custom property drawer, because each property can have only one drawer. But otherwise it is an ok solution, if Required is all you need.

I'll think about adding validation functionality like this to MarkupAttributes, though. On the one hand it seems like a useful and appropriate feature, but on the other, I want to keep the project small.

3ternal commented 2 years ago

Thanks for the explanation! This is working for me now, albeit crudely. My custom drawer overrides OnGUI, and it logs an error to the console if the SerializedProperty's objectReferenceValue is null.

It would be great if this were built-in, but I can understand why you'd want to keep the project small. Either way, thanks for the help.

gasgiant commented 2 years ago

You can also use https://docs.unity3d.com/ScriptReference/EditorGUI.HelpBox.html to display a warning in the editor, like in Odin. Depending on message type it will show a warning or an error icon next to your message.