SHARE
TWEET

Untitled

a guest Apr 23rd, 2019 87 Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  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.             Comma
RAW Paste Data
We use cookies for various purposes including analytics. By continuing to use Pastebin, you agree to our use of cookies as described in the Cookies Policy. OK, I Understand
Not a member of Pastebin yet?
Sign Up, it unlocks many cool features!
 
Top