Skip to content

[FEATURE] Add abstract generic support #83

Description

@Nitero

Feature description

First of all thanks for the great package, it works like a charm!

So I'm not sure if this is possible, but it would be nice if we could use SubclassSelector on abstract classes with a generic type. This way we could add an interface to the abstract class that does not have a generic type. Then we could use this to specify config values in a dictionary without a type.

My workaround for this is to use a generic interface and have a second plain interface just for selecting targets easily in configs.

public class Example : MonoBehaviour
{
    [SerializeReference, SubclassSelector]
    private Target<Character> _target;// does NOT show ConcreteTarget

    [SerializeReference, SubclassSelector]
    private ITarget<Character> _target2;// DOES show ConcreteTarget

    // [SerializeReference, SubclassSelector]
    // private SerializedDictionary<ITarget, float> _configs;// not possible as the interface needs a generic type
    
    [SerializeReference, SubclassSelector]
    private SerializedDictionary<ITargetConfigurable, float> _configs2;// possible workaround
}


// ideally we could add the generic type in the abstract class instead
public interface ITarget<T>
{
    T GetTargets();
}

// using an extra interface instead of ITarget allows us to select a target without type eg. in a config
public interface ITargetConfigurable { }

public abstract class Target<T> : ITarget<T>, ITargetConfigurable
{
    public abstract T GetTargets();
}

[Serializable]
public class ConcreteTarget : Target<Character>
{
    public override Character GetTargets() => null;
}

public class Character { }

Activity

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

Labels

enhancementNew feature or request

Projects

No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions