How do I use Tilemap.SetTile() with a tile asset? I want to use it for scriptable tiles in the 2d-extras package, such as rule tiles, weighted random tiles, and animated tiles. How would I 1. get inspector reference of this scriptable tile, and then 2. use SetTile() with it? Thanks for your attention.
It depends on how you want to store your Tile Assets, such as through Resources, Asset Bundles or creating them dynamically in the Player. Using Unity Addressable Assets (Getting started | Addressables | 1.16.19) could help to manage this for you, so that you can set a Address for a Tile Asset in the Editor and load it up in the Player with the same Address to call SetTile with.
Very, very simple Editor-based way
1. Create tile (whichever one you want)
2. Create script like so:
public class TileTest : MonoBehaviour
{
public TileBase someTile;
public Tilemap someTilemap;
public Vector3Int targetCell;
void Update()
{
if(someTilemap != null)
someTilemap.SetTile(targetCell, someTile);
}
}
3. Attach this script to a GameObject in the scene.
4. In the Inspector, drag in someTilemap (from the Scene), someTile (from the Project) and assign a targetCell.
Based on whatever you assign within the Inspector to targetCell, this script will set that cell with someTile every frame. If you adjust the targetCell at runtime, it will Set. If someTile is null, it will clear any tile at that cell. Make sure your someTile asset has a sprite assigned or else nothing will show up.
The docs could benefit from a simple example like this.
That’s correct. Basically you can have either TileBase or Tile to place on a TileMap. TileBase allows adding an asset tile (for example a Tile with rules)
[SerializeField] private Tilemap tileMap;
[SerializeField] private TileBase tile; // using TileBase enables the asset
...
tileMap.SetTile(new Vector3Int(-x + width / 2, -y + height / 2, 0), tile);
but if you only need a normal tile you can use only Tile
[SerializeField] private Tilemap tileMap;
[SerializeField] private Tile tile;
...
tileMap.SetTile(new Vector3Int(-x + width / 2, -y + height / 2, 0), tile);