コード例 #1
0
        private static void AddSystemToRootLevelSystemGroupsInternal(World world, IEnumerable <Type> systemTypesOrig, int managedTypesCountOrig)
        {
            var initializationSystemGroup = world.GetOrCreateSystem <InitializationSystemGroup>();
            var simulationSystemGroup     = world.GetOrCreateSystem <SimulationSystemGroup>();
            var presentationSystemGroup   = world.GetOrCreateSystem <PresentationSystemGroup>();

            var managedTypes   = new List <Type>();
            var unmanagedTypes = new List <Type>();

            foreach (var stype in systemTypesOrig)
            {
                if (typeof(ComponentSystemBase).IsAssignableFrom(stype))
                {
                    managedTypes.Add(stype);
                }
                else if (typeof(ISystemBase).IsAssignableFrom(stype))
                {
                    unmanagedTypes.Add(stype);
                }
                else
                {
                    throw new InvalidOperationException("Bad type");
                }
            }

            var systems = world.GetOrCreateSystemsAndLogException(managedTypes, managedTypes.Count);

            // Add systems to their groups, based on the [UpdateInGroup] attribute.
            foreach (var system in systems)
            {
                if (system == null)
                {
                    continue;
                }

                // Skip the built-in root-level system groups
                var type = system.GetType();
                if (type == typeof(InitializationSystemGroup) ||
                    type == typeof(SimulationSystemGroup) ||
                    type == typeof(PresentationSystemGroup))
                {
                    continue;
                }

                var updateInGroupAttributes = TypeManager.GetSystemAttributes(system.GetType(), typeof(UpdateInGroupAttribute));
                if (updateInGroupAttributes.Length == 0)
                {
                    simulationSystemGroup.AddSystemToUpdateList(system);
                }

                foreach (var attr in updateInGroupAttributes)
                {
                    var group = FindGroup(world, type, attr);
                    if (group != null)
                    {
                        group.AddSystemToUpdateList(system);
                    }
                }
            }

#if !UNITY_DOTSRUNTIME
            // Add unmanaged systems
            foreach (var type in unmanagedTypes)
            {
                SystemHandleUntyped sysHandle = world.Unmanaged.CreateUnmanagedSystem(world, type);

                // Add systems to their groups, based on the [UpdateInGroup] attribute.

                var updateInGroupAttributes = TypeManager.GetSystemAttributes(type, typeof(UpdateInGroupAttribute));
                if (updateInGroupAttributes.Length == 0)
                {
                    simulationSystemGroup.AddUnmanagedSystemToUpdateList(sysHandle);
                }

                foreach (var attr in updateInGroupAttributes)
                {
                    ComponentSystemGroup groupSys = FindGroup(world, type, attr);

                    if (groupSys != null)
                    {
                        groupSys.AddUnmanagedSystemToUpdateList(sysHandle);
                    }
                }
            }
#endif


            // Update player loop
            initializationSystemGroup.SortSystems();
            simulationSystemGroup.SortSystems();
            presentationSystemGroup.SortSystems();
        }
コード例 #2
0
        /// <summary>
        /// Adds the collection of systems to the world by injecting them into the root level system groups
        /// (InitializationSystemGroup, SimulationSystemGroup and PresentationSystemGroup)
        /// </summary>
        public static void AddSystemsToRootLevelSystemGroups(World world, params Type[] systemTypes)
        {
            var initializationSystemGroup = world.GetOrCreateSystem <InitializationSystemGroup>();
            var simulationSystemGroup     = world.GetOrCreateSystem <SimulationSystemGroup>();
            var presentationSystemGroup   = world.GetOrCreateSystem <PresentationSystemGroup>();

            var systems = world.GetOrCreateSystemsAndLogException(systemTypes.ToArray());

            // Add systems to their groups, based on the [UpdateInGroup] attribute.
            foreach (var system in systems)
            {
                if (system == null)
                {
                    continue;
                }

                // Skip the built-in root-level system groups
                var type = system.GetType();
                if (type == typeof(InitializationSystemGroup) ||
                    type == typeof(SimulationSystemGroup) ||
                    type == typeof(PresentationSystemGroup))
                {
                    continue;
                }

                var groups = TypeManager.GetSystemAttributes(system.GetType(), typeof(UpdateInGroupAttribute));
                if (groups.Length == 0)
                {
                    simulationSystemGroup.AddSystemToUpdateList(system);
                }

                foreach (var g in groups)
                {
                    var group = g as UpdateInGroupAttribute;
                    if (group == null)
                    {
                        continue;
                    }

                    if (!TypeManager.IsSystemAGroup(group.GroupType))
                    {
                        Debug.LogError($"Invalid [UpdateInGroup] attribute for {type}: {group.GroupType} must be derived from ComponentSystemGroup.");
                        continue;
                    }

                    var groupMgr = world.GetExistingSystem(group.GroupType);
                    if (groupMgr == null)
                    {
                        // Warn against unexpected behaviour combining DisableAutoCreation and UpdateInGroup
                        var parentDisableAutoCreation = TypeManager.GetSystemAttributes(group.GroupType, typeof(DisableAutoCreationAttribute)).Length > 0;
                        if (parentDisableAutoCreation)
                        {
                            Debug.LogWarning($"A system {type} wants to execute in {group.GroupType} but this group has [DisableAutoCreation] and {type} does not. The system will not be added to any group and thus not update.");
                        }
                        else
                        {
                            Debug.LogWarning(
                                $"A system {type} could not be added to group {group.GroupType}, because the group was not created. Fix these errors before continuing. The system will not be added to any group and thus not update.");
                        }
                        continue;
                    }

                    var groupSys = groupMgr as ComponentSystemGroup;
                    if (groupSys != null)
                    {
                        groupSys.AddSystemToUpdateList(system);
                    }
                }
            }

            // Update player loop
            initializationSystemGroup.SortSystems();
            simulationSystemGroup.SortSystems();
            presentationSystemGroup.SortSystems();
        }