Esempio n. 1
0
        public void HybridComponentSerialize()
        {
            var root   = CreateGameObject();
            var values = new[] { 123, 234, 345 };

            foreach (var value in values)
            {
                var gameObject = CreateGameObject().ParentTo(root);
                gameObject.AddComponent <EcsTestMonoBehaviourComponent>().SomeValue = value;
            }

            GameObjectConversionUtility.ConvertGameObjectHierarchy(root, MakeDefaultSettings().WithExtraSystem <ConversionHybridTests.MonoBehaviourComponentConversionSystem>());

            ReferencedUnityObjects objRefs = null;
            var writer = new TestBinaryWriter();

            SerializeUtilityHybrid.Serialize(m_Manager, writer, out objRefs);

            var world = new World("temp");

            var reader = new TestBinaryReader(writer);

            SerializeUtilityHybrid.Deserialize(world.EntityManager, reader, objRefs);

            var query      = world.EntityManager.CreateEntityQuery(typeof(EcsTestMonoBehaviourComponent));
            var components = query.ToComponentArray <EcsTestMonoBehaviourComponent>();

            CollectionAssert.AreEquivalent(components.Select(c => c.SomeValue), values);

            query.Dispose();
            world.Dispose();
            reader.Dispose();
        }
        public void SerializingSharedComponent_WhenMoreThanOne_AndProxyHasDisallowMultiple_DoesNotCrash()
        {
            for (var i = 0; i < 20; ++i)
            {
                var entity = m_Manager.CreateEntity();
                m_Manager.AddSharedComponentData(entity, new MockSharedDisallowMultiple {
                    Value = i
                });
                m_Manager.AddComponentData(entity, new EcsTestData(i));
            }

            var        writer           = new TestBinaryWriter();
            GameObject sharedComponents = null;

            try
            {
                Assert.That(
                    () => SerializeUtilityHybrid.Serialize(m_Manager, writer, out sharedComponents),
                    Throws.ArgumentException.With.Message.Matches(@"\bDisallowMultipleComponent\b")
                    );
            }
            finally
            {
                writer.Dispose();
                if (sharedComponents != null)
                {
                    GameObject.DestroyImmediate(sharedComponents);
                }
            }
        }
Esempio n. 3
0
        public unsafe void SerializeEntitiesWorksWithChunkComponents()
        {
            var dummyEntity = CreateEntityWithDefaultData(0); //To ensure entity indices are offset

            var e1 = m_Manager.CreateEntity();

            m_Manager.AddComponentData(e1, new EcsTestData(1));
            m_Manager.AddChunkComponentData <EcsTestData3>(e1);
            m_Manager.SetChunkComponentData(m_Manager.GetChunk(e1), new EcsTestData3(42));
            var e2 = m_Manager.CreateEntity();

            m_Manager.AddComponentData(e2, new EcsTestData2(2));
            m_Manager.AddChunkComponentData <EcsTestData3>(e2);
            m_Manager.SetChunkComponentData(m_Manager.GetChunk(e2), new EcsTestData3(57));

            m_Manager.DestroyEntity(dummyEntity);
            // disposed via reader
            var writer = new TestBinaryWriter();

            int[] sharedData;
            SerializeUtility.SerializeWorld(m_Manager, writer, out sharedData);
            var reader = new TestBinaryReader(writer);

            var deserializedWorld = new World("SerializeEntities Test World 3");
            var entityManager     = deserializedWorld.EntityManager;

            SerializeUtility.DeserializeWorld(entityManager.BeginExclusiveEntityTransaction(), reader, 0);
            entityManager.EndExclusiveEntityTransaction();

            try
            {
                var group1 = entityManager.CreateEntityQuery(typeof(EcsTestData));
                var group2 = entityManager.CreateEntityQuery(typeof(EcsTestData2));

                Assert.AreEqual(1, group1.CalculateEntityCount());
                Assert.AreEqual(1, group2.CalculateEntityCount());

                var entities1 = group1.ToEntityArray(Allocator.TempJob);
                var entities2 = group2.ToEntityArray(Allocator.TempJob);

                var new_e1 = entities1[0];
                var new_e2 = entities2[0];

                entities1.Dispose();
                entities2.Dispose();

                Assert.AreEqual(1, entityManager.GetComponentData <EcsTestData>(new_e1).value);
                Assert.AreEqual(42, entityManager.GetChunkComponentData <EcsTestData3>(new_e1).value0);

                Assert.AreEqual(2, entityManager.GetComponentData <EcsTestData2>(new_e2).value0);
                Assert.AreEqual(57, entityManager.GetChunkComponentData <EcsTestData3>(new_e2).value0);
            }
            finally
            {
                deserializedWorld.Dispose();
                reader.Dispose();
            }
        }
Esempio n. 4
0
        public void SerializeDoesntRemapOriginalHeapBuffers()
        {
            var dummyEntity = CreateEntityWithDefaultData(0); //To ensure entity indices are offset

            var e1 = m_Manager.CreateEntity();

            m_Manager.AddComponentData(e1, new EcsTestData(1));
            var e2 = m_Manager.CreateEntity();

            m_Manager.AddComponentData(e2, new EcsTestData2(2));

            m_Manager.AddBuffer <TestBufferElement>(e1);
            var buffer1 = m_Manager.GetBuffer <TestBufferElement>(e1);

            for (int i = 0; i < 1024; ++i)
            {
                buffer1.Add(new TestBufferElement {
                    entity = e2, value = 2
                });
            }

            m_Manager.AddBuffer <TestBufferElement>(e2);
            var buffer2 = m_Manager.GetBuffer <TestBufferElement>(e2);

            for (int i = 0; i < 8; ++i)
            {
                buffer2.Add(new TestBufferElement {
                    entity = e1, value = 1
                });
            }

            m_Manager.DestroyEntity(dummyEntity);
            // disposed via reader
            var writer = new TestBinaryWriter();

            int[] sharedData;
            SerializeUtility.SerializeWorld(m_Manager, writer, out sharedData);

            buffer1 = m_Manager.GetBuffer <TestBufferElement>(e1);
            for (int i = 0; i < buffer1.Length; ++i)
            {
                Assert.AreEqual(e2, buffer1[i].entity);
                Assert.AreEqual(2, buffer1[i].value);
            }

            buffer2 = m_Manager.GetBuffer <TestBufferElement>(e2);
            for (int i = 0; i < buffer2.Length; ++i)
            {
                Assert.AreEqual(e1, buffer2[i].entity);
                Assert.AreEqual(1, buffer2[i].value);
            }

            writer.Dispose();
        }
        public void SharedComponentSerialize()
        {
            for (int i = 0; i != 20; i++)
            {
                var entity = m_Manager.CreateEntity();
                m_Manager.AddSharedComponentData(entity, new MockSharedData {
                    Value = i
                });
                m_Manager.AddComponentData(entity, new EcsTestData(i));
            }

            var writer = new TestBinaryWriter();

            GameObject sharedComponents;

            SerializeUtilityHybrid.Serialize(m_Manager, writer, out sharedComponents);

            var reader = new TestBinaryReader(writer);

            var world = new World("temp");

            SerializeUtilityHybrid.Deserialize(world.GetOrCreateManager <EntityManager>(), reader, sharedComponents);

            var newWorldEntities = world.GetOrCreateManager <EntityManager>();

            {
                var entities = newWorldEntities.GetAllEntities();

                Assert.AreEqual(20, entities.Length);

                for (int i = 0; i != 20; i++)
                {
                    Assert.AreEqual(i, newWorldEntities.GetComponentData <EcsTestData>(entities[i]).value);
                    Assert.AreEqual(i, newWorldEntities.GetSharedComponentData <MockSharedData>(entities[i]).Value);
                }
                for (int i = 0; i != 20; i++)
                {
                    newWorldEntities.DestroyEntity(entities[i]);
                }

                entities.Dispose();
            }

            Assert.IsTrue(newWorldEntities.Debug.IsSharedComponentManagerEmpty());

            world.Dispose();
            reader.Dispose();

            Object.DestroyImmediate(sharedComponents);
        }
Esempio n. 6
0
        public void SerializeIntoExistingWorldThrows()
        {
            m_Manager.CreateEntity(typeof(EcsTestData));

            var writer = new TestBinaryWriter();

            int[] sharedData;
            SerializeUtility.SerializeWorld(m_Manager, writer, out sharedData);

            var reader = new TestBinaryReader(writer);

            Assert.Throws <ArgumentException>(() =>
                                              SerializeUtility.DeserializeWorld(m_Manager.BeginExclusiveEntityTransaction(), reader)
                                              );
            reader.Dispose();
        }
Esempio n. 7
0
        public void SerializeEntitiesSupportsNonASCIIComponentTypeNames()
        {
            var e1 = m_Manager.CreateEntity();

            m_Manager.AddComponentData(e1, new 测试 {
                value = 7
            });

            // disposed via reader
            var writer = new TestBinaryWriter();

            int[] sharedData;
            SerializeUtility.SerializeWorld(m_Manager, writer, out sharedData);
            var reader = new TestBinaryReader(writer);

            var deserializedWorld = new World("SerializeEntitiesSupportsNonASCIIComponentTypeNames Test World");
            var entityManager     = deserializedWorld.EntityManager;

            SerializeUtility.DeserializeWorld(entityManager.BeginExclusiveEntityTransaction(), reader, 0);
            entityManager.EndExclusiveEntityTransaction();

            try
            {
                var allEntities = entityManager.GetAllEntities(Allocator.Temp);
                var count       = allEntities.Length;
                allEntities.Dispose();

                Assert.AreEqual(1, count);

                var group1 = entityManager.CreateEntityQuery(typeof(测试));

                Assert.AreEqual(1, group1.CalculateEntityCount());

                var entities = group1.ToEntityArray(Allocator.TempJob);
                var new_e1   = entities[0];
                entities.Dispose();

                Assert.AreEqual(7, entityManager.GetComponentData <测试>(new_e1).value);
            }
            finally
            {
                deserializedWorld.Dispose();
                reader.Dispose();
            }
        }
Esempio n. 8
0
        public void SerializingSharedComponent_WhenMoreThanOne_AndWrapperHasDisallowMultiple_DoesNotCrash()
        {
            for (var i = 0; i < 20; ++i)
            {
                var entity = m_Manager.CreateEntity();
                m_Manager.AddSharedComponentData(entity, new MockSharedDisallowMultiple {
                    Value = i
                });
                m_Manager.AddComponentData(entity, new EcsTestData(i));
            }

            var        writer           = new TestBinaryWriter();
            GameObject sharedComponents = null;

            try
            {
                var ex = Assert.Throws <ArgumentException>(
                    () => SerializeUtilityHybrid.Serialize(m_Manager, writer, out sharedComponents)
                    );
                Assert.That(
                    ex.Message,
                    Is.EqualTo(
                        string.Format(
                            "{0} is marked with {1}, but current implementation of {2} serializes all shared components on a single GameObject.",
                            typeof(MockSharedDisallowMultipleComponent),
                            typeof(DisallowMultipleComponent),
                            nameof(SerializeUtilityHybrid.SerializeSharedComponents)
                            )
                        )
                    );
            }
            finally
            {
                writer.Dispose();
                if (sharedComponents != null)
                {
                    GameObject.DestroyImmediate(sharedComponents);
                }
            }
        }
Esempio n. 9
0
        // https://fogbugz.unity3d.com/f/cases/1204153/
        public void SharedComponentUnityObjectSerialize_Case_1204153()
        {
            var go1    = new GameObject();
            var go2    = new GameObject();
            var shared = new SharedComponentWithUnityObject {
                obj = go1
            };
            var entity = m_Manager.CreateEntity();

            m_Manager.AddSharedComponentData(entity, shared);
            var writer = new TestBinaryWriter();

            ReferencedUnityObjects objRefs = null;

            SerializeUtilityHybrid.Serialize(m_Manager, writer, out objRefs);
            var reader = new TestBinaryReader(writer);

            // Swap the obj ref to a different instance but same value.  This can occur with any Unity.Object
            // during subscene serialize/deserialize since the editor's Unity.Object.GetHashCode() returns
            // an instance ID instead of a hash of the values in it.  There is no reasonable expectation that
            // the instance IDs would match between a serialize and deserialize.
            objRefs.Array[0] = go2;

            var world = new World("temp");

            SerializeUtilityHybrid.Deserialize(world.EntityManager, reader, objRefs);

            var newWorldEntities = world.EntityManager;
            var uniqueShared     = new List <SharedComponentWithUnityObject>();
            var query            = newWorldEntities.CreateEntityQuery(ComponentType.ReadWrite <SharedComponentWithUnityObject>());

            newWorldEntities.GetAllUniqueSharedComponentData(uniqueShared);
            Assert.AreEqual(2, uniqueShared.Count);
            query.SetSharedComponentFilter(uniqueShared[1]);
            Assert.AreEqual(1, query.CalculateEntityCount());

            query.Dispose();
            world.Dispose();
            reader.Dispose();
        }
Esempio n. 10
0
        public unsafe void SerializeEntities()
        {
            var dummyEntity = CreateEntityWithDefaultData(0); //To ensure entity indices are offset
            var e1          = CreateEntityWithDefaultData(1);
            var e2          = CreateEntityWithDefaultData(2);
            var e3          = CreateEntityWithDefaultData(3);

            m_Manager.AddComponentData(e1, new TestComponentData1 {
                value = 10, referencedEntity = e2
            });
            m_Manager.AddComponentData(e2, new TestComponentData2 {
                value = 20, referencedEntity = e1
            });
            m_Manager.AddComponentData(e3, new TestComponentData1 {
                value = 30, referencedEntity = Entity.Null
            });
            m_Manager.AddComponentData(e3, new TestComponentData2 {
                value = 40, referencedEntity = Entity.Null
            });
            m_Manager.AddBuffer <EcsIntElement>(e1);
            m_Manager.RemoveComponent <EcsTestData2>(e3);
            m_Manager.AddBuffer <EcsIntElement>(e3);

            m_Manager.GetBuffer <EcsIntElement>(e1).CopyFrom(new EcsIntElement[] { 1, 2, 3 });                       // no overflow
            m_Manager.GetBuffer <EcsIntElement>(e3).CopyFrom(new EcsIntElement[] { 1, 2, 3, 4, 5, 6, 7, 8, 9, 10 }); // overflow into heap

            var e4 = m_Manager.CreateEntity();

            m_Manager.AddBuffer <EcsComplexEntityRefElement>(e4);
            var ebuf = m_Manager.GetBuffer <EcsComplexEntityRefElement>(e4);

            ebuf.Add(new EcsComplexEntityRefElement {
                Entity = e1, Dummy = 1
            });
            ebuf.Add(new EcsComplexEntityRefElement {
                Entity = e2, Dummy = 2
            });
            ebuf.Add(new EcsComplexEntityRefElement {
                Entity = e3, Dummy = 3
            });

            m_Manager.DestroyEntity(dummyEntity);
            var writer = new TestBinaryWriter();

            int[] sharedData;
            SerializeUtility.SerializeWorld(m_Manager, writer, out sharedData);
            var reader = new TestBinaryReader(writer);

            var deserializedWorld = new World("SerializeEntities Test World 3");
            var entityManager     = deserializedWorld.GetOrCreateManager <EntityManager>();

            SerializeUtility.DeserializeWorld(entityManager.BeginExclusiveEntityTransaction(), reader);
            entityManager.EndExclusiveEntityTransaction();

            try
            {
                var allEntities = entityManager.GetAllEntities(Allocator.Temp);
                var count       = allEntities.Length;
                allEntities.Dispose();

                Assert.AreEqual(4, count);

                var group1 = entityManager.CreateComponentGroup(typeof(EcsTestData), typeof(EcsTestData2),
                                                                typeof(TestComponentData1));
                var group2 = entityManager.CreateComponentGroup(typeof(EcsTestData), typeof(EcsTestData2),
                                                                typeof(TestComponentData2));
                var group3 = entityManager.CreateComponentGroup(typeof(EcsTestData),
                                                                typeof(TestComponentData1), typeof(TestComponentData2));
                var group4 = entityManager.CreateComponentGroup(typeof(EcsComplexEntityRefElement));

                Assert.AreEqual(1, group1.CalculateLength());
                Assert.AreEqual(1, group2.CalculateLength());
                Assert.AreEqual(1, group3.CalculateLength());
                Assert.AreEqual(1, group4.CalculateLength());

                var query = new EntityArchetypeQuery
                {
                    All  = Array.Empty <ComponentType>(),
                    Any  = Array.Empty <ComponentType>(),
                    None = Array.Empty <ComponentType>()
                };

                var archetypes = new NativeList <EntityArchetype>(Allocator.TempJob);
                entityManager.AddMatchingArchetypes(query, archetypes);
                Assert.AreEqual(4, archetypes.Length);

                var chunks = entityManager.CreateArchetypeChunkArray(archetypes, Allocator.TempJob);
                Assert.AreEqual(4, chunks.Length);
                archetypes.Dispose();

                var entityType = entityManager.GetArchetypeChunkEntityType();
                Assert.AreEqual(1, chunks[0].GetNativeArray(entityType).Length);
                Assert.AreEqual(1, chunks[1].GetNativeArray(entityType).Length);
                Assert.AreEqual(1, chunks[2].GetNativeArray(entityType).Length);
                Assert.AreEqual(1, chunks[3].GetNativeArray(entityType).Length);
                chunks.Dispose();

                var new_e1 = group1.GetEntityArray()[0];
                var new_e2 = group2.GetEntityArray()[0];
                var new_e3 = group3.GetEntityArray()[0];
                var new_e4 = group4.GetEntityArray()[0];

                Assert.AreEqual(1, entityManager.GetComponentData <EcsTestData>(new_e1).value);
                Assert.AreEqual(-1, entityManager.GetComponentData <EcsTestData2>(new_e1).value0);
                Assert.AreEqual(-1, entityManager.GetComponentData <EcsTestData2>(new_e1).value1);
                Assert.AreEqual(10, entityManager.GetComponentData <TestComponentData1>(new_e1).value);

                Assert.AreEqual(2, entityManager.GetComponentData <EcsTestData>(new_e2).value);
                Assert.AreEqual(-2, entityManager.GetComponentData <EcsTestData2>(new_e2).value0);
                Assert.AreEqual(-2, entityManager.GetComponentData <EcsTestData2>(new_e2).value1);
                Assert.AreEqual(20, entityManager.GetComponentData <TestComponentData2>(new_e2).value);

                Assert.AreEqual(3, entityManager.GetComponentData <EcsTestData>(new_e3).value);
                Assert.AreEqual(30, entityManager.GetComponentData <TestComponentData1>(new_e3).value);
                Assert.AreEqual(40, entityManager.GetComponentData <TestComponentData2>(new_e3).value);

                Assert.IsTrue(entityManager.Exists(entityManager.GetComponentData <TestComponentData1>(new_e1).referencedEntity));
                Assert.IsTrue(entityManager.Exists(entityManager.GetComponentData <TestComponentData2>(new_e2).referencedEntity));
                Assert.AreEqual(new_e2, entityManager.GetComponentData <TestComponentData1>(new_e1).referencedEntity);
                Assert.AreEqual(new_e1, entityManager.GetComponentData <TestComponentData2>(new_e2).referencedEntity);

                var buf1 = entityManager.GetBuffer <EcsIntElement>(new_e1);
                Assert.AreEqual(3, buf1.Length);
                Assert.AreNotEqual((UIntPtr)m_Manager.GetBuffer <EcsIntElement>(e1).GetBasePointer(), (UIntPtr)buf1.GetBasePointer());

                for (int i = 0; i < 3; ++i)
                {
                    Assert.AreEqual(i + 1, buf1[i].Value);
                }

                var buf3 = entityManager.GetBuffer <EcsIntElement>(new_e3);
                Assert.AreEqual(10, buf3.Length);
                Assert.AreNotEqual((UIntPtr)m_Manager.GetBuffer <EcsIntElement>(e3).GetBasePointer(), (UIntPtr)buf3.GetBasePointer());

                for (int i = 0; i < 10; ++i)
                {
                    Assert.AreEqual(i + 1, buf3[i].Value);
                }

                var buf4 = entityManager.GetBuffer <EcsComplexEntityRefElement>(new_e4);
                Assert.AreEqual(3, buf4.Length);

                Assert.AreEqual(1, buf4[0].Dummy);
                Assert.AreEqual(new_e1, buf4[0].Entity);

                Assert.AreEqual(2, buf4[1].Dummy);
                Assert.AreEqual(new_e2, buf4[1].Entity);

                Assert.AreEqual(3, buf4[2].Dummy);
                Assert.AreEqual(new_e3, buf4[2].Entity);
            }
            finally
            {
                deserializedWorld.Dispose();
                reader.Dispose();
            }
        }
Esempio n. 11
0
 public TestBinaryReader(TestBinaryWriter writer)
 {
     content        = writer.content;
     writer.content = new NativeList <byte>();
 }
        public void SharedComponentSerialize()
        {
            for (int i = 0; i != 20; i++)
            {
                var entity = m_Manager.CreateEntity();
                m_Manager.AddSharedComponentData(entity, new MockSharedData {
                    Value = i
                });
                m_Manager.AddComponentData(entity, new EcsTestData(i));
                var buffer = m_Manager.AddBuffer <EcsIntElement>(entity);
                foreach (var val in Enumerable.Range(i, i + 5))
                {
                    buffer.Add(new EcsIntElement {
                        Value = val
                    });
                }
            }

            var writer = new TestBinaryWriter();

            GameObject sharedComponents;

            SerializeUtilityHybrid.Serialize(m_Manager, writer, out sharedComponents);

            var reader = new TestBinaryReader(writer);

            var world = new World("temp");

            SerializeUtilityHybrid.Deserialize(world.EntityManager, reader, sharedComponents);

            var newWorldEntities = world.EntityManager;

            {
                var entities = newWorldEntities.GetAllEntities();

                Assert.AreEqual(20, entities.Length);

                for (int i = 0; i != 20; i++)
                {
                    Assert.AreEqual(i, newWorldEntities.GetComponentData <EcsTestData>(entities[i]).value);
                    Assert.AreEqual(i, newWorldEntities.GetSharedComponentData <MockSharedData>(entities[i]).Value);
                    var buffer = newWorldEntities.GetBuffer <EcsIntElement>(entities[i]);
                    Assert.That(
                        buffer.AsNativeArray().ToArray(),
                        Is.EqualTo(Enumerable.Range(i, i + 5).Select(x => new EcsIntElement {
                        Value = x
                    }))
                        );
                }
                for (int i = 0; i != 20; i++)
                {
                    newWorldEntities.DestroyEntity(entities[i]);
                }

                entities.Dispose();
            }

            Assert.IsTrue(newWorldEntities.Debug.IsSharedComponentManagerEmpty());

            world.Dispose();
            reader.Dispose();

            Object.DestroyImmediate(sharedComponents);
        }
Esempio n. 13
0
        public unsafe void SerializeEntitiesRemapsEntitiesInBuffers()
        {
            var dummyEntity = CreateEntityWithDefaultData(0); //To ensure entity indices are offset

            var e1 = m_Manager.CreateEntity();

            m_Manager.AddComponentData(e1, new EcsTestData(1));
            var e2 = m_Manager.CreateEntity();

            m_Manager.AddComponentData(e2, new EcsTestData2(2));

            m_Manager.AddBuffer <TestBufferElement>(e1);
            var buffer1 = m_Manager.GetBuffer <TestBufferElement>(e1);

            for (int i = 0; i < 1024; ++i)
            {
                buffer1.Add(new TestBufferElement {
                    entity = e2, value = 2
                });
            }

            m_Manager.AddBuffer <TestBufferElement>(e2);
            var buffer2 = m_Manager.GetBuffer <TestBufferElement>(e2);

            for (int i = 0; i < 8; ++i)
            {
                buffer2.Add(new TestBufferElement {
                    entity = e1, value = 1
                });
            }

            m_Manager.DestroyEntity(dummyEntity);
            var writer = new TestBinaryWriter();

            int[] sharedData;
            SerializeUtility.SerializeWorld(m_Manager, writer, out sharedData);
            var reader = new TestBinaryReader(writer);

            var deserializedWorld = new World("SerializeEntities Test World 3");
            var entityManager     = deserializedWorld.GetOrCreateManager <EntityManager>();

            SerializeUtility.DeserializeWorld(entityManager.BeginExclusiveEntityTransaction(), reader, 0);
            entityManager.EndExclusiveEntityTransaction();

            try
            {
                var group1 = entityManager.CreateComponentGroup(typeof(EcsTestData), typeof(TestBufferElement));
                var group2 = entityManager.CreateComponentGroup(typeof(EcsTestData2), typeof(TestBufferElement));

                Assert.AreEqual(1, group1.CalculateLength());
                Assert.AreEqual(1, group2.CalculateLength());

                var new_e1 = group1.GetEntityArray()[0];
                var new_e2 = group2.GetEntityArray()[0];

                var newBuffer1 = entityManager.GetBuffer <TestBufferElement>(new_e1);
                Assert.AreEqual(1024, newBuffer1.Length);
                for (int i = 0; i < 1024; ++i)
                {
                    Assert.AreEqual(new_e2, newBuffer1[i].entity);
                    Assert.AreEqual(2, newBuffer1[i].value);
                }

                var newBuffer2 = entityManager.GetBuffer <TestBufferElement>(new_e2);
                Assert.AreEqual(8, newBuffer2.Length);
                for (int i = 0; i < 8; ++i)
                {
                    Assert.AreEqual(new_e1, newBuffer2[i].entity);
                    Assert.AreEqual(1, newBuffer2[i].value);
                }
            }
            finally
            {
                deserializedWorld.Dispose();
                reader.Dispose();
            }
        }
Esempio n. 14
0
        public void ManagedComponentWithObjectReferenceSerialize()
        {
            for (int i = 0; i != 20; i++)
            {
                var e1 = m_Manager.CreateEntity();

                UnityEngine.Texture2D tex    = new UnityEngine.Texture2D(i + 1, i + 1);
                var expectedManagedComponent = new ManagedComponentWithObjectReference {
                    Texture = tex
                };

                m_Manager.AddComponentData(e1, expectedManagedComponent);
            }

            var writer = new TestBinaryWriter();
            ReferencedUnityObjects objRefs = null;

            SerializeUtilityHybrid.Serialize(m_Manager, writer, out objRefs);

            var world  = new World("temp");
            var reader = new TestBinaryReader(writer);

            SerializeUtilityHybrid.Deserialize(world.EntityManager, reader, objRefs);

            var newWorldEntities = world.EntityManager;

            {
                var entities = newWorldEntities.GetAllEntities();
                Assert.AreEqual(20, entities.Length);

                var seenWidths  = new NativeArray <bool>(entities.Length, Allocator.Temp);
                var seenHeights = new NativeArray <bool>(entities.Length, Allocator.Temp);
                for (int i = 0; i < entities.Length; ++i)
                {
                    var e = entities[i];

                    var actualManagedComponent = newWorldEntities.GetComponentData <ManagedComponentWithObjectReference>(e);
                    Assert.NotNull(actualManagedComponent);
                    var tex = actualManagedComponent.Texture;
                    seenWidths[tex.width - 1]   = true;
                    seenHeights[tex.height - 1] = true;
                }

                for (int i = 0; i < entities.Length; ++i)
                {
                    Assert.IsTrue(seenWidths[i]);
                    Assert.IsTrue(seenHeights[i]);
                }

                seenWidths.Dispose();
                seenHeights.Dispose();

                for (int i = 0; i != 20; i++)
                {
                    newWorldEntities.DestroyEntity(entities[i]);
                }

                entities.Dispose();
            }

            Assert.IsTrue(newWorldEntities.Debug.IsSharedComponentManagerEmpty());

            world.Dispose();
            reader.Dispose();
        }
Esempio n. 15
0
        public void SerializeEntities()
        {
            var dummyEntity = CreateEntityWithDefaultData(0); //To ensure entity indices are offset
            var e1          = CreateEntityWithDefaultData(1);
            var e2          = CreateEntityWithDefaultData(2);
            var e3          = CreateEntityWithDefaultData(3);

            m_Manager.AddComponentData(e1, new TestComponentData1 {
                value = 10, referencedEntity = e2
            });
            m_Manager.AddComponentData(e2, new TestComponentData2 {
                value = 20, referencedEntity = e1
            });
            m_Manager.AddComponentData(e3, new TestComponentData1 {
                value = 30, referencedEntity = Entity.Null
            });
            m_Manager.AddComponentData(e3, new TestComponentData2 {
                value = 40, referencedEntity = Entity.Null
            });
            m_Manager.RemoveComponent <EcsTestData2>(e3);

            m_Manager.DestroyEntity(dummyEntity);

            var writer = new TestBinaryWriter();

            int[] sharedData;
            SerializeUtility.SerializeWorld(m_Manager, writer, out sharedData);
            var reader = new TestBinaryReader(writer);

            var deserializedWorld = new World("SerializeEntities Test World 3");
            var entityManager     = deserializedWorld.GetOrCreateManager <EntityManager>();

            SerializeUtility.DeserializeWorld(entityManager.BeginExclusiveEntityTransaction(), reader);
            entityManager.EndExclusiveEntityTransaction();

            try
            {
                var allEntities = entityManager.GetAllEntities(Allocator.Temp);
                var count       = allEntities.Length;
                allEntities.Dispose();

                Assert.AreEqual(3, count);

                var group1 = entityManager.CreateComponentGroup(typeof(EcsTestData), typeof(EcsTestData2),
                                                                typeof(TestComponentData1));
                var group2 = entityManager.CreateComponentGroup(typeof(EcsTestData), typeof(EcsTestData2),
                                                                typeof(TestComponentData2));
                var group3 = entityManager.CreateComponentGroup(typeof(EcsTestData),
                                                                typeof(TestComponentData1), typeof(TestComponentData2));

                Assert.AreEqual(1, group1.CalculateLength());
                Assert.AreEqual(1, group2.CalculateLength());
                Assert.AreEqual(1, group3.CalculateLength());

                var new_e1 = group1.GetEntityArray()[0];
                var new_e2 = group2.GetEntityArray()[0];
                var new_e3 = group3.GetEntityArray()[0];

                Assert.AreEqual(1, entityManager.GetComponentData <EcsTestData>(new_e1).value);
                Assert.AreEqual(-1, entityManager.GetComponentData <EcsTestData2>(new_e1).value0);
                Assert.AreEqual(-1, entityManager.GetComponentData <EcsTestData2>(new_e1).value1);
                Assert.AreEqual(10, entityManager.GetComponentData <TestComponentData1>(new_e1).value);

                Assert.AreEqual(2, entityManager.GetComponentData <EcsTestData>(new_e2).value);
                Assert.AreEqual(-2, entityManager.GetComponentData <EcsTestData2>(new_e2).value0);
                Assert.AreEqual(-2, entityManager.GetComponentData <EcsTestData2>(new_e2).value1);
                Assert.AreEqual(20, entityManager.GetComponentData <TestComponentData2>(new_e2).value);

                Assert.AreEqual(3, entityManager.GetComponentData <EcsTestData>(new_e3).value);
                Assert.AreEqual(30, entityManager.GetComponentData <TestComponentData1>(new_e3).value);
                Assert.AreEqual(40, entityManager.GetComponentData <TestComponentData2>(new_e3).value);

                Assert.IsTrue(entityManager.Exists(entityManager.GetComponentData <TestComponentData1>(new_e1).referencedEntity));
                Assert.IsTrue(entityManager.Exists(entityManager.GetComponentData <TestComponentData2>(new_e2).referencedEntity));
                Assert.AreEqual(new_e2, entityManager.GetComponentData <TestComponentData1>(new_e1).referencedEntity);
                Assert.AreEqual(new_e1, entityManager.GetComponentData <TestComponentData2>(new_e2).referencedEntity);
            }
            finally
            {
                deserializedWorld.Dispose();
                reader.Dispose();
            }
        }