void UpdateChangeParents() { if (m_ExistingParentsQuery.IsEmptyIgnoreFilter) { return; } var count = m_ExistingParentsQuery.CalculateEntityCount() * 2; // Potentially 2x changed: current and previous if (count == 0) { return; } // 1. Get (Parent,Child) to remove // 2. Get (Parent,Child) to add // 3. Get unique Parent change list // 4. Set PreviousParent to new Parent var parentChildrenToAdd = new NativeMultiHashMap <Entity, Entity>(count, Allocator.TempJob); var parentChildrenToRemove = new NativeMultiHashMap <Entity, Entity>(count, Allocator.TempJob); var uniqueParents = new NativeHashMap <Entity, int>(count, Allocator.TempJob); var gatherChangedParentsJob = new GatherChangedParents { ParentChildrenToAdd = parentChildrenToAdd.AsParallelWriter(), ParentChildrenToRemove = parentChildrenToRemove.AsParallelWriter(), UniqueParents = uniqueParents.AsParallelWriter(), PreviousParentTypeHandle = GetComponentTypeHandle <PreviousParent>(false), ParentTypeHandle = GetComponentTypeHandle <Parent>(true), EntityTypeHandle = GetEntityTypeHandle(), LastSystemVersion = LastSystemVersion }; var gatherChangedParentsJobHandle = gatherChangedParentsJob.ScheduleParallel(m_ExistingParentsQuery, 1, default(JobHandle)); gatherChangedParentsJobHandle.Complete(); // 5. (Structural change) Add any missing Child to Parents var parentsMissingChild = new NativeList <Entity>(Allocator.TempJob); var findMissingChildJob = new FindMissingChild { UniqueParents = uniqueParents, ChildFromEntity = GetBufferFromEntity <Child>(), ParentsMissingChild = parentsMissingChild }; var findMissingChildJobHandle = findMissingChildJob.Schedule(); findMissingChildJobHandle.Complete(); EntityManager.AddComponent(parentsMissingChild.AsArray(), typeof(Child)); // 6. Get Child[] for each unique Parent // 7. Update Child[] for each unique Parent var fixupChangedChildrenJob = new FixupChangedChildren { ParentChildrenToAdd = parentChildrenToAdd, ParentChildrenToRemove = parentChildrenToRemove, UniqueParents = uniqueParents, ChildFromEntity = GetBufferFromEntity <Child>() }; var fixupChangedChildrenJobHandle = fixupChangedChildrenJob.Schedule(); fixupChangedChildrenJobHandle.Complete(); parentChildrenToAdd.Dispose(); parentChildrenToRemove.Dispose(); uniqueParents.Dispose(); parentsMissingChild.Dispose(); }
void UpdateChildWhoseParentIsChanged()//Including change form previous updates { if (ChildParentChangeQuery.IsEmptyIgnoreFilter) { return; } var count = ChildParentChangeQuery.CalculateEntityCount() * 2; // Potentially 2x changed: current and previous if (count == 0) { return; } // 1. Get (Parent,Child) to remove // 2. Get (Parent,Child) to add // 3. Get unique Parent change list // 4. Set PreviousParent to new Parent var parentChildrenToAdd = new NativeMultiHashMap <Entity, Entity>(count, Allocator.TempJob); var parentChildrenToRemove = new NativeMultiHashMap <Entity, Entity>(count, Allocator.TempJob); var uniqueParents = new NativeHashMap <Entity, int>(count, Allocator.TempJob); var gatherChangedParentsJob = new GatherChangedParents { ParentChildrenToAdd = parentChildrenToAdd.AsParallelWriter(), ParentChildrenToRemove = parentChildrenToRemove.AsParallelWriter(), tsAccess = GetComponentDataFromEntity <TimeScale>(true), ltsAccess = GetComponentDataFromEntity <LocalTimeScale>(false), UniqueParents = uniqueParents.AsParallelWriter(), PreviousParentType = GetArchetypeChunkComponentType <PreviousParentTime>(false), ParentType = GetArchetypeChunkComponentType <ParentTime>(true), EntityType = GetArchetypeChunkEntityType(), LastSystemVersion = LastSystemVersion }; var gatherChangedParentsJobHandle = gatherChangedParentsJob.Schedule(ChildParentChangeQuery); gatherChangedParentsJobHandle.Complete(); // 5. (Structural change) Add any missing Child to Parents var parentsMissingChildBuf = new NativeList <Entity>(Allocator.TempJob); var findMissingChildJob = new FindMissingChild { UniqueParents = uniqueParents, ChildFromEntity = GetBufferFromEntity <ChildTime>(), ParentsMissingChild = parentsMissingChildBuf }; var findMissingChildJobHandle = findMissingChildJob.Schedule(); findMissingChildJobHandle.Complete(); EntityManager.AddComponent(parentsMissingChildBuf.AsArray(), typeof(ChildTime)); // 6. Get Child[] for each unique Parent // 7. Update Child[] for each unique Parent var fixupChangedChildrenJob = new FixupChangedChildren { ParentChildrenToAdd = parentChildrenToAdd, ParentChildrenToRemove = parentChildrenToRemove, UniqueParents = uniqueParents, ChildFromEntity = GetBufferFromEntity <ChildTime>() }; var fixupChangedChildrenJobHandle = fixupChangedChildrenJob.Schedule(); fixupChangedChildrenJobHandle.Complete(); //Mark Time that became Parent with HasChildTIme tag. this tag is use to track Destroyed Parent entity //EntityManager.AddComponent<HasChildTime>(ParentChildrenRemovedQuery); //todo add localtimescale timescale EntityManager.AddComponent <TimeScale>(ParentChildrenRemovedQuery); EntityManager.AddComponent <LocalTimeScale>(ParentChildrenRemovedQuery); //ParentChildrenRemovedQuery is the same as Parent whose has no marked HasChildTIme has childTime buffer added to it parentChildrenToAdd.Dispose(); parentChildrenToRemove.Dispose(); uniqueParents.Dispose(); parentsMissingChildBuf.Dispose(); }