I have been working on a 2D Top Down game for some time now and I have been living dangerously taking each update as they become available.
Unfortunately, 2021.2.0f1 has done me in with setting tiles programmatically at run time.
My logic is basically a loop that creates a new Vector3Int when the noise map allows for it and a single rule tile that go into the positions and tiles arrays respectively for a call to Tilemap.SetTiles(vector3IntArray, tileBaseArray)
This was working fine, but now the tiles do not render.
If I change the code solely that is calls Tilemap.SetTile(vector3Int, tileBase) at every position with the same single rule tile, then the tiles render correctly.
// Works now
private void Generate2()
{
sandTilemap.ClearAllTiles();
for (var x = 0; x < 100; x++)
{
for (var y = 0; y < 100; y++)
{
sandTilemap.SetTile(new Vector3Int(x, y, 0), sandTile);
}
}
}
// Worked in 2021.1.27f, but not now
private void Generate()
{
sandTilemap.ClearAllTiles();
var sandPositions = new List<Vector3Int>();
var sandTiles = new List<TileBase>();
for (var y = 0; y < height; y++)
{
for (var x = 0; x < width; x++)
{
var position = new Vector3Int(x, y, 0);
sandPositions.Add(position);
sandTiles.Add(sandTile);
}
}
sandTilemap.SetTiles(sandPositions.ToArray(), sandTiles.ToArray());
}
The generate method looks a little odd in that the values height and width aren’t specified. I assume they’re defined elsewhere. But aside from that, at first glance, it should work.
If you look at the release notes for 2021.2:
2D: Improved performance for setting multiple Tiles on a Tilemap.
2D: Improved performance of RuleTile caching.
So you may have found a bug? If these are rule tiles, try RefreshAllTiles and see what happens.
Regarding the height and width, my test code does has serialize fields which are set to 100, but in my haste to write the individual SetTile example, I just used the literal integer values.
They are rule tiles and I did try calling RefreshAllTiles after the SetTiles(arr, arr) call and unfortunately it did not cause them to magically appear.
For what it’s worth, I looked at the inspector for the Tilemap component and I can at least see that there are tiles in the Info array.
You can kind of see that in the attached screenshots.
What happens if the tiles you place aren’t rule tiles?
I ran my Tilemaps extension’s sanity test that places, cut/paste, deletes, copies/paste etc many tiles (in some cases, 8000 of them) both plain and animated in 2021.2 and it works fine and all sprites show. This is totally via code on an empty tilemap. So this might be confined to rule tiles.
If your rule tiles are custom I’d check the GetTileData methods. I personally don’t use rule tiles at all, so can’t be much more help, sorry.
I really appreciate your help either way, it is at least pointing me in a direction such that I can likely solve the issue myself until such time as a new version addressing this issue is released, but I suppose I need to actually file a bug report at this point.
Going off of your suggestions…
I have static rule tiles and an animated rule tile, neither work.
I made a simple 5 frame animated tile and it works fine, just as you say.
Definitely seems like it is limited to rule tiles specifically.
I am using the default rule tile and not doing any custom scripting.
I suppose I could make a custom rule tile script that just calls the base methods to keep poking at this and maybe come up with an interim solution that isn’t downgrade back to 2021.1.27, though I will probably just do the latter to be honest.
Perhaps rule tile changes in the most recent tilemapsextras pkg? Maybe the base classes have changed and you need to update. Can’t help myself from trying to troubleshoot
Unfortunately, this is a bug which will have to be fixed in a future Unity release. We will work on that and post the version which this is fixed in here!
In the meantime, this can be worked around by setting a Color in the Tile’s GetTileData code:
tileData.color = Color.white;
This would require a change in the RuleTile if you were using the version from the Unity Package Manager though.
Thanks for confirming this issue. Do you have an ETA for when we can expect the patch? Before I found this thread, I reported a bug here, you can use it for tracking if it’s helpful: https://github.com/Unity-Technologies/2d-extras/issues/324
Sorry for the long delay, but the fix for this will finally be available in 2021.2.6f1! 2021.2.6f1 should hopefully be available by the end of next week.