Since we cannot serialize multi-dimensional arrays (in Unity) we are limited to using one dimensional arrays along with math to convert from x,y pairs to an index and back out. Here are the current conversion algorithms:
int Width = 3; // Number of cells along the x axis
int Height = 3; // Number of cells along the y axis
GameObject[] Cells = new GameObject[ Width * Height ];
public int CalculateIndex( int x, int y )
{
return y * Width + x;
}
public void CalculateXY(int i, out x, out y)
{
x = i % Width;
y = i / Width;
}
Now, I have the problem where I need to create a 3 dimensional grid which introduces Depth as well as the z axis but I can't figure out how to inject the changes into the methods above... I'm having a mental block.
I found this post that explains some of it:
How to "flatten" or "index" 3D-array in 1D array?
However, my array is not in the HEIGHT, WIDTH, DEPTH (y,x,z) order but rather the WIDTH, HEIGHT, DEPTH (x,y,z) order. Furthermore I need the algorithm to turn it from an index back into x,y,z values.
Thanks, - jeff