Search Unity

was anyone able to serialize and deserialize a World Correctly ?

Discussion in 'Entity Component System' started by Opeth001, Jun 20, 2019.

  1. Opeth001

    Opeth001

    Joined:
    Jan 28, 2017
    Posts:
    1,117
    im not able to serialize and diserialize a World and i didnt found any working example on the internet.

    The serialization id working correctly and generating the File but the deserialization is just not working and no errors.



    Code (CSharp):
    1.  
    2.   const string serializedMap = "Assets/Tests/SerializedMap.data";
    3.        
    4.   public void OnClickSerialize()
    5.         {
    6.             using (var writer = new StreamBinaryWriter(serializedMap))
    7.             {
    8.                 int[] sharedComponentIndices;
    9.                 SerializeUtility.SerializeWorld(MainManager, writer, out sharedComponentIndices);
    10.                 SerializeUtility.SerializeSharedComponents(MainManager, writer, sharedComponentIndices);
    11.             }
    12.         }
    13.  
    14.  
    15.         public void OnclickDeserialize()
    16.         {
    17.             var transaction = CachingEntityManager.BeginExclusiveEntityTransaction();
    18.             using (var reader = new StreamBinaryReader(serializedMap))
    19.             {
    20.                 var SCCount = SerializeUtility.DeserializeSharedComponents(CachingEntityManager, reader);
    21.                 SerializeUtility.DeserializeWorld(transaction, reader, SCCount);
    22.             }
    23.             CachingEntityManager.EndExclusiveEntityTransaction();
    24.             MainManager.MoveEntitiesFrom(CachingEntityManager);
    25.             if (CachingWorld.IsCreated)
    26.                 CachingWorld.Dispose();
    27.         }
    28.         }
     
  2. daschatten

    daschatten

    Joined:
    Jul 16, 2015
    Posts:
    208
    Trying the same... DeserializeSharedComponents() takes forever if SharedComponents and Entities are in the same file. I split them but now there seems to be an issue with index calculation in SerializeUtility around line 476:

    Code (CSharp):
    1.  
    2. int index = entityManager.ManagedComponentStore.InsertSharedComponentAssumeNonDefault(typeIndex, hashCode, data);
    3. Assert.AreEqual(i + 1, index);
    4.  
    index stays 1 for every iteration and assert triggers.

    This is my code for saving and loading a world:

    Code (CSharp):
    1. public void AddEntitiesForTile(MapData mapData, TileData tileData, World world)
    2. {
    3.     var path = Path.Combine(GetMapPath(mapData.id), GetTileEntitiesFilename(tileData));
    4.     int[] sharedComponents;
    5.  
    6.     using (var writer = new StreamBinaryWriter(path))
    7.     {
    8.         SerializeUtility.SerializeWorld(world.EntityManager, writer, out sharedComponents);
    9.     }
    10.  
    11.     path = Path.Combine(GetMapPath(mapData.id), GetTileSharedComponentsFilename(tileData));
    12.  
    13.     using (var writer = new StreamBinaryWriter(path))
    14.     {
    15.         SerializeUtility.SerializeSharedComponents(world.EntityManager, writer, in sharedComponents);
    16.     }
    17. }
    18.  
    19. public void GetEntitiesForTile(MapData mapData, TileData tileData, World world)
    20. {
    21.     var path = Path.Combine(GetMapPath(mapData.id), GetTileSharedComponentsFilename(tileData));
    22.     int sharedComponents;
    23.          
    24.     using (var reader = new StreamBinaryReader(path))
    25.     {
    26.         sharedComponents = SerializeUtility.DeserializeSharedComponents(world.EntityManager, reader);
    27.     }
    28.  
    29.     path = Path.Combine(GetMapPath(mapData.id), GetTileEntitiesFilename(tileData));
    30.     var transaction = world.EntityManager.BeginExclusiveEntityTransaction();
    31.  
    32.     using (var reader = new StreamBinaryReader(path))
    33.     {
    34.         SerializeUtility.DeserializeWorld(transaction, reader, sharedComponents);
    35.     }
    36.  
    37.     world.EntityManager.EndExclusiveEntityTransaction();
    38. }
    Any hint on this?
     
    Kuptsevych-Yuriy and Opeth001 like this.
  3. Opeth001

    Opeth001

    Joined:
    Jan 28, 2017
    Posts:
    1,117
    i ended up using subscenes it seems to be the most efficient solution, but the only problem now is dont know how to change subscenes Generated Data directory cause their generated files are too big ( 100mo+ ) in my case, they can be easly compressed to under 10mo but i cant access them via android devices (they are under StreamingAssets forlder)
     
  4. daschatten

    daschatten

    Joined:
    Jul 16, 2015
    Posts:
    208
    In a later stage i want to add runtime procgen so i tried to avoid subscenes. I hope next update brings some improvements for serialization.
     
    Opeth001 likes this.
  5. daschatten

    daschatten

    Joined:
    Jul 16, 2015
    Posts:
    208
    Got an error now, again the index calculation seems to be the issue. Maybe @Joachim_Ante has an idea why this happens with the code in my post above? It seems i missed something...

    Code (CSharp):
    1.  
    2. IndexOutOfRangeException: Index 2 is out of range in NativeList of '2' Length.
    3. Unity.Collections.NativeList`1[T].get_Item (System.Int32 index) (at Library/PackageCache/com.unity.collections@0.1.1-preview/Unity.Collections/NativeList.cs:102)
    4. Unity.Entities.ManagedComponentStore.AddReference (System.Int32 index, System.Int32 numRefs) (at Library/PackageCache/com.unity.entities@0.1.1-preview/Unity.Entities/ManagedComponentStore.cs:267)
    5. Unity.Entities.ManagedComponentStore.Playback (Unity.Entities.ManagedDeferredCommands& managedDeferredCommands) (at Library/PackageCache/com.unity.entities@0.1.1-preview/Unity.Entities/ManagedComponentStore.cs:728)
    6. Unity.Entities.Serialization.SerializeUtility.DeserializeWorld (Unity.Entities.ExclusiveEntityTransaction manager, Unity.Entities.Serialization.BinaryReader reader, System.Int32 numSharedComponents) (at Library/PackageCache/com.unity.entities@0.1.1-preview/Unity.Entities/SerializeUtility.cs:143)
    7.  
     
    Kuptsevych-Yuriy likes this.