Advertisement
Guest User

Untitled

a guest
Apr 23rd, 2019
95
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 2.97 KB | None | 0 0
  1. using Unity.Collections;
  2. using Unity.Entities;
  3. using Unity.Jobs;
  4. using Unity.Mathematics;
  5. using Unity.Transforms;
  6. using UnityEngine;
  7. using Random = Unity.Mathematics.Random;
  8.  
  9. [UpdateInGroup(typeof(SimulationSystemGroup))]
  10. public class ShipSpawnerSys : JobComponentSystem
  11. {
  12.     private const int MaxShips = 20000;
  13.     private const float SpawnRatePerSecond = 1000;
  14.  
  15.     private Random rand;
  16.     private int shipCount;
  17.     private BeginInitializationEntityCommandBufferSystem cmdBufferSystem;
  18.  
  19.     protected override void OnCreate()
  20.     {
  21.         rand = new Random(1);
  22.         shipCount = 0;
  23.         cmdBufferSystem = World.GetOrCreateSystem<BeginInitializationEntityCommandBufferSystem>();
  24.     }
  25.  
  26.     private struct SpawnJob : IJobForEachWithEntity<ShipSpawner, LocalToWorld, Rotation>
  27.     {
  28.         public EntityCommandBuffer CommandBuffer;
  29.         //public uint RandSeedOffset;
  30.         public Random Rand;
  31.         public float Time;
  32.         public float Dt;
  33.         public int SpawnCount;
  34.  
  35.         public void Execute(Entity entity, int index, [ReadOnly] ref ShipSpawner spawner, [ReadOnly] ref LocalToWorld location, [ReadOnly] ref Rotation rotation)
  36.         {
  37.             //Random rand = new Random((uint)((index + RandSeedOffset) % uint.MaxValue));
  38.             for (int i = 0; i < SpawnCount; i++)
  39.             {
  40.                 Entity ship = CommandBuffer.Instantiate(spawner.Prefab);
  41.                 float3 pos = math.transform(location.Value, new float3(Rand.NextFloat(-500f, 500f), 0, Rand.NextFloat(-20f, 20f)));
  42.                 CommandBuffer.SetComponent(ship, new Rotation { Value = rotation.Value });
  43.                 CommandBuffer.AddComponent(ship, new Velocity());
  44.                 CommandBuffer.SetComponent(ship, new Translation { Value = pos });
  45.                 CommandBuffer.AddComponent(ship, new SpawnTime { Value = Time });
  46.             }
  47.         }
  48.     }
  49.  
  50.     protected override JobHandle OnUpdate(JobHandle inputDeps)
  51.     {
  52.         int maxToSpawn = MaxShips - shipCount;
  53.         int spawnCount = math.min((int)(SpawnRatePerSecond * Time.deltaTime), maxToSpawn);
  54.         shipCount += spawnCount;
  55.  
  56.         // Schedule the job that will add Instantiate commands to the EntityCommandBuffer.
  57.         var job = new SpawnJob
  58.         {
  59.             CommandBuffer = cmdBufferSystem.CreateCommandBuffer(),
  60.             //RandSeedOffset = rand.NextUInt(),
  61.             Rand = rand,
  62.             Time = Time.time,
  63.             Dt = Time.deltaTime,
  64.             SpawnCount = spawnCount,
  65.         }.ScheduleSingle(this, inputDeps);
  66.  
  67.         // SpawnJob runs in parallel with no sync point until the barrier system executes.
  68.         // When the barrier system executes we want to complete the SpawnJob and then play back the commands (Creating the entities and placing them).
  69.         // We need to tell the barrier system which job it needs to complete before it can play back the commands.
  70.         cmdBufferSystem.AddJobHandleForProducer(job);
  71.         return job;
  72.     }
  73. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement