Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

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

Open
3ternal opened this issue Feb 25, 2022 · 3 comments
Open

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

3ternal opened this issue Feb 25, 2022 · 3 comments

Comments

@3ternal
Copy link

3ternal commented Feb 25, 2022

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
Copy link
Owner

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
Copy link
Author

3ternal commented Mar 1, 2022

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
Copy link
Owner

gasgiant commented Mar 1, 2022

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.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants