TylerTemp / SaintsField

A Unity Inspector extension tool focusing on script fields inspector enhancement
MIT License
154 stars 9 forks source link

Possible Feature Request: Allow for easier custom Attributes #4

Closed MPierer closed 6 months ago

MPierer commented 6 months ago

I would like to add attributes like HideInPlayMode or ReadOnlyInPlayMode or any other condition. I am aware that I can use the existing attributes and use callbacks. But these callbacks have to live inside the MonoBehaviour or ScriptableObject, meaning I would have to add this callback in every MB or SO I would like the attribute to be used. Subclassing the ReadOnlyAttributeDrawer is also not a suitable thing, because there are different code-paths for IMGUI and UIToolkit (GetAndVisibility and GetShow). So maybe some solutions would be:

  1. Allow callbacks to searched not only on the target MB or SO, but also on the PropertyAttribute itsel. Then we could add a new attribute and set the callback to be local.
  2. Add virtual methods to the attribute (e.g. GetVisiblity / GetEnabled) which could then be implemented in the derived property and is evaluated in addition to the normal callbacks.
TylerTemp commented 6 months ago

When writing this project I did think about this feature but decided to not do it cuz I didn't feel necessary.

So, seems like this feature is more important than I thought...

OK, to roundup:

Odin:

NaughtyAttributes:

EButtonEnableMode, only for buttons.

So sounds like a necessary feature

MPierer commented 6 months ago

Thanks for consideration! Well, yes I had some of these attributes in mind, but my point was more like, allow SubClassing of HideIfAttribute or ReadOnlyAttribute. Let's have the ReadOnlyAttribute for example.

We could then simply subclass the ReadOnlyAttribute with

public class DisableInPlayModeAttribute: ReadOnlyAttribute
{
      protected override bool isDisabled() { return Application.isPlaying; }
}

// also implement a dummy custom property drawer, as the Saints drawers are not used for Subclasses currently
[CustomPropertyDrawer(typeof(DisableInPlayModeAttribute): ReadyOnlyAttributeDrawer
{
   // implement c'tor...
}
TylerTemp commented 6 months ago

Sorry I wrote in a hurry and didn't explain very well.

Yeah if the attribute drawer can have a better inherent-able API it'll be very convent. During the developing I did changes some part to make it easier.

However, I'm not very good at API design that focusing on extending. So at this point:

  1. If there is an obviously good API, I'll put it there
  2. If people who want to extend some specific attribute, I'll consider it and try to make it extendable.

This is my answer to this issue about "easier custom(or extend) attribute)


As an example of this, the ShowIf, HideIf, DisableIf, EnableIf have exposed the API for convent extending. See:

As a reference. Well, it's not documented and the API might change in the feature.


Feel free to comment or open new issues if you have other ideas, or improved ideas for this issue.