internal void AddSharedComponent <T>(NativeArray <ArchetypeChunk> chunks, T componentData)
            where T : struct, ISharedComponentData
        {
            ManagedComponentStore mcs = GetCheckedEntityDataAccess()->ManagedComponentStore;
            var componentType         = ComponentType.ReadWrite <T>();
            int sharedComponentIndex  = mcs.InsertSharedComponent(componentData);

            m_EntityDataAccess->AddSharedComponentData(chunks, sharedComponentIndex, componentType);
            mcs.RemoveReference(sharedComponentIndex);
        }
Example #2
0
        /// <summary>
        /// Sets the shared component of all entities in the query.
        /// </summary>
        /// <remarks>
        /// The component data stays in the same chunk, the internal shared component data indices will be adjusted.
        ///
        /// **Important:** This function creates a sync point, which means that the EntityManager waits for all
        /// currently running Jobs to complete before setting 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="entity">The entity</param>
        /// <param name="componentData">A shared component object containing the values to set.</param>
        /// <typeparam name="T">The shared component type.</typeparam>
        public void SetSharedComponentData <T>(EntityQuery query, T componentData) where T : struct, ISharedComponentData
        {
            using (var chunks = query.CreateArchetypeChunkArray(Allocator.TempJob))
            {
                if (chunks.Length == 0)
                {
                    return;
                }

                BeforeStructuralChange();

                var type = ComponentType.ReadWrite <T>();
                RemoveComponent(chunks, type);

                int sharedComponentIndex = ManagedComponentStore.InsertSharedComponent(componentData);
                AddSharedComponentData(chunks, sharedComponentIndex, type);
            }
        }