In fact, i use prefab tiles. so i hope to get the script on prefab/gameobject by Tilemap. i see the function GetTileData, but i dont know how to get ITilemap.Or you have better idea about this?thank you very much if can give me any help
Hello,
did you ever find out how to get ITilemap …? …
I googled but couldn’t find anything…
ITilemap is passed in from the Tilemap component as part of the Tilemap system. If you need to access the Tilemap component, you can use ITilemap.GetComponent<Tilemap>()
.
I looked everywhere, but I still cannot find how to get the itilemap.
GetTileData demands this, I have the actual tilemap, the tile, everything. I just do know how I can get an ITilemap.
Can anyone show a code where a variable gets assigned an ITilemap so we can see how to get it?
I am assuming this is incridiable simple, and I am just being dumb as a door because obvious very few people had problems with it.
AFAIK GetTileData is for Tilemap system / Unity to use…
Docs for TileBase.GetTileData say*:*
“Retrieves any tile rendering data from the scripted tile.”
AFAIK it is not intended for you to use to get tiles from a Tilemap.
So the question is, what are you trying to do?
If you are trying to get tiles of your tilemap, you can do it with this:
myTilemap.GetTile(position)
…unless you really want to get ITilemap for some reason.
Classic Unity nonsense. So you want to edit a tilemap in the Editor and then record that at runtime. Well obviously you iterate through the tiles and record what they are. We are given TileMap.cellBounds to do this. Great, cycle through the bounds of the tilemap, getting the tile data to see which tile is in which cell.
Okay, simple enough. Let’s get that cell data.
BoundsInt bounds = tm_floor.cellBounds;
for (int x = bounds.min.x; x < bounds.max.x; x++) {
for (int y = bounds.min.y; y < bounds.max.y; y++) {
//get the tile on the position
var temp = tm_floor.GetTile(new Vector3Int(x, y, 0));
}
}
Great! What does ‘temp’ contain? Oh, nothing useful. I want access to the tiles details like colour and sprite. I need the TileData.
TileData = temp.GetTileData(…, where the hell do I get the iTileMap to be able to retrieve the tile’s data? Let me spend a morning trying to make sense of Unity’s documentation.
Nope, no joy there. I know, I’ll expose it in the editor and see what I can drag to fit there. Oh, it doesn’t get serialised. What does Google say about retrieving the data from an Editor tilemap? It doesn’t. What advice has the forums got? Classic cyclical non-answers pointing to documentation that doesn’t explain anything.
Two minute job takes a day or three! I was going to use the editor to save time creating a procedural level generator, but it’s actually easier to create a procgen level then to read and record the details of a tilemap in the editor! Simply because the mechanism to read the data is obfuscated in poor documentation. It’d be better if things weren’t mentioned than were mentioned as indicative of a solution that’s actually not useable. Then at least I wouldn’t be wasting time trying to find access to a system I can’t access and would just get on with the other known solution.
Well I did have similar feelings when I started using tilemaps - what exactly do you need from a tile?
Note that I think by default tilemap gives you a Tilebase, not a Tile.
I’ve done it like this, IIRC.
Cast the Tilebase into a Tile, and read the values you need.
Tile itself pretty much has those same values as the TileData struct.
// get cells
BoundsInt bounds = tm.cellBounds;
tm.CompressBounds();
for (int x = bounds.min.x; x < bounds.max.x; x++)
{
for (int y = bounds.min.y; y < bounds.max.y; y++)
{
// cast to Tile
var tile = tm.GetTile(new Vector3Int(x, y, 0)) as Tile;
if (tile != null)
{
Debug.Log("--------");
Debug.Log("Tile name: " + tile.name);
Debug.Log("Tile sprite: " + tile.sprite);
Debug.Log("Tile color: " + tile.color);
Debug.Log("Tile colliderType: " + tile.colliderType);
Debug.Log("Tile pos:" + new Vector3(x,y,0));
Debug.Log("Tile flags: " + tile.flags);
// ...
}
}
}
Thanks! I didn’t know this existed within ITilemap! Appreciate the tip!
public static implicit operator ITilemap(Tilemap tilemap)
{
return new ITilemap(tilemap);
}