Ejemplo n.º 1
0
        protected override JobHandle HandleMessage(JobHandle handle, EntityCommandBuffer commandBuffer)
        {
            var spawnJob = new SpawnJob {
                CommandBuffer = commandBuffer,
                Receivers     = Receivers,
            };

            return(spawnJob.Schedule(this, handle));
        }
Ejemplo n.º 2
0
        protected override JobHandle HandleMessage(JobHandle handle, EntityCommandBuffer commandBuffer)
        {
            var spawnJob = new SpawnJob {
                CommandBuffer = commandBuffer,
                Receivers     = Receivers,
                RandomNumber  = random.NextFloat(4f) - 1f,
            };

            return(spawnJob.Schedule(this, handle));
        }
Ejemplo n.º 3
0
    protected override JobHandle OnUpdate(JobHandle inputDeps)
    {
        //计算准备
        var concurrent = bufferSystem.CreateCommandBuffer().ToConcurrent();
        //给Job赋值
        var spawnJob = new SpawnJob();

        spawnJob.concurrent = concurrent;
        //挂载句柄
        var handle = spawnJob.Schedule(this, inputDeps);

        bufferSystem.AddJobHandleForProducer(handle);
        return(handle);
    }
Ejemplo n.º 4
0
    protected override JobHandle OnUpdate(JobHandle inputDeps)
    {
        SpawnJob spawningJob = new SpawnJob()
        {
            entityCommandBuffer = beginInitializationEntityCommandBuffer.CreateCommandBuffer().ToConcurrent(),
            destinations        = GetComponentDataFromEntity <DestinationData>(true),
            inputs    = GetComponentDataFromEntity <InputData>(true),
            deltaTime = Time.DeltaTime
        };
        JobHandle jobHandle = spawningJob.Schedule(this, inputDeps);

        beginInitializationEntityCommandBuffer.AddJobHandleForProducer(jobHandle);
        return(jobHandle);
    }
Ejemplo n.º 5
0
        protected override JobHandle OnUpdate(JobHandle pInputDeps)
        {
            SpawnJob _job = new SpawnJob(
                m_bufferSystem.CreateCommandBuffer().ToConcurrent(),
                new Random((uint)UnityEngine.Random.Range(0, int.MaxValue)),
                UnityEngine.Time.deltaTime
                );

            JobHandle _jobHandle = _job.Schedule(this, pInputDeps);

            m_bufferSystem.AddJobHandleForProducer(_jobHandle);

            return(_jobHandle);
        }
Ejemplo n.º 6
0
        protected override JobHandle OnUpdate(JobHandle inputDeps)
        {
            if (m_connectionGroup.IsEmptyIgnoreFilter)
            {
                // No connected players, just destroy all asteroids to save CPU
                inputDeps.Complete();
                World.EntityManager.DestroyEntity(asteroidGroup);
                return(default(JobHandle));
            }

            var settings = GetSingleton <ServerSettings>();

            if (m_Prefab == Entity.Null)
            {
                m_useStaticAsteroid = settings.staticAsteroidOptimization;
                var prefabs       = GetSingleton <GhostPrefabCollectionComponent>();
                var serverPrefabs = EntityManager.GetBuffer <GhostPrefabBuffer>(prefabs.serverPrefabs);
                for (int i = 0; i < serverPrefabs.Length; ++i)
                {
                    if (EntityManager.HasComponent <AsteroidTagComponentData>(serverPrefabs[i].Value) && (EntityManager.HasComponent <StaticAsteroid>(serverPrefabs[i].Value) == m_useStaticAsteroid))
                    {
                        m_Prefab = serverPrefabs[i].Value;
                    }
                }
                m_Radius = EntityManager.GetComponentData <CollisionSphereComponent>(m_Prefab).radius;
            }
            var maxAsteroids = settings.numAsteroids;

            JobHandle levelHandle;
            var       spawnJob = new SpawnJob
            {
                commandBuffer     = barrier.CreateCommandBuffer(),
                count             = asteroidGroup.CalculateEntityCountWithoutFiltering(),
                targetCount       = maxAsteroids,
                asteroidPrefab    = m_Prefab,
                asteroidRadius    = m_Radius,
                asteroidVelocity  = settings.asteroidVelocity,
                level             = m_LevelGroup.ToComponentDataArrayAsync <LevelComponent>(Allocator.TempJob, out levelHandle),
                rand              = new Unity.Mathematics.Random((uint)Stopwatch.GetTimestamp()),
                useStaticAsteroid = m_useStaticAsteroid,
                tick              = m_ServerSimulationSystemGroup.ServerTick
            };
            var handle = spawnJob.Schedule(JobHandle.CombineDependencies(inputDeps, levelHandle));

            barrier.AddJobHandleForProducer(handle);
            return(handle);
        }
Ejemplo n.º 7
0
        protected override JobHandle OnUpdate(JobHandle inputDeps)
        {
            var settings        = GetSingleton <SettingsComponent>();
            var singletonEntity = GetSingletonEntity <SettingsComponent>();

            if (_positionsQuery.CalculateEntityCount() == settings.Width * settings.Height)
            {
                return(inputDeps);
            }

            var helper = new ArrayHelper(settings.Width, settings.Height);

            var cachedEntities = new NativeArray <Entity>(settings.Width * settings.Height, Allocator.TempJob);
            var randomValues   = new NativeArray <int>(settings.Width * settings.Height, Allocator.TempJob);

            for (var i = 0; i < settings.Width * settings.Height; ++i)
            {
                randomValues[i] = Random.Range(0, settings.SetSize);
            }

            var cacheJob = new CacheJob
            {
                CachedEntities = cachedEntities,
                Entities       = _positionsQuery.ToEntityArray(Allocator.TempJob),
                Positions      = _positionsQuery.ToComponentDataArray <PositionComponent>(Allocator.TempJob),
                Helper         = helper
            };

            var spawnJob = new SpawnJob
            {
                CachedEntities  = cachedEntities,
                RandomValues    = randomValues,
                GemSet          = GetBufferFromEntity <GemSet>(true),
                CommandBuffer   = _commandBuffer.CreateCommandBuffer(),
                SingletonEntity = singletonEntity,
                Helper          = helper
            };

            var spawnHandler = cacheJob.Schedule(_positionsQuery.CalculateEntityCount(), 32, inputDeps);

            spawnHandler = spawnJob.Schedule(spawnHandler);

            _commandBuffer.AddJobHandleForProducer(spawnHandler);

            return(spawnHandler);
        }
Ejemplo n.º 8
0
    void Start()
    {
        var world         = World.DefaultGameObjectInjectionWorld;
        var entityManager = world.EntityManager;

        EntityCommandBuffer ecb = new EntityCommandBuffer(Allocator.TempJob);

        // Create a RenderMeshDescription using the convenience constructor
        // with named parameters.
        var desc = new RenderMeshDescription(
            Mesh,
            Material,
            shadowCastingMode: ShadowCastingMode.Off,
            receiveShadows: false);

        // Create empty base entity
        var prototype = entityManager.CreateEntity();

        // Call AddComponents to populate base entity with the components required
        // by Hybrid Renderer
        RenderMeshUtility.AddComponents(
            prototype,
            entityManager,
            desc);
        entityManager.AddComponentData(prototype, new LocalToWorld());

        // Spawn most of the entities in a Burst job by cloning a pre-created prototype entity,
        // which can be either a Prefab or an entity created at run time like in this sample.
        // This is the fastest and most efficient way to create entities at run time.
        var spawnJob = new SpawnJob
        {
            Prototype   = prototype,
            Ecb         = ecb.AsParallelWriter(),
            EntityCount = EntityCount,
        };

        var spawnHandle = spawnJob.Schedule(EntityCount, 128);

        spawnHandle.Complete();

        ecb.Playback(entityManager);
        ecb.Dispose();
        entityManager.DestroyEntity(prototype);
    }
Ejemplo n.º 9
0
    // Update is called once per frame
    protected override JobHandle OnUpdate(JobHandle inputDeps)
    {
        var spawnJob = new SpawnJob
        {
            dt         = Time.deltaTime,
            fortresses = spawnings.fortresses,
            spawners   = spawnings.spawners
        };
        var overloadJob = new OverloadJob
        {
            dt         = Time.deltaTime,
            fortresses = overloadings.fortresses,
            overloads  = overloadings.overloads
        };
        var spawnFence    = spawnJob.Schedule(spawnings.Length, SimulationState.TinyBatchSize, inputDeps);
        var overloadFence = overloadJob.Schedule(overloadings.Length, SimulationState.TinyBatchSize, spawnFence);

        return(JobHandle.CombineDependencies(spawnFence, overloadFence));
    }
Ejemplo n.º 10
0
    protected override JobHandle OnUpdate(JobHandle inputDeps)
    {
        NativeArray<Entity> enemiesArray = new NativeArray<Entity>(0, Allocator.TempJob);

        if (Time.time - timer > cooldownTime)
        {
            EntityQuery enemyQuery = GetEntityQuery(ComponentType.ReadOnly<Enemy>(), ComponentType.ReadOnly<Disabled>());

            NativeArray<Entity> tempArray = enemyQuery.ToEntityArray(Allocator.TempJob);




            NativeSlice<Entity> nativeSlice = tempArray.Slice(0, math.min(enemyQuery.CalculateLength(), multiplier * 2));


            enemiesArray.Dispose();

            enemiesArray = new NativeArray<Entity>(nativeSlice.Length, Allocator.TempJob);

            nativeSlice.CopyTo(enemiesArray);
            tempArray.Dispose();

            timer = Time.time;
            multiplier++;
        }



        SpawnJob spawnJob = new SpawnJob
        {
            commandBuffer = endSimulationEntityCommandBufferSystem.CreateCommandBuffer().ToConcurrent(),
            enemiesArray = enemiesArray
        };

        JobHandle jobHandle = spawnJob.Schedule(inputDeps);
        endSimulationEntityCommandBufferSystem.AddJobHandleForProducer(jobHandle);
        jobHandle.Complete();

        enemiesArray.Dispose();
        return jobHandle;
    }
Ejemplo n.º 11
0
    protected override JobHandle OnUpdate(JobHandle inputDeps)
    {
        //取代直接执行结构的改变,一个任务可以添加一个命令到EntityCommandBuffer(实体命令缓存),从而在主线程上完成其任务后执行这些改变
        //命令缓存允许在工作线程上执行任何潜在消耗大的计算,同时把实际的增删排到之后把将要添加实例化命令到EntityCommandBuffer的任务加入计划
        var job = new SpawnJob
        {
            commandBuffer = m_BeginInitializationEntityCommandBufferSystem.CreateCommandBuffer().ToConcurrent(),
            delatime      = Time.deltaTime,
            position      = UnityEngine.Random.insideUnitSphere * 50
        };
        JobHandle jobHandle = job.Schedule(this, inputDeps);

        //Debug.Log("场景实体个数===" + EntityManager.EntityCapacity);   错误
        ///生成任务并行且没有同步机会直到阻塞系统执行
        ///当阻塞系统执行时,我们想完成生成任务,然后再执行那些命令(创建实体并放置到指定位置)
        /// 我们需要告诉阻塞系统哪个任务需要在它能回放命令之前完成
        m_BeginInitializationEntityCommandBufferSystem.AddJobHandleForProducer(jobHandle);

        return(jobHandle);
    }
Ejemplo n.º 12
0
        protected override JobHandle OnUpdate(JobHandle inputDeps)
        {
            if (m_connectionGroup.IsEmptyIgnoreFilter)
            {
                // No connected players, just destroy all asteroids to save CPU
                inputDeps.Complete();
                World.GetExistingManager <EntityManager>().DestroyEntity(asteroidGroup);
                return(default(JobHandle));
            }
            var settings     = GetSingleton <ServerSettings>();
            var maxAsteroids = settings.numAsteroids;

            JobHandle gatherJob;
            var       countJob = new CountJob
            {
                chunks     = asteroidGroup.CreateArchetypeChunkArray(Allocator.TempJob, out gatherJob),
                count      = count,
                entityType = GetArchetypeChunkEntityType()
            };

            inputDeps = countJob.Schedule(JobHandle.CombineDependencies(inputDeps, gatherJob));

            JobHandle levelHandle;
            var       spawnJob = new SpawnJob
            {
                commandBuffer     = barrier.CreateCommandBuffer(),
                count             = count,
                targetCount       = maxAsteroids,
                asteroidArchetype = settings.asteroidArchetype,
                asteroidRadius    = settings.asteroidRadius,
                asteroidVelocity  = settings.asteroidVelocity,
                level             = m_LevelGroup.ToComponentDataArray <LevelComponent>(Allocator.TempJob, out levelHandle),
                rand = new Unity.Mathematics.Random((uint)Stopwatch.GetTimestamp())
            };
            var handle = spawnJob.Schedule(JobHandle.CombineDependencies(inputDeps, levelHandle));

            barrier.AddJobHandleForProducer(handle);
            return(handle);
        }
Ejemplo n.º 13
0
        protected override JobHandle OnUpdate(JobHandle inputDeps)
        {
            if (m_connectionGroup.IsEmptyIgnoreFilter)
            {
                // No connected players, just destroy all asteroids to save CPU
                inputDeps.Complete();
                World.EntityManager.DestroyEntity(asteroidGroup);
                return(default(JobHandle));
            }

            if (m_Prefab == Entity.Null)
            {
                var prefabs       = GetSingleton <GhostPrefabCollectionComponent>();
                var serverPrefabs = EntityManager.GetBuffer <GhostPrefabBuffer>(prefabs.serverPrefabs);
                m_Prefab = serverPrefabs[AsteroidsGhostSerializerCollection.FindGhostType <AsteroidSnapshotData>()].Value;
                m_Radius = EntityManager.GetComponentData <CollisionSphereComponent>(m_Prefab).radius;
            }
            var settings     = GetSingleton <ServerSettings>();
            var maxAsteroids = settings.numAsteroids;

            JobHandle levelHandle;
            var       spawnJob = new SpawnJob
            {
                commandBuffer    = barrier.CreateCommandBuffer(),
                count            = asteroidGroup.CalculateEntityCountWithoutFiltering(),
                targetCount      = maxAsteroids,
                asteroidPrefab   = m_Prefab,
                asteroidRadius   = m_Radius,
                asteroidVelocity = settings.asteroidVelocity,
                level            = m_LevelGroup.ToComponentDataArray <LevelComponent>(Allocator.TempJob, out levelHandle),
                rand             = new Unity.Mathematics.Random((uint)Stopwatch.GetTimestamp())
            };
            var handle = spawnJob.Schedule(JobHandle.CombineDependencies(inputDeps, levelHandle));

            barrier.AddJobHandleForProducer(handle);
            return(handle);
        }
Ejemplo n.º 14
0
    // Start is called before the first frame update
    void Start()
    {
        var world         = World.DefaultGameObjectInjectionWorld;
        var entityManager = world.EntityManager;

        EntityCommandBuffer ecbJob        = new EntityCommandBuffer(Allocator.TempJob);
        EntityCommandBuffer ecbMainThread = new EntityCommandBuffer(Allocator.Temp);

        var desc = new RenderMeshDescription(
            Mesh,
            Material,
            shadowCastingMode: ShadowCastingMode.Off,
            receiveShadows: false);

        var prototype = entityManager.CreateEntity();

        RenderMeshUtility.AddComponents(
            prototype,
            entityManager,
            desc);
        entityManager.AddComponentData(prototype, new MaterialColor());

        // Spawn most of the entities in a Burst job by cloning a pre-created prototype entity,
        // which can be either a Prefab or an entity created at run time like in this sample.
        // This is the fastest and most efficient way to create entities at run time.
        var spawnJob = new SpawnJob
        {
            Prototype   = prototype,
            Ecb         = ecbJob.AsParallelWriter(),
            EntityCount = EntityCount,
            ObjectScale = ObjectScale,
            Radius      = Radius,
            Twists      = Twists,
        };

        int numJobEntities = EntityCount - MainThreadEntityCount;
        var spawnHandle    = spawnJob.Schedule(numJobEntities, 128);

        // Spawn a small portion in the main thread to test that the ECB API works.
        // This is NOT the recommended way, this simply tests that this API works.
        for (int i = 0; i < MainThreadEntityCount; ++i)
        {
            int index = i + numJobEntities;
            var e     = ecbMainThread.CreateEntity();
            RenderMeshUtility.AddComponents(
                e,
                ecbMainThread,
                desc);
            ecbMainThread.SetComponent(e, new LocalToWorld {
                Value = spawnJob.ComputeTransform(index)
            });
            // Use AddComponent because we didn't clone the prototype here
            ecbMainThread.AddComponent(e, new MaterialColor {
                Value = spawnJob.ComputeColor(index)
            });
        }

        spawnHandle.Complete();

        ecbJob.Playback(entityManager);
        ecbJob.Dispose();
        ecbMainThread.Playback(entityManager);
        entityManager.DestroyEntity(prototype);
    }