I want to try some path finding algorithm, I use tilemap to generate the path nodes, now I need to link each node as edge.If I can make the gameobjects as 2D-array it would be much easier.
as I know, In gameobject brush in tilemap, the tilemap only care about instantiate gameobject in the grid and no other relationship information between grids and gameobects.
Now the only way I can know my gameobjects’ relative coordinate information is to raycast scan one by one over whole tilemap. Is there any way in tilemap can do this more convenience?
you could create an array of arrays. this way you can use the two indexes to represent the two x and y coordinates to get the object or info you are looking for.
here is a quick example of what im talking about:
GameObject[][] tiles;
void Start () {
// make a 20 by 20 grid of objects
tiles = new GameObject[20][];
int x = 20;
while(x>0){x--;
tiles[x]= new GameObject[20];
int y = 20;
while(y>0){y--;
tiles[x][y] = GameObject.CreatePrimitive(PrimitiveType.Cube);
tiles[x][y].transform.position = new Vector3(x,y,0);
}
}
//pick object at coordinates like this:
tiles[3][7].transform.renderer.material.color = Color.blue;
}
if you are using this for a tile based pathfinding you shouldnt really need to assign node connections the algo should be able to go up and down and left and right freely through the coordinates. you would just need to mark coordinates as “not walkable” so the algo can avoid it when figuring the path.
in your case you would pry want to replace the simple gameObject array with your own custom class to hold your needed info about the squares for your pathfinding.