How to build a tilemap at runtime?

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

155478-2020-03-30-09-28-45-steam.png

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.

The problem turned out to be in the BorderTile TileBase override. I was setting the TileData in ‘GetTileData’ to a new instance, which wipes out the tile’s transform.