Create a custom tile that runs a script
Create a prebuilt or custom scriptable tile that dynamically changes its look or the look of the tiles around it.
Read time 2 minutesLast updated 20 days ago
Scriptable tiles are tiles that dynamically change their appearance, or change the appearance of the tiles around them. Scriptable tiles are part of the 2D Tilemap Extras package.
For example, you can script tiles to change how they look based on neighboring sprites, or play animations.
You can do either the following:
- Add a prebuilt scriptable tile.
- Create a custom scriptable tile using the API.
TileBase
Add a prebuilt scriptable tile
To create a prebuilt scriptable tile, follow these steps:
- Make sure the 2D Tilemap Extras package is installed in your project. For more information, refer to set up your project for 2D games.
- From the main menu, select Assets > Create > 2D > Tiles.
- Select a prebuilt scriptable tile.
- Drag the tile from the Project window into a tile palette to paint with it.
For more information, refer to Scriptable tiles in the 2D Tilemap Extras package documentation.
Create a custom scriptable tile
Follow these steps:
-
To create a new C# script, from the main menu select Assets > Create > MonoBehaviour script.
-
Open the script and replace the code with a class that inherits from. For example:
UnityEngine.Tilemaps.TileBaseusing UnityEngine;using UnityEngine.Tilemaps;public class MyScriptableTile : TileBase{} -
Implement themethod. This method determines the look of the new tile. For example:
GetTileDatapublic override void GetTileData(Vector3Int position, ITilemap tilemap, ref TileData tileData){ tileData.color = Color.white;} -
To create instances of your tile as assets in your project, add aattribute before the class declaration.
CreateAssetMenu[CreateAssetMenu]public class MyScriptableTile : TileBase{ ...} -
Create an instance of the tile. For example in the previous example, from the main menu, select Assets > My Scriptable Tile.
-
Drag the tile from the Project window into a tile palette to paint with it.
Examples
Refer to the following for examples of a custom scriptable tile:
- .
Tilemaps.TileBase.GetTileData - for an example of using
Tilemaps.TileBase.RefreshTileto determine which other tiles run theirRefreshTilemethod when you paint the tile.GetTileData