Exemple #1
0
        void UpdateChangeParents()
        {
            if (m_ExistingParentsGroup.IsEmptyIgnoreFilter)
            {
                return;
            }

            var count = m_ExistingParentsGroup.CalculateEntityCount();

            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(),
                PreviousParentType     = GetArchetypeChunkComponentType <PreviousParent>(false),
                ParentType             = GetArchetypeChunkComponentType <Parent>(true),
                EntityType             = GetArchetypeChunkEntityType(),
                LastSystemVersion      = LastSystemVersion
            };
            var gatherChangedParentsJobHandle = gatherChangedParentsJob.Schedule(m_ExistingParentsGroup);

            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();
        }