443eb9

Untitled

Sep 25th, 2023
110
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 2.50 KB | None | 0 0
  1. using System;
  2. using Unity.Collections;
  3. using Unity.Entities;
  4. using Unity.Jobs;
  5. using Unity.Physics;
  6.  
  7. namespace HexagonSurvive.HexPhysics
  8. {
  9.     [UpdateInGroup(typeof(InitializationSystemGroup))]
  10.     public partial class BatchRaycastSystem : SystemBase
  11.     {
  12.         private NativeArray<RaycastTaskData> _data;
  13.  
  14.         protected override void OnCreate()
  15.         {
  16.             RequireForUpdate<PhysicsWorldSingleton>();
  17.             _data = new NativeArray<RaycastTaskData>(16, Allocator.Persistent);
  18.  
  19.             for (int i = 0; i < _data.Length; i++)
  20.                 _data[i] = new RaycastTaskData { isDisposed = true };
  21.         }
  22.  
  23.         protected override void OnUpdate()
  24.         {
  25.             Dependency.Complete();
  26.            
  27.             Dependency = new RaycastJob
  28.             {
  29.                 collisionWorld = SystemAPI.GetSingleton<PhysicsWorldSingleton>().CollisionWorld,
  30.                 inputs = _data
  31.             }.Schedule(_data.Length, Dependency);
  32.         }
  33.  
  34.         protected override void OnDestroy()
  35.         {
  36.             _data.Dispose();
  37.         }
  38.  
  39.         /// <summary>
  40.         /// Schedule a raycast task and you can get that result next frame.
  41.         /// </summary>
  42.         /// <param name="input">Input data</param>
  43.         /// <returns>Id of the task.</returns>
  44.         /// <exception cref="Exception">Thrown if the buffer is full.</exception>
  45.         public int ScheduleRaycastTask(RaycastInput input)
  46.         {
  47.             Dependency.Complete();
  48.  
  49.             for (int i = 0; i < _data.Length; i++)
  50.             {
  51.                 if (_data[i].isDisposed)
  52.                 {
  53.                     _data[i] = new RaycastTaskData { input = input };
  54.                     return i;
  55.                 }
  56.             }
  57.  
  58.             throw new Exception("Raycast task buffer is too small! Expand it!");
  59.         }
  60.  
  61.         /// <summary>
  62.         /// Get the hit result of the task by id.
  63.         /// </summary>
  64.         /// <param name="id">Task id</param>
  65.         /// <param name="hit">The hit result</param>
  66.         /// <returns>If the task is done and the return value is valid.</returns>
  67.         public bool GetRaycastTaskResult(int id, out RaycastHit hit)
  68.         {
  69.             Dependency.Complete();
  70.  
  71.             if (_data[id].isDisposed)
  72.             {
  73.                 hit = default;
  74.                 return false;
  75.             }
  76.  
  77.             hit = _data[id].hit;
  78.             _data[id] = new RaycastTaskData { isDisposed = true };
  79.             return true;
  80.         }
  81.     }
  82. }
Advertisement
Add Comment
Please, Sign In to add comment