using System; using Unity.Collections; using Unity.Mathematics; using UnityEngine; using Object = UnityEngine.Object; namespace DanieleMarotta.RiversongCodeShowcase { public class TileHighlight : IDisposable { private NativeArray _clear; private NativeGrid _grid; public Texture2D Texture { get; private set; } public void Create(int2 size) { Texture = new Texture2D(size.x, size.y, TextureFormat.RGBA32, false); _clear = new NativeArray(size.x * size.y, Allocator.Persistent); _grid = new NativeGrid(size, Allocator.Persistent); } public void Dispose() { if (Texture) { Object.Destroy(Texture); Texture = null; } if (_clear.IsCreated) { _clear.Dispose(); _clear = default; } if (_grid != null) { _grid.Dispose(); _grid = null; } } public void Apply() { var dataArray = _grid.GetNativeArray(); Texture.SetPixelData(dataArray, 0); Texture.Apply(); NativeArray.Copy(_clear, dataArray); } public void AddTile(int2 tile, Color32 color) { _grid.SetValue(tile, color); } } }