Search Unity

SerializeUtilityHybrid.Serialize & Deserialize.

Discussion in 'Entity Component System' started by RoughSpaghetti3211, Jul 9, 2019.

  1. RoughSpaghetti3211

    RoughSpaghetti3211

    Joined:
    Aug 11, 2015
    Posts:
    1,705
    Can anyone point me to a working example of SerializeUtilityHybrid.Serialize & Deserialize.
    Im not sure what to pass as the BinaryWriter & BinaryReader.

    Thank you
     
  2. tertle

    tertle

    Joined:
    Jan 25, 2011
    Posts:
    3,759
    The package provides a couple of built in ones

    StreamBinaryWriter - writes to file
    MemoryBinaryWriter - writes to memory

    Or you can implement your own

    For example, before they had scene conversion workflow, I implemented a simple TextAsset version.

    Code (CSharp):
    1.     //// <summary>
    2.     /// The TextAssetReader.
    3.     /// </summary>
    4.     public unsafe class TextAssetReader : BinaryReader
    5.     {
    6.         private byte[] bytes;
    7.         private int offset;
    8.  
    9.         /// <summary>
    10.         /// Initializes a new instance of the <see cref="TextAssetReader"/> class.
    11.         /// </summary>
    12.         /// <param name="textAsset">The <see cref="TextAsset"/>.</param>
    13.         public TextAssetReader(TextAsset textAsset)
    14.         {
    15.             this.bytes = textAsset.bytes;
    16.         }
    17.  
    18.         /// <inheritdoc />
    19.         public void Dispose()
    20.         {
    21.             this.bytes = null;
    22.         }
    23.  
    24.         /// <inheritdoc />
    25.         public void ReadBytes(void* data, int count)
    26.         {
    27.             fixed (byte* fixedBuffer = this.bytes)
    28.             {
    29.                 UnsafeUtilityExtensions.MemCpy(data, 0, fixedBuffer, this.offset, UnsafeUtility.SizeOf<byte>(), count);
    30.                 this.offset += count;
    31.             }
    32.         }
    33.     }
     
  3. RoughSpaghetti3211

    RoughSpaghetti3211

    Joined:
    Aug 11, 2015
    Posts:
    1,705
    Thank you, I did a quick test and it very very slow. Im I doing something wrong

    Code (CSharp):
    1.            
    2. string m_Path;
    3.             m_Path = Application.dataPath;
    4.             Debug.Log("Path : " + m_Path);
    5.             StreamBinaryWriter w =  new StreamBinaryWriter(m_Path,  65536);
    6.             GameObject g = new GameObject();
    7.             SerializeUtilityHybrid.Serialize(DstEntityManager, w, out g);
     
  4. tertle

    tertle

    Joined:
    Jan 25, 2011
    Posts:
    3,759
    Depends how much you're serializing.
     
  5. RoughSpaghetti3211

    RoughSpaghetti3211

    Joined:
    Aug 11, 2015
    Posts:
    1,705
    That is a good point , Could you point me to some docs. How do I specify what to serialize ? I trying to sterilized a single gameobject and made some assumptions ( mother of all you know what)

    Code (CSharp):
    1.     public class HexSphereBuildDataConversion : GameObjectConversionSystem
    2.     {
    3.  
    4.         protected override void OnUpdate()
    5.         {
    6.             GameObject go = GameObject.FindGameObjectWithTag("tagHexSphereBuildGameObject");
    7.             HexSphereBuildDataProxy data = go.GetComponent<HexSphereBuildDataProxy>();
    8.             WORLD_SIZE worldSize = (Lightbringer.ECS.HexSphere.WORLD_SIZE)data.worldSize;
    9.             int subdivisionsCount = data.subdivisionsCount;
    10.             int raduis = data.raduis;
    11.             int vertexCount = data.vertexCount;
    12.             int triangleCount = data.triangleCount;
    13.  
    14.             Entity e = GetPrimaryEntity(go);
    15.             EntityQuery query = this.DstEntityManager.CreateEntityQuery(ComponentType.ReadOnly<HexSphereBuildDataComponent>());
    16.  
    17.  
    18.             if (this.DstEntityManager.HasComponent<HexSphereBuildDataComponent>(e))
    19.             {
    20.                 Entities.With(query).ForEach<HexSphereBuildDataComponent>((ref HexSphereBuildDataComponent d) =>
    21.                 {
    22.                     d.worldSize = worldSize;
    23.                     d.subdivisionsCount = subdivisionsCount;
    24.                     d.raduis = raduis;
    25.                     d.vertexCount = vertexCount;
    26.                     d.triangleCount = triangleCount;
    27.                 });
    28.             }
    29.             else
    30.             {
    31.                 var componentData = new HexSphereBuildDataComponent
    32.                 {
    33.                     worldSize = worldSize,
    34.                     subdivisionsCount = subdivisionsCount,
    35.                     raduis = raduis,
    36.                     vertexCount = vertexCount,
    37.                     triangleCount = triangleCount
    38.                 };
    39.  
    40.                 this.DstEntityManager.AddComponentData(e, componentData);
    41.                 this.DstEntityManager.AddComponentData(e, new RequestSceneLoaded());
    42.             }
    43.             // TEST
    44.             string m_Path;
    45.             m_Path = Application.dataPath;
    46.             Debug.Log("Path : " + m_Path);
    47.             StreamBinaryWriter w =  new StreamBinaryWriter(m_Path,  32);
    48.             GameObject g = new GameObject();
    49.             SerializeUtilityHybrid.Serialize(this.DstEntityManager, w, out g);
    50.         }
    51.     }
     
  6. tertle

    tertle

    Joined:
    Jan 25, 2011
    Posts:
    3,759
    There are no documents as far as I'm aware.

    SerializeUtilityHybrid.Serialize will serialize every entity in the world.
    If you only want to serialize specific entities, create a new world, copy the subset of entities to that world and use that manager instead.

    The gameobject you're providing is for getting shared component data that needs to be serialized separate.
    You don't need to declare it beforehand, you're just creating an empty game object, notice how it's an out field so it'll always be declared within the function.

    SerializeUtilityHybrid.Serialize(this.DstEntityManager, w, out var sharedData);
     
    RoughSpaghetti3211 likes this.
  7. RoughSpaghetti3211

    RoughSpaghetti3211

    Joined:
    Aug 11, 2015
    Posts:
    1,705
    Got it thank you.