Trying to do what should be a pretty simple task - I want to populate a Tilemap with tiles at runtime. Here’s the relevant code:
using UnityEngine;
using UnityEngine.Tilemaps;
/// <summary>
/// Manages the grid that masks the terrain grid lines.
/// </summary>
public class PreviewGrid : SingletonBehavior<PreviewGrid>
{
#region Cached Components
private Tilemap m_tilemap;
#endregion
public TileBase GridTile;
protected override void Awake()
{
base.Awake();
m_tilemap = GetComponent<Tilemap>();
}
/// <summary>
/// Clears the current grid preview.
/// </summary>
public void ClearPreview()
{
//m_tilemap.ClearAllTiles();
}
/// <summary>
/// Shows the grid preview at the specified location.
/// </summary>
public void SetPreview(Vector3Int location)
{
ClearPreview();
m_tilemap.SetTile(location + new Vector3Int(0, 0, 0), GridTile);
m_tilemap.SetTile(location + new Vector3Int(-1, 0, 0), GridTile);
m_tilemap.SetTile(location + new Vector3Int(0, -1, 0), GridTile);
m_tilemap.SetTile(location + new Vector3Int(-1, -1, 0), GridTile);
m_tilemap.RefreshAllTiles();
}
}
What I’m seeing is that this only works in cells that already had tiles painted in at edit time - i.e. it can update existing tiles, but not create new ones. The problem seems to be mostly on the mesh generation side, because the tile is a scripted tile and it will select a sprite properly as though the missing tiles were there. It feels like I’m just missing another refresh or update call, but I can’t find one.