Created
January 18, 2019 21:22
-
-
Save jeffvella/80d541070b558468dc55116839bf76be to your computer and use it in GitHub Desktop.
Pattern for creating helper Unity Burst jobs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
using System; | |
using Providers.Grid; | |
using Unity.Burst; | |
using Unity.Collections; | |
using Unity.Collections.LowLevel.Unsafe; | |
using Unity.Jobs; | |
using Unity.Mathematics; | |
using UnityEngine; | |
public static class GridNodeOperations | |
{ | |
public static class AllNodes | |
{ | |
[BurstCompile] | |
public struct SetFlags : IJob | |
{ | |
[NativeDisableUnsafePtrRestriction] public IntPtr BaseAddress; | |
public ulong Flags; | |
public int Stride; | |
public int Length; | |
public unsafe void Execute() | |
{ | |
for (int i = 0; i < Length; i++) | |
{ | |
((GridNode*) (BaseAddress + i * Stride))->Flags = Flags; | |
} | |
} | |
public static unsafe JobHandle Schedule(NativeArray<GridNode> array, NodeFlags defaultFlags) | |
{ | |
return new SetFlags | |
{ | |
Flags = (ulong) defaultFlags, | |
BaseAddress = (IntPtr) array.GetUnsafePtr(), | |
Stride = UnsafeUtility.SizeOf<GridNode>(), | |
Length = array.Length | |
}.Schedule(); | |
} | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment