mackysoft / Unity-SerializeReferenceExtensions

Provide popup to specify the type of the field serialized by the [SerializeReference] attribute in the inspector.
https://qiita.com/makihiro_dev/items/26daeb3e5f176934bf0a
MIT License
770 stars 59 forks source link

feat: add SubclassSelector generic class to allow "nested" PropertyDrawers #26

Closed mnicolas94 closed 10 months ago

mnicolas94 commented 1 year ago

Description

The problem:

I have and interface and I want to select subclasses with SubclassSelector attribute. One of the subclasses has its own PrepertyDrawer. Let's look to an example code:

public interface IInterace {}

[Serializable]
public class SubclassA : IInterface {}

[Serializable]
public class SubclassB : IInterface {}

public class MyObject : MonoBehaviour {
    [SerializeReference, SubclassSelector] private IInterface _interface;
}

// in an Editor script
[CustomPropertyDrawer(typeof(SubclassB))]
public class SubclassBPropertyDrawer : PropertyDrawer
{
    // ... editor code
}

The problem is the property drawer of SubclassB wont be used as it competes with SubclassSelectorDrawer because both drawers are applied to the same field _interface.

MySolution

Instead of an attribute I used a generic class SubclassSelector<T> that have a SerializeReference field. I added this class to your code so users can use either SubclassSelectorAttribute or SubclassSelector<T> depending on their needs. The usage is as follows:

public class MyObject : MonoBehaviour {
    [SerializeField] private SubclassSelector<IInterface> _interface;
}

Advantages:

Disadvantages

Changes made

mackysoft commented 10 months ago

Sorry for the very late reply. I had reservations as to whether a new feature should be implemented.

If I am correct, the issue may have been resolved in the following release https://github.com/mackysoft/Unity-SerializeReferenceExtensions/releases/tag/1.1.9

This release allows SubclassSelector to retrieve and handle custom drawers for subclasses.

I hope the issue has been resolved.