Search Unity

  1. Megacity Metro Demo now available. Download now.
    Dismiss Notice
  2. Unity support for visionOS is now available. Learn more in our blog post.
    Dismiss Notice

Question Unity Serialization

Discussion in 'Scripting' started by puttin, Jul 1, 2020.

  1. puttin

    puttin

    Joined:
    Oct 16, 2019
    Posts:
    6
    Hi guys, I am having a issue with Serialization.

    I made my code everthing all right i have a lot of array with interfaces, now i am having the problem to serialize it.

    Code (CSharp):
    1. interface Animal{
    2. void Sound();
    3. }
    4.  
    5. class Dog:animal{
    6. public string name;
    7. ...
    8. }
    9. class Bird:animal{
    10. public bool CanFly;
    11. ...
    12. }
    13.  
    14. Animal[] animals..
    Can you give me some tips how i can use Editor to edit the properties and how can i Serialize my array of animals?

    I can make editor stuff and i now how to use JsonUtility.ToJson(script);

    I am very used to javascript so is kinda hard thinking in a typed language.
     
  2. Antistone

    Antistone

    Joined:
    Feb 22, 2014
    Posts:
    2,836
    Unity only serializes variables based on their static type, not dynamic type. So if you create a Dog and stick it in an Animal variable, it will only serialize the Animal fields, and when you deserialize it it won't be a Dog anymore.

    Actually, since Animal is an interface rather than a class, I bet it won't deserialize at all.

    If you want to serialize subclasses in the Unity editor, you need to make them inherit from something that the inspector shows as a reference rather than embedded data. Such as ScriptableObject or MonoBehaviour.

    If you don't care about the Unity editor and just want to be able to serialize and deserialize in script, then you could instead look for a third-party JSON serializer that handles subtypes.
     
    puttin likes this.
  3. StarManta

    StarManta

    Joined:
    Oct 23, 2006
    Posts:
    8,773
    JSON is great and commonly used in Unity games. JsonUtility is very limited, especially with regard to inheritance. I haven't tried serializing a list of interfaces but I imagine it'd have the same limitations.

    Newtonsoft's JSON.NET is much much more capable, and I highly recommend it. When you serialize/deserialize you can use the settings flag TypeNameHandling to make it put the type name into the JSON file. this should allow the interface list to be serialized properly. (Disclaimer: I've done that with an abstract base class, but not with an interface. Don't see any reason it wouldn't work, though.)
     
    puttin likes this.
  4. WarmedxMints

    WarmedxMints

    Joined:
    Feb 6, 2017
    Posts:
    1,035
    I would use a class myself and mark it as serializable. You can then inherit from that class and you should be able to see it all in the editor. You can also create a custom editor for it if you wish.
    Code (CSharp):
    1. using System;
    2. using UnityEngine;
    3.  
    4. [Serializable]
    5. public class Animal
    6. {
    7.     public string Name;
    8.     public bool CanFly;
    9.     public virtual void Sound() {}
    10. }
    11.  
    12. public class Dog : Animal
    13. {
    14.     public override void Sound()
    15.     {
    16.         //...
    17.     }
    18. }
    19.  
    20. public class Bird : Animal
    21. {
    22.     public override void Sound()
    23.     {
    24.         //....
    25.     }
    26. }
     
    puttin likes this.
  5. puttin

    puttin

    Joined:
    Oct 16, 2019
    Posts:
    6
    I give a simple example but i need something with more fields and complex data.

    I tried JSON.NET it works fine with abstract classes, but i cant serialize a GameObject or a Transform
     
  6. StarManta

    StarManta

    Joined:
    Oct 23, 2006
    Posts:
    8,773
    Yeah, you can't and you won't be able to. You'll have to copy whatever data you need to save into other classes.