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;
}

 

Advertisement

Published by

creativespore

I love making tools for games. And games...

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s