Search Unity

Problem with Preview of Tiles on Tilemap

Discussion in '2D' started by JupiterOP, Oct 13, 2019 at 12:23 AM.

  1. JupiterOP


    Mar 5, 2019
    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?
  2. ChuanXin


    Unity Technologies

    Apr 7, 2015
    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:

    Code (CSharp):
    1. using UnityEditor;
    2. using UnityEditor.Tilemaps;
    3. using UnityEngine;
    5. [CustomGridBrush(true, false, true, "No Preview Brush")]
    6. public class NoPreviewBrush : GridBrush
    7. {
    8.     public bool showPreview = false;
    9. }
    11. [CustomEditor(typeof(NoPreviewBrush))]
    12. public class NoPreviewBrushEditor : GridBrushEditor
    13. {
    14.     public new NoPreviewBrush brush
    15.     {
    16.         get { return target as NoPreviewBrush; }
    17.     }
    19.     public override void OnPaintSceneGUI(GridLayout gridLayout, GameObject brushTarget, BoundsInt position,
    20.         GridBrushBase.Tool tool, bool executing)
    21.     {
    22.         if (brush.showPreview)
    23.         {
    24.             base.OnPaintSceneGUI(gridLayout, brushTarget, position, tool, executing);
    25.         }
    26.     }
    27. }
    Alternatively, depending on when you are calling Tilemap.SetTile(), you could check if Tilemap.HasEditorPreviewTile is valid to see if you are currently previewing?
    JupiterOP likes this.
  3. JupiterOP


    Mar 5, 2019
    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.