ScenePartition/Runtime/SceneGrid.cs

38 lines
921 B
C#
Raw Normal View History

using System;
using System.Collections.Generic;
using UnityEngine;
namespace VertexColor.ScenePartition.Editor
{
[Serializable]
public class SceneGrid
{
[SerializeField]
private int cellSize = 10;
[SerializeField]
private SceneGridDictionary grid = new SceneGridDictionary();
public void Insert(uint id, Vector2 point)
{
Vector2 gridPos = CalculateGridPosition(point);
if (grid.TryGetValue(gridPos, out List<uint> ids))
{
ids.Add(id);
}
else
{
grid.Add(gridPos, new List<uint> { id });
}
}
public Vector2 CalculateGridPosition(Vector2 point)
{
int x = Mathf.FloorToInt(point.x / cellSize);
int y = Mathf.FloorToInt(point.y / cellSize);
return new Vector2(x, y);
}
}
}