public void RemoveComponent_WhenArchetypeModifiedInsideForEach_CanModifySafely() { var arch = m_Manager.CreateArchetype(typeof(EcsTestData), typeof(EcsTestData2)); using (var ents = new NativeArray <Entity>(10, Allocator.Persistent)) { m_Manager.CreateEntity(arch, ents); for (int i = 0; i < 10; i++) { m_Manager.SetComponentData(ents[i], new EcsTestData(i + 1)); } MutableEntities.ForEach((Entity entity, ref EcsTestData c0, ref EcsTestData2 c1) => { m_Manager.RemoveComponent <EcsTestData>(entity); c1.value0 = -c0.value; }); using (var group = new ExtractTestDataFromEntityManager <EcsTestData>(m_Manager)) { Assert.AreEqual(0, group.Values.Length); } using (var group = new ExtractTestDataFromEntityManager <EcsTestData2>(m_Manager)) { group.Sort(new EcsTestData2Comparer()); Assert.AreEqual(10, group.Values.Length); for (int i = 0; i < 10; i++) { Assert.AreEqual(-10 + i, group.Values[i].value0); } } } }
public void SetComponentData_WhenBothSetAndRefAreModified_RefWins() { var archA = EntityManager.CreateArchetype(typeof(EcsTestData), typeof(EcsTestData2)); using (var ents = new NativeArray <Entity>(4, Allocator.Persistent)) { EntityManager.CreateEntity(archA, ents); int count = 0; Entities .WithStructuralChanges() .ForEach((Entity entity, ref EcsTestData ca, ref EcsTestData2 ca2) => { switch (count) { case 0: EntityManager.SetComponentData(entity, new EcsTestData2(count * 100)); break; case 1: EntityManager.SetComponentData(entity, new EcsTestData2(count * 100)); break; case 2: EntityManager.SetComponentData(entity, new EcsTestData2(count * 100)); ca2.value0 = 1; break; case 3: ca2.value0 = -count * 100; break; case 4: EntityManager.CreateEntity(); break; } count = count + 1; }).Run(); Assert.AreEqual(ents.Length, count); using (var group = new ExtractTestDataFromEntityManager <EcsTestData2>(EntityManager)) { Assert.AreEqual(4, group.Values.Length); Assert.AreEqual(0, group.Values[0].value0); // case 0 Assert.AreEqual(100, group.Values[1].value0); // case 1 Assert.AreEqual(1, group.Values[2].value0); // case 2 Assert.AreEqual(-300, group.Values[3].value0); // case 3 } } }
public void NestedForEach_ArchetypeCreatedInsideForEach_CanExecuteNestedForEach() { Entity e = m_Manager.CreateEntity(); m_Manager.AddComponentData(e, new EcsTestData(5)); m_Manager.AddComponentData(e, new EcsTestData2(12)); MutableEntities.ForEach((Entity e1, ref EcsTestData testData) => { if (m_Manager.HasComponent <EcsTestData2>(e1)) { MutableEntities.ForEach((Entity e2, ref EcsTestData ed, ref EcsTestData2 ed2) => { m_Manager.SetComponentData(m_Manager.Instantiate(e2), new EcsTestData2(-ed.value)); }); } }); using (var group = m_Manager.CreateEntityQuery(typeof(EcsTestData), typeof(EcsTestData2))) { using (var arr = group.ToComponentDataArray <EcsTestData>(Allocator.TempJob)) { Assert.AreEqual(2, arr.Length); // (e) Assert.AreEqual(5, arr[0].value); Assert.AreEqual(5, arr[1].value); } } using (var group = new ExtractTestDataFromEntityManager <EcsTestData2>(m_Manager)) { Assert.AreEqual(2, group.Values.Length); group.Sort(new EcsTestData2Comparer()); Assert.AreEqual(-5, group.Values[0].value0); Assert.AreEqual(12, group.Values[1].value0); } }
public void Instantiate_HasGetComponent_VisibleFromInsideForEach() { m_Manager.AddComponentData(m_Manager.CreateEntity(), new EcsTestData(5)); MutableEntities.ForEach((Entity e, ref EcsTestData testData) => { Assert.AreEqual(5, testData.value); Assert.IsFalse(m_Manager.HasComponent <EcsTestData2>(e)); Entity newe1 = m_Manager.Instantiate(e); m_Manager.AddComponentData(e, new EcsTestData2() { value0 = 1, value1 = 3 }); { EcsTestData2 ed2 = m_Manager.GetComponentData <EcsTestData2>(e); Assert.AreEqual(3, ed2.value1); Assert.AreEqual(1, ed2.value0); } Entity deferred = m_Manager.CreateEntity(); m_Manager.AddComponentData(deferred, testData); { var ed = m_Manager.GetComponentData <EcsTestData>(deferred); Assert.AreEqual(testData.value, ed.value); } Entity newe2 = m_Manager.Instantiate(e); Assert.IsFalse(m_Manager.HasComponent <EcsTestData2>(newe1)); { EcsTestData ed = m_Manager.GetComponentData <EcsTestData>(newe1); Assert.AreEqual(5, ed.value); } Assert.IsTrue(m_Manager.HasComponent <EcsTestData2>(e)); { EcsTestData2 ed2 = m_Manager.GetComponentData <EcsTestData2>(newe2); Assert.AreEqual(3, ed2.value1); Assert.AreEqual(1, ed2.value0); } Assert.IsTrue(m_Manager.HasComponent <EcsTestData>(newe1)); m_Manager.RemoveComponent <EcsTestData>(newe1); Assert.IsFalse(m_Manager.HasComponent <EcsTestData>(newe1)); }); using (var allEntities = m_Manager.GetAllEntities()) Assert.AreEqual(4, allEntities.Length); using (var group = new ExtractTestDataFromEntityManager <EcsTestData>(m_Manager)) { Assert.AreEqual(3, group.Values.Length); Assert.AreEqual(5, group.Values[0].value); // e Assert.AreEqual(5, group.Values[1].value); // deferred Assert.AreEqual(5, group.Values[2].value); // newe2 } using (var group = new ExtractTestDataFromEntityManager <EcsTestData2>(m_Manager)) { Assert.AreEqual(2, group.Values.Length); // (e && newe2) Assert.AreEqual(3, group.Values[0].value1); Assert.AreEqual(1, group.Values[0].value0); Assert.AreEqual(3, group.Values[1].value1); Assert.AreEqual(1, group.Values[1].value0); } }