Search Unity

  1. We are looking for feedback on the experimental Unity Safe Mode which is aiming to help you resolve compilation errors faster during project startup.
    Dismiss Notice
  2. Good news ✨ We have more Unite Now videos available for you to watch on-demand! Come check them out and ask our experts any questions!
    Dismiss Notice

Generics serialization

Discussion in '2020.1 Beta' started by AlkisFortuneFish, Sep 17, 2019.

  1. Lelepoop

    Lelepoop

    Joined:
    Nov 14, 2016
    Posts:
    119
    Nice, refactoring made easier
     
  2. AdamRamberg

    AdamRamberg

    Joined:
    Dec 8, 2016
    Posts:
    20
    First of all, great work! This feature simplifies so many cases and workflows.

    Got a question for you. In my project I defined the following generic ScriptableObjects:
    Code (CSharp):
    1. using System;
    2. using UnityEngine;
    3.  
    4. [Serializable]
    5. public class BaseFoo<T> : ScriptableObject
    6. {
    7.   [SerializeField]
    8.   private T _bar;
    9. }
    10.  
    11. [CreateAssetMenu(menuName = "Foo", fileName = "FooSO")]
    12. public class Foo : BaseFoo<float> { }
    I then created an ScriptableObject asset from Foo. I also created a MonoBehaviour looking like this:
    Code (CSharp):
    1. using UnityEngine;
    2.  
    3. public class Baz : MonoBehaviour
    4. {
    5.     [SerializeField]
    6.     private BaseFoo<float> _foo;
    7. }
    8.  
    The field serializes and I'm able to assign my Foo ScriptableObject to the field. so that is great! However, the default search functionality for the property drawer is not finding Foo ScriptableObject (see attached screenshot).

    I guess this is because Foo is derived from BaseFoo<T> and instead of being a BaseFoo<T>. Is this something that is going to be fixed or is this the intended behaviour?
     

    Attached Files:

  3. quabug

    quabug

    Joined:
    Jul 18, 2015
    Posts:
    50
    Is it possible to support `SerializeReference` on generic type in the foreseeable future?
    It just show an error message if I try to add `SerializeReference` on a field of generic type.
    "This is not supported. You must create a non-generic subclass of your generic instance type and use that as the field type instead."​
     
    Ghat-Smith likes this.
  4. joshcamas

    joshcamas

    Joined:
    Jun 16, 2017
    Posts:
    1,029
    serializing of generics is coming in 2020.1. I don't know if this includes serialize reference, however
     
  5. quabug

    quabug

    Joined:
    Jul 18, 2015
    Posts:
    50
    Neither of 2020.1b nor 2020.2a have introduce this feature already.
    In the meanwhile, Odin(Serializer) provide alternative way to serialize generic field with `SerializeReference` attribute.
     
    Last edited: May 22, 2020
  6. stonstad

    stonstad

    Joined:
    Jan 19, 2018
    Posts:
    161
    The Newtonsoft.Json package available in the Unity asset store puts the Unity serializer to shame. It can work off the main render thread and it supports a range of generic serialization options. I prefer it because it works server-side so that I get the same serialized JSON on game clients and server.
     
  7. AlkisFortuneFish

    AlkisFortuneFish

    Joined:
    Apr 26, 2013
    Posts:
    794
    Yeah, been using that for many years now but you do have to remember that the scope and purpose of it is very different to the Unity serialiser and their use can be combined. We've had polymorphic serialisation with proper UnityEngine.Object references and inspectors that look like the regular properly drawers for years now, but performance is not amazing.

    Saying that, I wonder how the Json.Net package manager package behaves with AOT etc.
     
    stonstad likes this.
  8. Dextozz

    Dextozz

    Joined:
    Apr 8, 2018
    Posts:
    338
    In what Unity version was this introduced? I can't seem to find it anywhere in the 2019 Release Notes.
     
  9. karl_jones

    karl_jones

    Unity Technologies

    Joined:
    May 5, 2015
    Posts:
    4,021
    This is a 2020.1 feature, which is why this thread is in the 2020.1 Beta forum section ;)
     
    Dextozz likes this.
  10. Thimo_

    Thimo_

    Joined:
    Aug 26, 2019
    Posts:
    12
    Will the abstract be removed from an UnityEvent at the final release of unity 2020.1?
     
    OndrejP likes this.
unityunity