Search Unity

Feedback A Plea: Support for template serialization

Discussion in 'General Discussion' started by joshcamas, May 19, 2019.

  1. joshcamas

    joshcamas

    Joined:
    Jun 16, 2017
    Posts:
    1,277
    Hello!

    One thing that has always troubled me is the fact that templates are not serialized, which means one must create a class wrapper.

    I know that Unity believes that having a super duper simple serialization (from a number of features pov) is on purpose to make people not lazy and to make them think about how to serialize info. Makes sense, I like that! But... I feel like this restriction does the opposite! It makes a major part of C# lose much of its potential.

    This issue has come up again for me when looking at Addressables! Want to restrict what a AssetReference does? Can't do that without creating a class for each individual type you'll want to use it with. :( The same goes for UnityEvents. And those are just examples of how Unity features themselves require this hacky workaround.

    Obviously it's not the end of the world, since this workaround does indeed work fine. It's just frustrating, especially if I'm using multiple templated types (such as now that I want to use addressables)

    Is there any chance that someday in the future Unity will rethink this?

    Note: I'd guess I have around 30-40 classes in my game devoted to just being template wrappers. :^)

    Thank you
     
  2. elmar1028

    elmar1028

    Joined:
    Nov 21, 2013
    Posts:
    2,359
    Have you tried Odin Serializer?

    It's a free and open source framework for serializing a bigger variety of data types, including generics, interfaces, dictionaries and loads more.
     
    joshcamas likes this.
  3. joshcamas

    joshcamas

    Joined:
    Jun 16, 2017
    Posts:
    1,277
    Yep! It's by far my favorite addon!!!!

    However since Odin is of course its own serializer, I am wary when utilizing it, especially when writing code I want to open source, since it adds a whole lot of fat on top of something that would usually be quite simple. While using Odin for this issue is indeed a solution, I still believe it would be incredibly useful if Unity implemented template serialization...
     
  4. superpig

    superpig

    Drink more water! Unity Technologies

    Joined:
    Jan 16, 2011
    Posts:
    4,657
    What gave you that idea? o_O

    Keeping serialisation high-performance is a top priority - it is not only used for saving/loading data, but also for e.g. asset GC, the Inspector, instantiation, etc. But beyond that it is really just a matter of development priorities. 2019.2 adds the ability to serialise a field 'by reference' (which means you can do null support, polymorphism, etc), but generics simply haven't been high enough on the priority list to get done. As you say, there's a workaround which is not the end of the world.
     
    Flavelius and joshcamas like this.
  5. Kiwasi

    Kiwasi

    Joined:
    Dec 5, 2013
    Posts:
    16,860
    Colour me excited! That's something I've been hoping for for a long time.
     
    Flavelius and angrypenguin like this.
  6. joshcamas

    joshcamas

    Joined:
    Jun 16, 2017
    Posts:
    1,277
    YES I'm so glad about polymorphism!!!! That's so much more exciting than generics :D I assume that generics will be done some day though?
     
  7. angrypenguin

    angrypenguin

    Joined:
    Dec 29, 2011
    Posts:
    15,620
    I also hope so. That and support for Interfaces. Or will serialisation "by reference" also support this?
     
  8. Kiwasi

    Kiwasi

    Joined:
    Dec 5, 2013
    Posts:
    16,860
    I don't pretend to understand the tech yet. But it wouldn't surprise me if serilisation "by reference" implicitly solves both generics and interfaces.