Unity-SerializeReferenceExtensions icon indicating copy to clipboard operation
Unity-SerializeReferenceExtensions copied to clipboard

[FEATURE] `[AddTypeMenu]` with dynamic string value

Open Antoshidza opened this issue 1 year ago • 1 comments

Feature description

I have IEffect and some wrapper class like CompositeEffect which contains IEffect and IProvider<int> to repeat effect execution certain times. In editor it looks like this: image And when it not expanded I see only Element 1 Composite Effect.

It would be perfect if I could see something like: Element 1 Composite Effect: Draw / Element 1 Draw (composite) / Element 1 Draw.

I think it could be down like Odin does in it's attributes, where we can specify method name to get string dynamically instead of specify const string value.

Antoshidza avatar Aug 21 '24 10:08 Antoshidza

One another way would be to use additional bool useToStringAsName which would use ToString() method of instance (not affect dropdown list when selecting type)

Antoshidza avatar Sep 23 '24 16:09 Antoshidza

@Antoshidza For example, is this good enough?

[SerializeReference, SubclassSelector]
[ArrayDrawer(CustomLabel = "{LabelName}")]
public List<Item> items1 = new();

[SerializeReference, SubclassSelector]
[ArrayDrawer(CustomLabel = "{ToString()}")]
public List<Item> items2 = new();

[System.Serializable]
public class Item
{
    public string LabelName;
    public int Value;

    public override string ToString () {
        return LabelName;
    }
}

mackysoft avatar Oct 26 '24 13:10 mackysoft

I'm talking not about customizing array drawing (which is also good way to enhance this package, but array drawing isn't a part of this package responsibilities), but about drawing selected subclass. In my example here it is "Composite Effect" which I'd rather would draw as "Draw card multiple"

Antoshidza avatar Oct 26 '24 13:10 Antoshidza

Is this what you want to do?

[SerializeReference, SubclasSelector(useAsToStringAsName = true)]
public IEffect effect;

mackysoft avatar Oct 26 '24 14:10 mackysoft

Is this what you want to do?

[SerializeReference, SubclasSelector(useAsToStringAsName = true)]
public IEffect effect;

Yep

Antoshidza avatar Oct 26 '24 14:10 Antoshidza

Functionality implemented. #74 Publish a release soon, just note that we are using UNITY_2021_3_OR_NEWER.

mackysoft avatar Oct 26 '24 14:10 mackysoft