/// <summary>
        /// Sets the value of a singleton component.
        /// </summary>
        /// <param name="value">A component containing the value to assign to the singleton.</param>
        /// <typeparam name="T">The <see cref="IComponentData"/> subtype of the singleton component.</typeparam>
        /// <seealso cref="EntityQuery.SetSingleton{T}"/>
        public static void SetSingleton <T>(this ComponentSystemBase sys, T value) where T : class, IComponentData
        {
            var type  = ComponentType.ReadWrite <T>();
            var query = sys.GetEntityQueryInternal(&type, 1);

            query.SetSingleton(value);
        }
        /// <summary>
        /// Gets the value of a singleton component.
        /// </summary>
        /// <typeparam name="T">The <see cref="IComponentData"/> subtype of the singleton component.</typeparam>
        /// <returns>The component.</returns>
        /// <seealso cref="EntityQuery.GetSingleton{T}"/>
        public static T GetSingleton <T>(this ComponentSystemBase sys) where T : class, IComponentData
        {
            var type  = ComponentType.ReadOnly <T>();
            var query = sys.GetEntityQueryInternal(&type, 1);

            return(query.GetSingleton <T>());
        }
        /// <summary>
        /// Checks whether a singelton component of the specified type exists.
        /// </summary>
        /// <typeparam name="T">The <see cref="IComponentData"/> subtype of the singleton component.</typeparam>
        /// <returns>True, if a singleton of the specified type exists in the current <see cref="World"/>.</returns>
        public static bool HasSingleton <T>(this ComponentSystemBase sys) where T : class, IComponentData
        {
            var type  = ComponentType.ReadOnly <T>();
            var query = sys.GetEntityQueryInternal(&type, 1);

            return(query.CalculateEntityCount() == 1);
        }
Exemple #4
0
        static void PrepareEntityQuery(ComponentSystemBase system, Type jobType)
        {
            var iType = GetIJobForEachInterface(jobType);

            ComponentType[] filterChanged;
            int             processTypesCount;
            var             types = GetComponentTypes(jobType, iType, out processTypesCount, out filterChanged);

            system.GetEntityQueryInternal(types);
        }
Exemple #5
0
        public static EntityQuery GetEntityQueryForIJobForEach(this ComponentSystemBase system,
                                                               Type jobType)
        {
            var types = GetComponentTypes(jobType);

            if (types != null)
            {
                return(system.GetEntityQueryInternal(types));
            }
            else
            {
                return(default);
Exemple #6
0
        static unsafe void Initialize <T>(ComponentSystemBase system, EntityQuery entityQuery, Type jobType, Type wrapperJobType,
                                          bool isParallelFor, ref JobForEachCache cache, out ProcessIterationData iterator, ref T jobData)
            where T : struct
#if UNITY_DOTSPLAYER
        , IBaseJobForEach
#endif
        {
            // Get the job reflection data and cache it if we don't already have it cached.
            if (isParallelFor && cache.JobReflectionDataParallelFor == IntPtr.Zero ||
                !isParallelFor && cache.JobReflectionData == IntPtr.Zero)
            {
#if UNITY_DOTSPLAYER
                if (cache.Types == null)
                {
                    cache.Types = jobData.GetComponentTypes(out cache.ProcessTypesCount, out cache.FilterChanged);
                }
                var res = jobData.GetJobReflection(isParallelFor);
#else
                var iType = GetIJobForEachInterface(jobType);
                if (cache.Types == null)
                {
                    cache.Types = GetComponentTypes(jobType, iType, out cache.ProcessTypesCount,
                                                    out cache.FilterChanged);
                }

                var res = GetJobReflection(jobType, wrapperJobType, iType, isParallelFor);
#endif
                if (isParallelFor)
                {
                    cache.JobReflectionDataParallelFor = res;
                }
                else
                {
                    cache.JobReflectionData = res;
                }
            }

            // Update cached EntityQuery and ComponentSystem data.
            if (system != null)
            {
                if (cache.ComponentSystem != system)
                {
                    cache.EntityQuery = system.GetEntityQueryInternal(cache.Types);

                    // If the cached filter has changed, update the newly cached EntityQuery with those changes.
                    if (cache.FilterChanged.Length != 0)
                    {
                        cache.EntityQuery.SetChangedVersionFilter(cache.FilterChanged);
                    }

                    // Otherwise, just reset our newly cached EntityQuery's filter.
                    else
                    {
                        cache.EntityQuery.ResetFilter();
                    }

                    cache.ComponentSystem = system;
                }
            }
            else if (entityQuery != default)
            {
                if (cache.EntityQuery != entityQuery)
                {
                    // Cache the new EntityQuery and cache that our system is null.
                    cache.EntityQuery     = entityQuery;
                    cache.ComponentSystem = null;
                }
            }

            var query = cache.EntityQuery;

            iterator.IsReadOnly0 = iterator.IsReadOnly1 = iterator.IsReadOnly2 = iterator.IsReadOnly3 = iterator.IsReadOnly4 = iterator.IsReadOnly5 = 0;
            fixed(int *isReadOnly = &iterator.IsReadOnly0)
            {
                for (var i = 0; i != cache.ProcessTypesCount; i++)
                {
                    isReadOnly[i] = cache.Types[i].AccessModeType == ComponentType.AccessMode.ReadOnly ? 1 : 0;
                }
            }

            iterator.TypeIndex0 = iterator.TypeIndex1 = iterator.TypeIndex2 = iterator.TypeIndex3 = iterator.TypeIndex4 = iterator.TypeIndex5 = -1;
            fixed(int *typeIndices = &iterator.TypeIndex0)
            {
                for (var i = 0; i != cache.ProcessTypesCount; i++)
                {
                    typeIndices[i] = cache.Types[i].TypeIndex;
                }
            }

            iterator.m_IsParallelFor = isParallelFor;
            iterator.m_Length        = query.CalculateChunkCountWithoutFiltering();

            iterator.GlobalSystemVersion = query._GetImpl()->_Access->EntityComponentStore->GlobalSystemVersion;

#if ENABLE_UNITY_COLLECTIONS_CHECKS
            iterator.m_MaxIndex = iterator.m_Length - 1;
            iterator.m_MinIndex = 0;

            iterator.m_Safety0     = iterator.m_Safety1 = iterator.m_Safety2 = iterator.m_Safety3 = iterator.m_Safety4 = iterator.m_Safety5 =
                iterator.m_Safety6 = iterator.m_Safety7 = iterator.m_Safety8 = iterator.m_Safety9 = iterator.m_Safety10 = iterator.m_Safety11 = default(AtomicSafetyHandle);

            var bufferTypeCount = 0;
            iterator.m_SafetyReadOnlyCount = 0;
            fixed(AtomicSafetyHandle *safety = &iterator.m_Safety0)
            {
                for (var i = 0; i != cache.ProcessTypesCount; i++)
                {
                    if (cache.Types[i].AccessModeType == ComponentType.AccessMode.ReadOnly)
                    {
                        safety[iterator.m_SafetyReadOnlyCount] = query.GetSafetyHandle(query.GetIndexInEntityQuery(cache.Types[i].TypeIndex));
                        iterator.m_SafetyReadOnlyCount++;
                        if (cache.Types[i].IsBuffer)
                        {
                            safety[iterator.m_SafetyReadOnlyCount] = query.GetBufferSafetyHandle(query.GetIndexInEntityQuery(cache.Types[i].TypeIndex));
                            iterator.m_SafetyReadOnlyCount++;
                            bufferTypeCount++;
                        }
                    }
                }
            }

            iterator.m_SafetyReadWriteCount = 0;
            fixed(AtomicSafetyHandle *safety = &iterator.m_Safety0)
            {
                for (var i = 0; i != cache.ProcessTypesCount; i++)
                {
                    if (cache.Types[i].AccessModeType == ComponentType.AccessMode.ReadWrite)
                    {
                        safety[iterator.m_SafetyReadOnlyCount + iterator.m_SafetyReadWriteCount] = query.GetSafetyHandle(query.GetIndexInEntityQuery(cache.Types[i].TypeIndex));
                        iterator.m_SafetyReadWriteCount++;
                        if (cache.Types[i].IsBuffer)
                        {
                            safety[iterator.m_SafetyReadOnlyCount + iterator.m_SafetyReadWriteCount] = query.GetBufferSafetyHandle(query.GetIndexInEntityQuery(cache.Types[i].TypeIndex));
                            iterator.m_SafetyReadWriteCount++;
                            bufferTypeCount++;
                        }
                    }
                }
            }

            Assert.AreEqual(cache.ProcessTypesCount + bufferTypeCount, iterator.m_SafetyReadWriteCount + iterator.m_SafetyReadOnlyCount);
#endif
        }