Advertisement
Guest User

Untitled

a guest
Apr 23rd, 2019
93
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 3.08 KB | None | 0 0
  1. using Unity.Burst;
  2. using Unity.Collections;
  3. using Unity.Entities;
  4. using Unity.Jobs;
  5. using Unity.Mathematics;
  6. using static Unity.Mathematics.math;
  7. using Unity.Transforms;
  8.  
  9. // JobComponentSystems can run on worker threads.
  10. // However, creating and removing Entities can only be done on the main thread to prevent race conditions.
  11. // The system uses an EntityCommandBuffer to defer tasks that can't be done inside the Job.
  12. public class SpawnCabotsSystem : JobComponentSystem
  13. {
  14.     // EndSimulationBarrier is used to create a command buffer which will then be played back when that barrier system executes.
  15.     EndSimulationEntityCommandBufferSystem m_EntityCommandBufferSystem;
  16.     Random rng;
  17.  
  18.     protected override void OnCreateManager()
  19.     {
  20.         // Cache the EndSimulationBarrier in a field, so we don't have to create it every frame
  21.         m_EntityCommandBufferSystem = World.GetOrCreateSystem<EndSimulationEntityCommandBufferSystem>();
  22.         rng = new Random(1);
  23.     }
  24.  
  25.     struct SpawnJob : IJobForEachWithEntity<CabotsSpawner>
  26.     {
  27.         public EntityCommandBuffer CommandBuffer;
  28.         public Random rng;
  29.  
  30.         public void Execute(Entity spawner_entity, int index, [ReadOnly] ref CabotsSpawner spawner)
  31.         {
  32.             for (int i = 0; i < spawner.Count; i++)
  33.             {
  34.                 var ent = CommandBuffer.CreateEntity();
  35.                 var props = normalize(rng.NextFloat4()) * 2 - 1;
  36.                 CommandBuffer.AddComponent(
  37.                     ent,
  38.                     new CabotPropertiesComponent
  39.                     {
  40.                         Value = props
  41.                     }
  42.                 );
  43.                 CommandBuffer.AddBuffer<InventoryGoodBE>(ent);
  44.                 CommandBuffer.AddComponent(ent, new BasicNeeds { power = 0.1f });
  45.                 CommandBuffer.AddBuffer<TaxGoodBE>(ent);
  46.             }
  47.             CommandBuffer.DestroyEntity(spawner_entity);
  48.         }
  49.     }
  50.  
  51.     protected override JobHandle OnUpdate(JobHandle inputDeps)
  52.     {
  53.         //Instead of performing structural changes directly, a Job can add a command to an EntityCommandBuffer to perform such changes on the main thread after the Job has finished.
  54.         //Command buffers allow you to perform any, potentially costly, calculations on a worker thread, while queuing up the actual insertions and deletions for later.
  55.  
  56.         // Schedule the job that will add Instantiate commands to the EntityCommandBuffer.
  57.         var job = new SpawnJob
  58.         {
  59.             CommandBuffer = m_EntityCommandBufferSystem.CreateCommandBuffer(),
  60.             rng = rng
  61.         }.ScheduleSingle(this, inputDeps);
  62.  
  63.  
  64.         // SpawnJob runs in parallel with no sync point until the barrier system executes.
  65.         // When the barrier system executes we want to complete the SpawnJob and then play back the commands (Creating the entities and placing them).
  66.         // We need to tell the barrier system which job it needs to complete before it can play back the commands.
  67.         m_EntityCommandBufferSystem.AddJobHandleForProducer(job);
  68.  
  69.         return job;
  70.     }
  71. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement