コード例 #1
0
        protected override void CreateSystems()
        {
            var managedCreateType         = typeof(ManagedComponentCreateSystem <>);
            var managedDestroyType        = typeof(ManagedComponentDestroySystem <>);
            var collectionCreateType      = typeof(CollectionComponentCreateSystem <>);
            var collectionDestroyType     = typeof(CollectionComponentDestroySystem <>);
            var managedSysStateTagType    = typeof(ManagedComponentSystemStateTag <>);
            var collectionSysStateTagType = typeof(CollectionComponentSystemStateTag <>);

            var typePairs = new NativeHashSet <AssociatedTypeSysStateTagTypePair>(128, Allocator.TempJob);

            var assemblies = AppDomain.CurrentDomain.GetAssemblies();

            foreach (var assembly in assemblies)
            {
                if (!BootstrapTools.IsAssemblyReferencingLatios(assembly))
                {
                    continue;
                }

                foreach (var type in assembly.GetTypes())
                {
                    if (type.GetCustomAttribute(typeof(DisableAutoTypeRegistration)) != null)
                    {
                        continue;
                    }

                    if (type == typeof(IManagedComponent) || type == typeof(ICollectionComponent))
                    {
                        continue;
                    }

                    if (typeof(IManagedComponent).IsAssignableFrom(type))
                    {
                        GetOrCreateAndAddSystem(managedCreateType.MakeGenericType(type));
                        GetOrCreateAndAddSystem(managedDestroyType.MakeGenericType(type));
                        typePairs.Add(new AssociatedTypeSysStateTagTypePair
                        {
                            sysStateTagType = ComponentType.ReadOnly(managedSysStateTagType.MakeGenericType(type)),
                            associatedType  = ComponentType.ReadOnly((Activator.CreateInstance(type) as IManagedComponent).AssociatedComponentType)
                        });
                    }
                    else if (typeof(ICollectionComponent).IsAssignableFrom(type))
                    {
                        GetOrCreateAndAddSystem(collectionCreateType.MakeGenericType(type));
                        GetOrCreateAndAddSystem(collectionDestroyType.MakeGenericType(type));
                        typePairs.Add(new AssociatedTypeSysStateTagTypePair
                        {
                            sysStateTagType = ComponentType.ReadOnly(collectionSysStateTagType.MakeGenericType(type)),
                            associatedType  = ComponentType.ReadOnly((Activator.CreateInstance(type) as ICollectionComponent).AssociatedComponentType)
                        });
                    }
                }
            }

            //Todo: Bug in Unity prevents iterating over NativeHashSet (value is defaulted).
            var typePairsArr = typePairs.ToNativeArray(Allocator.TempJob);

            EntityQueryDesc[] descs = new EntityQueryDesc[typePairsArr.Length * 2];
            int i = 0;

            foreach (var pair in typePairsArr)
            {
                descs[i] = new EntityQueryDesc
                {
                    All  = new ComponentType[] { pair.associatedType },
                    None = new ComponentType[] { pair.sysStateTagType }
                };
                i++;
                descs[i] = new EntityQueryDesc
                {
                    All  = new ComponentType[] { pair.sysStateTagType },
                    None = new ComponentType[] { pair.associatedType }
                };
                i++;
            }
            //Bug in Unity prevents constructing this EntityQuery because the scratch buffer is hardcoded to a size of 1024 which is not enough.
            //m_allSystemsQuery = GetEntityQuery(descs);
            typePairsArr.Dispose();
            typePairs.Dispose();
        }
コード例 #2
0
        protected override void CreateSystems()
        {
            var managedCreateType     = typeof(ManagedComponentCreateSystem <>);
            var managedDestroyType    = typeof(ManagedComponentDestroySystem <>);
            var collectionCreateType  = typeof(CollectionComponentCreateSystem <>);
            var collectionDestroyType = typeof(CollectionComponentDestroySystem <>);
            //var managedTagType         = typeof(ManagedComponentTag<>);
            var managedSysStateType = typeof(ManagedComponentSystemStateTag <>);
            //var collectionTagType      = typeof(CollectionComponentTag<>);
            var collectionSysStateType = typeof(CollectionComponentSystemStateTag <>);

            var tagTypes = new NativeList <ComponentType>(Allocator.TempJob);
            var sysTypes = new NativeHashMap <ComponentType, byte>(128, Allocator.TempJob);

            var assemblies = AppDomain.CurrentDomain.GetAssemblies();

            foreach (var assembly in assemblies)
            {
                if (!BootstrapTools.IsAssemblyReferencingLatios(assembly))
                {
                    continue;
                }

                foreach (var type in assembly.GetTypes())
                {
                    if (type.GetCustomAttribute(typeof(DisableAutoTypeRegistration)) != null)
                    {
                        continue;
                    }

                    if (type == typeof(IManagedComponent) || type == typeof(ICollectionComponent))
                    {
                        continue;
                    }

                    if (typeof(IManagedComponent).IsAssignableFrom(type))
                    {
                        GetOrCreateAndAddSystem(managedCreateType.MakeGenericType(type));
                        GetOrCreateAndAddSystem(managedDestroyType.MakeGenericType(type));
                        sysTypes.TryAdd(ComponentType.ReadOnly(managedSysStateType.MakeGenericType(type)), 0);
                        //tagTypes.Add(ComponentType.ReadOnly(managedTagType.MakeGenericType(type)));
                        tagTypes.Add(ComponentType.ReadOnly((Activator.CreateInstance(type) as IManagedComponent).AssociatedComponentType));
                    }
                    else if (typeof(ICollectionComponent).IsAssignableFrom(type))
                    {
                        GetOrCreateAndAddSystem(collectionCreateType.MakeGenericType(type));
                        GetOrCreateAndAddSystem(collectionDestroyType.MakeGenericType(type));
                        sysTypes.TryAdd(ComponentType.ReadOnly(collectionSysStateType.MakeGenericType(type)), 0);
                        //tagTypes.Add(ComponentType.ReadOnly(collectionTagType.MakeGenericType(type)));
                        tagTypes.Add(ComponentType.ReadOnly((Activator.CreateInstance(type) as ICollectionComponent).AssociatedComponentType));
                    }
                }
            }

            var tags = tagTypes.ToArray();
            var nss  = sysTypes.GetKeyArray(Allocator.Temp);
            var ss   = nss.ToArray();

            nss.Dispose();  //Todo: Is this necessary? I keep getting conflicting info from Unity on Allocator.Temp

            EntityQueryDesc descCreate = new EntityQueryDesc
            {
                Any  = tags,
                None = ss
            };

            m_anythingNeedsCreationQuery = GetEntityQuery(descCreate);
            EntityQueryDesc descDestroy = new EntityQueryDesc
            {
                Any  = ss,
                None = tags
            };

            m_anythingNeedsDestructionQuery = GetEntityQuery(descDestroy);

            tagTypes.Dispose();
            sysTypes.Dispose();
        }