Search Unity

  1. Welcome to the Unity Forums! Please take the time to read our Code of Conduct to familiarize yourself with the forum rules and how to post constructively.

Feedback [SerializeReference] Generic (Inflated) Types

Discussion in '2020.2 Beta' started by Catsoft-Studios, Jun 20, 2020.

  1. Catsoft-Studios

    Catsoft-Studios

    Joined:
    Jan 15, 2011
    Posts:
    699
    We're working on a new set of tools and stumbled upon this limitation of the [SerializeReference] attribute. In the documentation (https://docs.unity3d.com/ScriptReference/SerializeReference.html) it says any managed type can be saved as a reference unless it's an inflated type. This means that this will work:

    Code (CSharp):
    1.  
    2. interface IAnimal
    3. { }
    4.  
    5. class Cat : IAnimal
    6. {
    7.    int health = 9;
    8. }
    9.  
    10. [SerializeReference]
    11. public IAnimal[] animals = { new Cat() };
    However, wrapping the array declaration in a generic class won't work:

    Code (CSharp):
    1. // ...
    2.  
    3. class Zoo<T> where T : IAnimal
    4. {
    5.     [SerializeReference]
    6.     public T[] animals = { new Cat() }; // incorrectly serialized
    7. }
    8.  
    9. public Zoo<IAnimal> zoo = new Zoo();
    Is this going to be supported? In the foreseeable future at least. Or is there a technical limitation? Just asking in case we can wait or work around this. Thanks!
     
  2. LeonhardP

    LeonhardP

    Unity Technologies

    Joined:
    Jul 4, 2016
    Posts:
    3,065
    Reply from the devs: "Supporting inflated types for serialize reference is currently not planned."
     
    Catsoft-Studios likes this.
  3. Catsoft-Studios

    Catsoft-Studios

    Joined:
    Jan 15, 2011
    Posts:
    699
  4. Lhawika

    Lhawika

    Joined:
    May 27, 2015
    Posts:
    52
    Well this is actually a really sad news... I was really looking forward this feature :(
     
    EZaca and Catsoft-Studios like this.
  5. Flying_Banana

    Flying_Banana

    Joined:
    Jun 19, 2019
    Posts:
    25
    We are so close to be able to use interfaces in Unity! Also - I found out that this is wrong in the docs:

    - Field value cannot be a specific specialization of a generic type(inflated type).​

    Not only the value, but the actual type of the field must be a non-generic type for serialization to succeed.

    This is likely a superficial restriction as it can be worked around by storing a System.object with [SerializedReference], then cast to your interface value. Nevertheless, it is somewhat ugly and it would be great if we can have this implemented.

    For the value itself, we can create a factory class that handles the subclassing. Some more boilerplate code, but tolerable.

    Again, we are so close to having interfaces work as they should in Unity! Looking forward to the day this lands eventually :)
     
    SisusCo and Ghat-Smith like this.
  6. EZaca

    EZaca

    Joined:
    Dec 9, 2017
    Posts:
    27
    I don't think this is correct, unless I did not understand what you said there, because I tried to create an object and cast it to a non-generic, but it failed as well. That makes sense, because objects are given by reference, then the underlying generic object remains untouched by the type cast. Other workarounds (to put a non-generic class inside a generic class and extend the generic) also does not work.

    What I don't understand is: why there is such limitation? Isn't "ExampleClass<int>" as concrete as "ExampleClassInt"? I even tried to get a generic class through Type.GetType and instantiate it with Activator.CreateInstance, and it worked just fine. Does Unity use something else to instantiate the object?
     
    Last edited: Nov 14, 2022
  7. joan_stark

    joan_stark

    Joined:
    Jul 24, 2018
    Posts:
    33
    In version 2023 this is already a thing. Finally!
     
    EZaca, Catsoft-Studios and Ghat-Smith like this.
  8. pokelocos

    pokelocos

    Joined:
    Nov 9, 2015
    Posts:
    36
    Do you know if this feature will be brought back to Unity 2022?
     
    Ghat-Smith likes this.
  9. joan_stark

    joan_stark

    Joined:
    Jul 24, 2018
    Posts:
    33
    Doesn't look like...
     
  10. Lars-Blaabjerg

    Lars-Blaabjerg

    Joined:
    Apr 5, 2011
    Posts:
    54
    I would really like that feature in 2022!

    As some said earlier. It seems like it seems like the restriction on the field type (as opposed to the value type) is an artificial limitation, as the type of the field itself would have nothing to do with the deserialization of the object, if that object is of a non generic derived type.

    Proper generic support would also be nice. But I am not asking that much