This is just a testing setup and has a cell and grid class.
[System.Serializable]
public class Cell
{
public int row, column;
public Cell North, South, East, West;
[SerializeField]
public List<Cell> neighbors = new List<Cell>();
public Cell(int r , int c)
{
row = r;
column = c;
}
public Cell() { }
public void Link(Cell cell)
{
if (!neighbors.Contains(cell))
{
neighbors.Add(cell);
cell.neighbors.Add(this);
}
}
public void UnLink(Cell cell)
{
if (neighbors.Contains(cell))
{
neighbors.Remove(cell);
cell.neighbors.Remove(this);
}
}
public bool Links(Cell cell)
{
return neighbors.Contains(cell) ;
}
public List<Cell> Neighbors()
{
List<Cell> deffcell = new List<Cell>();
if(North.neighbors.Contains(this)) deffcell.Add(North);
if(South.neighbors.Contains(this)) deffcell.Add(South);
if(East.neighbors.Contains(this)) deffcell.Add(East);
if(West.neighbors.Contains(this)) deffcell.Add(West);
return deffcell;
}
}
public class MyGrid : MonoBehaviour
{
public int rows, columns;
public List<Cell> cells = new List<Cell>();
// Start is called before the first frame update
void Start()
{
PrepareMyGrid();
ConfigureCells();
}
// Update is called once per frame
void Update()
{
}
public void PrepareMyGrid()
{
for (int x = 0; x < rows; x++)
{
for (int y = 0; y < columns; y++)
{
cells.Add(new Cell(x,y));
}
}
}
public void ConfigureCells()
{
foreach (Cell c in cells)
{
int row = c.row, col = c.column;
c.North = new Cell(row - 1, col);
c.South = new Cell( row + 1, col);
c.East = new Cell(row,col+ 1);
c.West = new Cell(row, col - 1);
}
}
[CanBeNull]
public bool InBounds(int row, int column)
{
bool t = true;
if ((row < 0 || row > rows) || (column < 0 || column > columns)) t = false;
return t;
}
public Cell GetCell(int row, int colunm)
{
Cell cell = new Cell();
foreach (Cell c in cells)
{
if (c.row == row && c.column == colunm)
{
cell = c;
break;
}
}
return cell;
}
}