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

New to manual serialization via byte arrays...

Discussion in 'Scripting' started by Nemox, Feb 16, 2015.

  1. Nemox

    Nemox

    Joined:
    Feb 16, 2010
    Posts:
    396
    So I need to serialize some polymorphic data and it appears that the only way this will really work is by doing it manually. My previous method won't end up working. xP

    I have my MyAsset class which is a ScriptableObject that is to contain a list of my Foo class. Each Foo contains a list of various Bar subclass objects (let's say Crobar and Rebar).

    I know that MyAsset needs a byte[] to store the data. So I figure what I need is two things:
    1. A way to save & load the list of Foo objects. From there, I assume I would have to do the same with the Bars, right? Keep the list stored as byte[] inside of Foo, and then save/load it as necessary?


    2. A way to edit this data. Once I've got it all loaded up in the custom EditorWindow, how do I draw each Bar class if each has its own set of properties?
     
  2. bloomingdedalus

    bloomingdedalus

    Joined:
    Aug 13, 2012
    Posts:
    139
    Just mark "MyAsset" with [Serializable] and as long as everything within it is serializable as well, you can serialize the entire class at will. If you want to serialize objects, store them in Foo and then serialize MyAsset, you can do that as well:

    Code (csharp):
    1.  
    2. [Serializable]
    3. public class MyAsset
    4. {
    5.     public List<byte[]> SerializedFoos = new List<byte[]>();
    6.  
    7.     public void AddFoo(Foo incFoo)
    8.     {
    9.          BinaryFormatter bf = new BinaryFormatter();
    10.          MemoryStream ms = new MemoryStream();
    11.          bf.Serialize(ms, incFoo);
    12.          SerializedFoos.Add(ms.ToArray());
    13.     }
    14.  
    15.     public Foo UnpackAFooForFun(int a)
    16.     {
    17.          MemoryStream ms = new MemoryStream(SerializedFoos[a]);
    18.          BinaryFormatter bf = new BinaryFormatter();
    19.          Foo ReturnFoo = (Foo)bf.Deserialize(ms);
    20.          return ReturnFoo;
    21.     }
    22. }
    23.  
    24. ///method for serializing MyAsset object
    25. public byte[] SerializeAsset()
    26. {
    27.      BinaryFormatter bf = new BinaryFormatter();
    28.      MemoryStream ms = new MemoryStream();
    29.      bf.Serialize(ms, MyAssetInstance);
    30.      return ms.ToArray();
    31. }
    32.  
     
  3. Nemox

    Nemox

    Joined:
    Feb 16, 2010
    Posts:
    396
    The issue I've been having is that with Unity's default serialization via SerializedProperties is that it can't allow polymorphism, so that's why I wanted to rely on byte arrays.

    You bring up a good point. I could merely store the list of Bars as a byte array inside of each Foo, and then just have a normal list of Foos. I believe I understand how to do this part, then.

    Though this does still leave the issue of drawing and each Bar's properties in the EditorWindow. Each type of Bar will have its own variables, so I essentially need to make a custom drawer for each one, or something. For example, Crobar might have a string and a float, while Rebar might have a float and a Rect. Then a future class, Ackbar, might have a string and a GameObject.

    How can I manage to draw each different type of bar?