How to create a checkerboard tile pattern in Unity?

598 views Asked by At

I'm trying to create a checkerboard alternating tile in Unity, but creating it by hand is time-consuming and cumbersome.

I tried creating a rule tile to achieve the checkered effect, but it hasn't worked since the rule tile doesn't allow checking if the neighbor tile is of a specific type.

How can I achieve a checkerboard effect with automatic tiles to automatically place black and white tiles respectively - alternate between them?

1

There are 1 answers

0
Phillip Zoghbi On BEST ANSWER

Final script

If you're already familiar with all the steps how to make this work inside Unity, here's the script:

using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.Tilemaps;
using UnityEditor;

public class AlternatingTile : Tile
{
    [SerializeField] private Sprite[] tiles;
    
    public override void GetTileData(Vector3Int position, ITilemap tilemap, ref TileData tileData)
    {
        uint mask = (uint) (position.x + position.y);
        tileData.sprite = tiles[mask % 2];
    }

#if UNITY_EDITOR
    [MenuItem("Assets/Create/2D/Tiles/Alternating Tile")]
    public static void CreateAlternatingTile()
    {
        
        string path = EditorUtility.SaveFilePanelInProject(
            "Alternating Tile",
            "New Alternating Tile",
            "Asset",
            "Please enter a name for the new alternating tile",
            "Assets");

        AssetDatabase.CreateAsset(ScriptableObject.CreateInstance<AlternatingTile>(), path);
    }
#endif
}

Explanation

To create the desired checkerboard tile rule in Unity, first, create a new C# script and call it something like AlternatingTile.cs.

Open the script and change the base class to Tile.

public class AlternatingTile : Tile

Delete the Start() and Update() methods, because we are not using MonoBehaviour.

Add a class-level Sprite[] array for the "black and white" tiles.

[SerializeField] private Sprite[] tiles;

Create an override function for Tile.GetFileData.

public override void GetTileData(Vector3Int position, ITilemap tilemap, ref TileData tileData)
{
    uint mask = (uint) (position.x + position.y); // gets the absolute position of the tile
    tileData.sprite = tiles[mask % 2]; // mask = 0 or 1 depending on the absolute position of the tile
}

Now that we created the logic for handling which tiles to draw, let's enable Unity to create the asset for this tile.

#if UNITY_EDITOR
[MenuItem("Assets/Create/2D/Tiles/Alternating Tile")] // Where to put the menu item in menu
public static void CreateAlternatingTile()
{
    string path = EditorUtility.SaveFilePanelInProject(
            "Alternating Tile", // name of the menu
            "New Alternating Tile", // default name of the new file
            "Asset", // extension (.Asset)
            "Please enter a name for the new alternating tile", // message
            "Assets/Sprites & Tiles/Tiles"); // default folder in the project - change this to match your project!

    AssetDatabase.CreateAsset(ScriptableObject.CreateInstance<AlternatingTile>(), path);
}
#endif

Usage

If you followed the same menu item path as in the example above - in Unity Editor, click Assets > Create > 2D > Tiles > Alternating Tile

Menu item

Select the asset in your Project window, and assign the two tiles you wish to alternate.

Alternating Tile in Inspector

Drag the asset to your Tile Palette and start painting it. As you can see, the tiles are drawn in alternating pattern (checkered/checkerboard pattern).

Painting the tiles