Search Unity

  1. 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:
    126
    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:
    58
    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,061
    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:
    58
    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:
    208
    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:
    839
    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:
    398
    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,191
    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:
    27
    Will the abstract be removed from an UnityEvent at the final release of unity 2020.1?

    [EDIT] Yes they did, awesome!
     
    Last edited: Jul 25, 2020
    OndrejP likes this.
  11. ShuheiAoki

    ShuheiAoki

    Joined:
    Oct 15, 2018
    Posts:
    2
    I am also having this exact problem. Its holding me back from using generics because of this
     
  12. michal-zetkowski

    michal-zetkowski

    Joined:
    Mar 11, 2015
    Posts:
    76
    Hey, I've read through this thread and I'm wondering if I understand the implications of the following point correctly:
    As far as I understand, it basically means that I can do this:
    Code (CSharp):
    1. public class GameEventListener : MonoBehaviour
    2. {
    3.     public UnityEvent<GameEventArgs> Response;
    4. }
    Instead of this:
    Code (CSharp):
    1. public class TypedUnityEvent : UnityEvent<GameEventArgs> { }
    2.  
    3. public class GameEventListener : MonoBehaviour
    4. {
    5.     public TypedUnityEvent Response;
    6. }
    It that it? I am aware that it saves a lot of boilerplate code, but I feel like there must be more to it. You gave some examples in this thread, but they were quite difficult for me to understand and I would be thankful if someone could point me towards some simple use cases.

    Secondly, I'm working on a event system and I would like to extend the code to accept different types of event args, something like this:
    Code (CSharp):
    1. public class GameEventListener<T> : MonoBehaviour where T : GameEventArgs
    2. {
    3.     public UnityEvent<T> Response;
    4. }
    I am aware that this is beyond the point that I quoted, but could you maybe point me towards some solution different than subclassing GameEventListener for each type of GameEventArgs? Thanks :)
     
  13. superpig

    superpig

    Drink more water! Unity Technologies

    Joined:
    Jan 16, 2011
    Posts:
    4,353
    Nope, that's pretty much it.
     
  14. Ghat-Smith

    Ghat-Smith

    Joined:
    Aug 16, 2016
    Posts:
    27
    Same question. I would love to directly serialize generic type by reference.
     
  15. cecarlsen

    cecarlsen

    Joined:
    Jun 30, 2006
    Posts:
    602
    I get the warning "Generic MonoBehaviours are not supported!" when I call ScriptableObject.CreateInstance on a generic class extending ScriptableObject in Unity 2020.1.3f1. Shouldn't this be supported with the new generic serialization feature?
     
  16. AlkisFortuneFish

    AlkisFortuneFish

    Joined:
    Apr 26, 2013
    Posts:
    839
    No. You still need to derive a concrete UnityEngine.Object derived instance for the engine to actually support it. This feature is about normal classes.
     
  17. cecarlsen

    cecarlsen

    Joined:
    Jun 30, 2006
    Posts:
    602
    Thanks, good to know. Does anyone know if the feature (serialization of generic classes derived from UnityEngine.Object) is planned? This would greatly influence the design of the system I am building.
     
unityunity