Пример #1
0
        internal static unsafe JobHandle ScheduleInternal <T>(ref T jobData, EntityQuery query, JobHandle dependsOn, ScheduleMode mode)
            where T : struct, IJobChunk
        {
            ComponentChunkIterator iterator = query.GetComponentChunkIterator();

            var unfilteredChunkCount = query.CalculateChunkCountWithoutFiltering();

            var prefilterHandle = ComponentChunkIterator.PreparePrefilteredChunkLists(unfilteredChunkCount,
                                                                                      iterator.m_MatchingArchetypeList, iterator.m_Filter, dependsOn, mode, out var prefilterData,
                                                                                      out var deferredCountData);

            JobChunkData <T> fullData = new JobChunkData <T>
            {
#if ENABLE_UNITY_COLLECTIONS_CHECKS
                // All IJobChunk jobs have a EntityManager safety handle to ensure that BeforeStructuralChange throws an error if
                // jobs without any other safety handles are still running (haven't been synced).
                safety = new EntitySafetyHandle {
                    m_Safety = query.SafetyManager->GetEntityManagerSafetyHandle()
                },
#endif
                Data          = jobData,
                PrefilterData = prefilterData,
            };

            var scheduleParams = new JobsUtility.JobScheduleParameters(
                UnsafeUtility.AddressOf(ref fullData),
                JobChunk_Process <T> .Initialize(),
                prefilterHandle,
                mode);

#if ENABLE_UNITY_COLLECTIONS_CHECKS
            try
            {
#endif
            if (mode == ScheduleMode.Batched)
            {
                return(JobsUtility.ScheduleParallelForDeferArraySize(ref scheduleParams, 1, deferredCountData, null));
            }
            else
            {
                var count = unfilteredChunkCount;
                return(JobsUtility.ScheduleParallelFor(ref scheduleParams, count, 1));
            }
#if ENABLE_UNITY_COLLECTIONS_CHECKS
        }

        catch (InvalidOperationException e)
        {
            prefilterData.Dispose();
            throw e;
        }
#endif
        }
Пример #2
0
        public static T[] ToComponentArray <T>(this EntityQuery group) where T : Component
        {
            int length = group.CalculateLength();
            ComponentChunkIterator iterator = group.GetComponentChunkIterator();
            var indexInComponentGroup       = group.GetIndexInEntityQuery(TypeManager.GetTypeIndex <T>());

            iterator.IndexInEntityQuery = indexInComponentGroup;

            var arr   = new T[length];
            var cache = default(ComponentChunkCache);

            for (int i = 0; i < length; ++i)
            {
                if (i < cache.CachedBeginIndex || i >= cache.CachedEndIndex)
                {
                    iterator.MoveToEntityIndexAndUpdateCache(i, out cache, true);
                }

                arr[i] = (T)iterator.GetManagedObject(group.ManagedComponentStore, cache.CachedBeginIndex, i);
            }

            return(arr);
        }
        /// <summary>
        /// Destroy all entities having a common set of component types.
        /// </summary>
        /// <remarks>Since entities in the same chunk share the same component structure, this function effectively destroys
        /// the chunks holding any entities identified by the `entityQueryFilter` parameter.</remarks>
        /// <param name="entityQueryFilter">Defines the components an entity must have to qualify for destruction.</param>
        public void DestroyEntity(EntityQuery entityQuery)
        {
            var iterator = entityQuery.GetComponentChunkIterator();

            DestroyEntity(iterator.m_MatchingArchetypeList, iterator.m_Filter);
        }
Пример #4
0
        /// <summary>
        /// Adds a component to a set of entities defined by a EntityQuery.
        /// </summary>
        /// <remarks>
        /// Adding a component changes an entity's archetype and results in the entity being moved to a different
        /// chunk.
        ///
        /// The added components have the default values for the type.
        ///
        /// **Important:** This function creates a sync point, which means that the EntityManager waits for all
        /// currently running Jobs to complete before adding the component and no additional Jobs can start before
        /// the function is finished. A sync point can cause a drop in performance because the ECS framework may not
        /// be able to make use of the processing power of all available cores.
        /// </remarks>
        /// <param name="entityQuery">The EntityQuery defining the entities to modify.</param>
        /// <param name="componentType">The type of component to add.</param>
        public void AddComponent(EntityQuery entityQuery, ComponentType componentType)
        {
            var iterator = entityQuery.GetComponentChunkIterator();

            AddComponent(iterator.m_MatchingArchetypeList, iterator.m_Filter, componentType);
        }