61 lines
1.4 KiB
C#
61 lines
1.4 KiB
C#
using System;
|
|
using Unity.Collections;
|
|
using Unity.Mathematics;
|
|
using UnityEngine;
|
|
using Object = UnityEngine.Object;
|
|
|
|
namespace DanieleMarotta.RiversongCodeShowcase
|
|
{
|
|
public class TileHighlight : IDisposable
|
|
{
|
|
private NativeArray<Color32> _clear;
|
|
|
|
private NativeGrid<Color32> _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<Color32>(size.x * size.y, Allocator.Persistent);
|
|
_grid = new NativeGrid<Color32>(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<Color32>.Copy(_clear, dataArray);
|
|
}
|
|
|
|
public void AddTile(int2 tile, Color32 color)
|
|
{
|
|
_grid.SetValue(tile, color);
|
|
}
|
|
}
|
|
} |