gasgiant / Markup-Attributes

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

Any way to make this work with Odin? #2

Closed 3ternal closed 2 years ago

3ternal commented 2 years ago

Hey, Thanks for making this!

I'm having trouble with this step in the installation instructions: "Alternatively, you can apply MarkedUpEditor to all classes that inherit from MonoBehaviour or ScriptableObject and don't have their own custom editor:"

[CustomEditor(typeof(MonoBehaviour), true), CanEditMultipleObjects]
internal class MarkedUpMonoBehaviourEditor : MarkedUpEditor
{
}
[CustomEditor(typeof(ScriptableObject), true), CanEditMultipleObjects]
internal class MarkedUpScriptableObjectEditor : MarkedUpEditor
{
}

I tried putting this in an editor script, but I still can't use markup attributes in my monobehaviours. For example, even if I tag a field as ReadOnly, it still displays as a normal editable field.

Meanwhile, I tried applying the script to a single class, and it worked. Like this:

[CustomEditor(typeof(LockedDoor)), CanEditMultipleObjects]
internal class LockedDoorEditor : MarkedUpEditor
{
}

In the LockedDoor class, I have a field that looks like this [SerializeField, ReadOnly] bool canBeRelocked = false;

Using the second example, the ReadOnly attribute works as expected.

After a quick test in an empty project, it seems that this problems only occurs when Odin is present in the project (even if Sirenix is not implemented in that particular script). Is there any way this asset can work with Odin in the project without having to create a separate MarkedUpEditor class for every component that I want to use it with? I'm interested in Odin alternatives, but my project isn't prepared to just remove it outright.

gasgiant commented 2 years ago

Hi! Odin inspector inserts its custom inspector for all Unity.Object's by default. I think it does it in the similar way to how I do it for MarkupAttributes. A simple workaround is to create an empty MonoBehaviour child class and an empty custom editor for it that inherits from MarkedUpEditor. Then if you inherit your scripts (for which you want MarkupAttributes) from this new class instead of MonoBehaviour, the attrubutes will work, I think.

gasgiant commented 2 years ago

Something like this (not tested).

class MarkedUpMonoBehaviour : MonoBehaviour
{
}

[CustomEditor(typeof(MarkedUpMonoBehaviour), true), CanEditMultipleObjects]
class MarkedUpMonoBehaviourEditor : MarkedUpEditor
{
}

class YourScript : MarkedUpMonoBehaviour 
{
}
3ternal commented 2 years ago

Yep, you are correct. The wrapper works. Thanks for the quick reply!

If MarkupAttributes inserts its custom inspector into all objects just like Odin, is there any way to selectively toggle this on or off? For example, I've had issues before with Odin breaking serialization for some obscure Unity classes; I guess the same problem could occur with MarkupAttributes as well?

gasgiant commented 2 years ago

MarkupAttributes doesn't change anything about Unity serialization. It's a purely UI package. And it only applies its inspector to a class if you tell it to do so. You can tell it to aplly its inspector to all MonoBehaviours and SerializedObjects, as described in the Usage part of the readme. Odin has something like this in its codebase by default.

3ternal commented 2 years ago

Got it; thanks!