I have about 40 tile maps and multiple layers, rather than modifying each one by hand and wishing I was a robot, I'm trying to write a script to do that. The problem is that this is leaving the old layers under the render data game object and creating the layers rather oddly. Obviously I'm doing this wrong, what is the best practice to add/remove tile map layers through code to ensure everything is taken care of?
Here is my code for what I have:
if(GUILayout.Button("Create Tile Map Layers"))
{
List<tk2dTileMapData> tileMapDataList = new List<tk2dTileMapData>();
tileMaps = FindObjectsOfType(typeof(tk2dTileMap)) as tk2dTileMap[];
Debug.LogError("Size: "+tileMaps.Count().ToString());
foreach (tk2dTileMap tileMap in tileMaps)
{
tileMap.data.tileMapLayers.Clear();
tileMap.BeginEditMode();
for(int i = 0; i < 5; i++)
{
tk2dRuntime.TileMap.LayerInfo newLayer = new tk2dRuntime.TileMap.LayerInfo();
newLayer.name = "Layer "+i.ToString();
newLayer.generateCollider = false;
newLayer.useColor = true;
newLayer.z = i;
newLayer.unityLayer = 20 + i;
newLayer.sortingOrder = i;
newLayer.hash = 0x70d32b98;
if(i == 1)
{
newLayer.generateCollider = true;
}
if(i == 0)
{
tileMap.data.tileMapLayers[i] = newLayer;
}
else tileMap.data.tileMapLayers.Add(newLayer);
Debug.LogError(tileMap.name+" i:"+i.ToString());
}
tileMap.EndEditMode();
}
}