Search Unity

  1. Unity Asset Manager is now available in public beta. Try it out now and join the conversation here in the forums.
    Dismiss Notice
  2. Unity 6 Preview is now available. To find out what's new, have a look at our Unity 6 Preview blog post.
    Dismiss Notice
  3. Unity is excited to announce that we will be collaborating with TheXPlace for a summer game jam from June 13 - June 19. Learn more.
    Dismiss Notice

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:
    706
    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,136
    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:
    706
  4. Lhawika

    Lhawika

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

    Flying_Banana

    Joined:
    Jun 19, 2019
    Posts:
    29
    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 :)
     
    INeatFreak, SisusCo and Ghat-Smith like this.
  6. EZaca

    EZaca

    Joined:
    Dec 9, 2017
    Posts:
    32
    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:
    55
    In version 2023 this is already a thing. Finally!
     
    Marks4, SisusCo, EZaca and 2 others like this.
  8. pokelocos

    pokelocos

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

    joan_stark

    Joined:
    Jul 24, 2018
    Posts:
    55
    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
     
  11. bilalincid7

    bilalincid7

    Joined:
    Apr 9, 2021
    Posts:
    5
    This feature should definitely come in the 2021 and 2022 versions. Please.
     
    Marks4 likes this.
  12. spiney199

    spiney199

    Joined:
    Feb 11, 2021
    Posts:
    8,228
    New features never get back ported.