Skip to content

Instantly share code, notes, and snippets.

@jeffvella
Created January 18, 2019 21:22
Show Gist options
  • Save jeffvella/80d541070b558468dc55116839bf76be to your computer and use it in GitHub Desktop.
Save jeffvella/80d541070b558468dc55116839bf76be to your computer and use it in GitHub Desktop.
Pattern for creating helper Unity Burst jobs
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