using System; using Unity.Collections; using Unity.Collections.LowLevel.Unsafe; using Unity.Mathematics; namespace DanieleMarotta.RiversongCodeShowcase { public class NativeGrid : IDisposable where T : struct { private NativeArray _data; public NativeGrid(int2 size, Allocator allocator) { Size = size; _data = new NativeArray(Size.x * Size.y, allocator); } public int2 Size { get; } protected int GetIndex(int2 point) { return math.mad(point.y, Size.x, point.x); } public T GetValue(int index) { return _data[index]; } public T GetValue(int2 point) { return GetValue(GetIndex(point)); } public unsafe ref T GetValueRW(int index) { return ref UnsafeUtility.ArrayElementAsRef(_data.GetUnsafePtr(), index); } public ref T GetValueRW(int2 point) { return ref GetValueRW(GetIndex(point)); } public void SetValue(int index, T value) { _data[index] = value; } public void SetValue(int2 point, T value) { SetValue(GetIndex(point), value); } public NativeArray GetNativeArray() { return _data; } public void Dispose() { _data.Dispose(); } } }