I'm trying to render a 2D tile map. I've placed a Tilemap
object in the scene from the create 2D object context menu, set a reference to it to one of my script objects, and tried to render. It's definitely placing tiles, (I can see it go from transparent to drawing something,) but instead of the tiles I'm giving it, it creates a pure black square in every cell.
My tiles are coming from a tileset image, comprised of a 16x16 grid of tiles of 64x64 pixels in size. The tile setup code is as follows:
public class GameLevelView : MonoBehaviour
{
private Tilemap _grid;
private Tile[] _tiles;
public MapData Map { get; set; } //supplied in code by an external source
void Start()
{
_grid = GetComponent<Tilemap>();
_tiles = Resources.LoadAll<Sprite>("tiles").Select(s => new Tile { sprite = s }).ToArray();
}
void Update()
{
Map.PaintTo(_grid, _tiles);
}
}
The PaintTo
routine simply loops over every cell, gets a tile index, and calls tilemap.SetTile(new Vector3Int(x, y, 0), tiles[idx]);
. This seems simple enough that nothing could go wrong... and yet all the tiles are rendering black.
In case it's relevant, using Unity 2020.3.26f1 with the 2D URP. Things I've already verified:
- The tileset image has been imported as a sprite sheet correctly.
- The tile array loading code is working as expected.
- The tiles produced by the tile array have their values set up as expected. For example, their Color property is
(1,1,1,1)
rather than somehow being black. - The GameObject holding the tile map is active in the scene and positioned correctly on the camera.
- MapData is being set up correctly.
At this point I have no idea what else to look at to try and make this work. Has anyone seen this before and might know how to fix this?