Search Unity

Problem with Preview of Tiles on Tilemap

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

  1. JupiterOP

    JupiterOP

    Joined:
    Mar 5, 2019
    Posts:
    4
    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

    ChuanXin

    Unity Technologies

    Joined:
    Apr 7, 2015
    Posts:
    325
    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;
    4.  
    5. [CustomGridBrush(true, false, true, "No Preview Brush")]
    6. public class NoPreviewBrush : GridBrush
    7. {
    8.     public bool showPreview = false;
    9. }
    10.  
    11. [CustomEditor(typeof(NoPreviewBrush))]
    12. public class NoPreviewBrushEditor : GridBrushEditor
    13. {
    14.     public new NoPreviewBrush brush
    15.     {
    16.         get { return target as NoPreviewBrush; }
    17.     }
    18.    
    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

    JupiterOP

    Joined:
    Mar 5, 2019
    Posts:
    4
    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.