How to iterate a Tilemap

Sometimes you need to perform an action with all the tiles in a tilemap. For example, to save all the tile data information in a serialized class to save and load a tilemap from disk.

You can do this by using the helper methods IterateTilemapWithAction in TilemapUtils.


[System.Serializable]
public class TilemapSerializedData
{
[System.Serializable]
public class TileData
{
public int gridX;
public int gridY;
public uint tileData;
}

public List tileDataList = new List();
}

public static TilemapSerializedData SerializeTilemap(STETilemap tilemap)
{
TilemapSerializedData data = new TilemapSerializedData();
System.Action action = (tmap, gridX, gridY, tileData) =>
{
data.tileDataList.Add( new TilemapSerializedData.TileData() { gridX = gridX, gridY = gridY, tileData = tileData } );
};
TilemapUtils.IterateTilemapWithAction(tilemap, action);
return data;
}

 

Advertisements