I’m working with a function that came from a tutorial on tilemaps. The original author wrote it for a single row example, so it always worked. However, I expanded my tiles out to multiple rows, and this function promptly broke as it assumes 0,0 is at the bottom-left of the tiles and working up with a ++. What can I do to fix this behavior so that it swaps properly and gets the tiles starting at the top?
Color[][] ChopUpTiles()
{
int numTilesPerRow = terrainTiles.width / tileResolution;
int numRows = terrainTiles.height / tileResolution;
Color[][] tiles = new Color[numTilesPerRow * numRows][];
for (int y = 0; y < numRows; y++)
{
for (int x = 0; x < numTilesPerRow; x++)
{
tiles[y * numTilesPerRow + x] = terrainTiles.GetPixels(x * tileResolution, y * tileResolution, tileResolution, tileResolution);
}
}
return tiles;
}
The easiest thing to do probably would be to swap the tiles on the file, as it is currently a fixed size, but that may not always be the case, so I’d like to try to fix it in code as well.
I cannot find any mistake in your code.
I also don’t see why it would start at the bottm left corner… If I do my math correctly it starts at the top left corner (in texture space).
Not sure what you mean exactly with “swap the tiles”…
Please describe a little bit more what’s exactly going wrong (if there is an exception: please copy paste it).
However, if you want to have different tile sizes (if this is what you’d “like to fix (…) as well”) you need to save every tile individually with position and size.
I am not sure why you need all the pixel colors of a tile… yould you describe the purpose for this? If it is only to display them it is the wrong way (you should modify the uv coordinates in that case)…
Apparently it has to do with the coordinate system Unity3D uses, which switches to the bottom left when dealing with Texture2D - don’t ask me why, although it’s easily provable by testing it. It’s also recorded elsewhere as well.