Problem with Preview of Tiles on Tilemap

Currently I have a tile that I am placing on Unity’s 2D tilemap. It’s a custom tile that places another tile below it. I’m having a bit of clash when selecting the tile in the Tile Palette as the preview of the tile on the Tilemap is causing my code which calls Tilemap.SetTile() to set a tile even though it’s in preview mode. This is making my tilemap full of tiles even if I haven’t clicked to place anything and I just drag my mouse across the screen.

Is there any way to disable or modify this previewing functionality?

For example could I find out through code If I am in preview mode and avoid calling Tilemap.SetTile() only in preview mode?

There is no way to disable this directly. You could try the following code as a custom brush to toggle on and off the preview:

using UnityEditor;
using UnityEditor.Tilemaps;
using UnityEngine;

[CustomGridBrush(true, false, true, "No Preview Brush")]
public class NoPreviewBrush : GridBrush
{
    public bool showPreview = false;
}

[CustomEditor(typeof(NoPreviewBrush))]
public class NoPreviewBrushEditor : GridBrushEditor
{
    public new NoPreviewBrush brush
    {
        get { return target as NoPreviewBrush; }
    }
   
    public override void OnPaintSceneGUI(GridLayout gridLayout, GameObject brushTarget, BoundsInt position,
        GridBrushBase.Tool tool, bool executing)
    {
        if (brush.showPreview)
        {
            base.OnPaintSceneGUI(gridLayout, brushTarget, position, tool, executing);
        }
    }
}

Alternatively, depending on when you are calling Tilemap.SetTile(), you could check if Tilemap.HasEditorPreviewTile is valid to see if you are currently previewing?

1 Like

Ah ok thank you! I was wondering if a custom brush could do that. I had hacked a solution where I wouldn’t run the TileData setup if I didn’t have the spawning tile above it. Tilemap.HasEditorPreviewTile fits cleanly in my code.