diff --git a/src/ComponentStorage.cs b/src/ComponentStorage.cs index 51d5750..3020bb1 100644 --- a/src/ComponentStorage.cs +++ b/src/ComponentStorage.cs @@ -118,9 +118,18 @@ namespace MoonTools.ECS public override void Serialize(ComponentStorageState serializedComponentStorage) { ReadOnlySpan entityIDBytes = MemoryMarshal.Cast(new ReadOnlySpan(entityIDs, 0, nextID)); + + if (entityIDBytes.Length > serializedComponentStorage.EntityIDs.Length) + { + Array.Resize(ref serializedComponentStorage.EntityIDs, entityIDBytes.Length); + } entityIDBytes.CopyTo(serializedComponentStorage.EntityIDs); ReadOnlySpan componentBytes = MemoryMarshal.Cast(AllComponents()); + if (componentBytes.Length > serializedComponentStorage.Components.Length) + { + Array.Resize(ref serializedComponentStorage.Components, componentBytes.Length); + } componentBytes.CopyTo(serializedComponentStorage.Components); serializedComponentStorage.EntityIdToStorageIndex.Clear();