Search Unity

Tile's TileData not properly updating

Discussion in '2D' started by SYCrash, Oct 19, 2020.

  1. SYCrash

    SYCrash

    Joined:
    May 5, 2019
    Posts:
    3
    I've been trying to implement GetTileData but to no avail.
    I need the information on the Tile Data to update itself once the game is at runtime.
    For example I want the sprite in editor to be X, but during play mode to be Y
    or when in editor, i want all tiles to have their normal transform, but once play mode is up, i want the Z position to change accordingly.
    If i do a tilemap.RefreshAllTiles, it goes through every single one of them, but it visually doesn't update as if either the new tile data gets rejected.
    but this issue only applies to tiles that have been set up in editor. any new tile added during playmode/runtime, works as intended.

    Example
    Code (CSharp):
    1. [CreateAssetMenu]
    2. public class VisualTile : Tile
    3. {
    4.     public Color playmode;
    5.     public override void GetTileData(Vector3Int position, ITilemap tilemap, ref TileData tileData)
    6.     {
    7.         base.GetTileData(position, tilemap, ref tileData);
    8.         // ANY CHANGE IN TILE DATA
    9.  
    10.         if (Application.isPlaying)
    11.         {
    12.             // ANY CHANGE IN TILE DATA DURING PLAY
    13.  
    14.             tileData.color = playmode;
    15.  
    16.         }
    17.     }
    18.  
    19. }
    20.  
    Things I've noticed:
    - Even in editor mode, two different tile using the same tile from the pallete will always be the same regardles.
    - Any tile that is added through SetTile will properly show differences.
    - Any tile that is added from Editor's tilemap brush when the game isn't playing seems to have this issue.
    - Doing Tilemap.RefreshAllTiles() triggers the code, but does not actually fix the issue.
    I'll be testing this more over the coming days, but i've been really blocked by this issue which doesn't seem to be documented anywhere.
     
    Last edited: Oct 19, 2020
  2. spryx

    spryx

    Joined:
    Jul 23, 2013
    Posts:
    557
    You aren't doing anything in GetTileData... At least not from what you posted....it's simply calling the base method. If you want the Sprite to change, you need to actually assign it. Ex. https://docs.unity3d.com/ScriptReference/Tilemaps.TileBase.GetTileData.html
     
  3. Lo-renzo

    Lo-renzo

    Joined:
    Apr 8, 2018
    Posts:
    1,513
    Does this simple test case work?
    Code (CSharp):
    1.     [CreateAssetMenu]
    2.     public class VisualTile : Tile
    3.     {
    4.         public Sprite editorSprite;
    5.         public Sprite playSprite;
    6.  
    7.         public override void GetTileData(Vector3Int position, ITilemap tilemap, ref TileData tileData)
    8.         {
    9.             tileData.sprite = Application.isPlaying ? playSprite : editorSprite;
    10.         }
    11.     }
    12.  
    Depending on the type of changes you're making, TileFlags may be relevant. You might want to double-check TileFlags. If you're making changes without the correct TileFlags that can sometimes be a source of issues.

    You should probably describe what you want to do here in more detail / show code.
     
    SYCrash likes this.
  4. SYCrash

    SYCrash

    Joined:
    May 5, 2019
    Posts:
    3
    Apologies, I had mistakenly posted an older copy of it.

    I'll edit the original post and post it here again
    Code (CSharp):
    1. [CreateAssetMenu]
    2. public class VisualTile : Tile
    3. {
    4.     public Color playmode;
    5.     public override void GetTileData(Vector3Int position, ITilemap tilemap, ref TileData tileData)
    6.     {
    7.         base.GetTileData(position, tilemap, ref tileData);
    8.         // ANY CHANGE IN TILE DATA
    9.         if (Application.isPlaying)
    10.         {
    11.             // ANY CHANGE IN TILE DATA DURING PLAY
    12.             tileData.color = playmode;
    13.         }
    14.     }
    15. }
    From what i found out so far, is that if i paint using a brush for a tile from a pallete, it'll always appear as the pallete, regardless of what changes i have set up in the getTileData
    But if i programmatically add a tile to the tilemap, it'll appropriately update.

    Like mentioned above, i've tested the simple case like the above written code, and it did not work. not calling the base tile data ends up causing the tile to be invisible for some reason. tile data gets assigned the editor color, and then gets assigned playmode after, and like mentioned previously, it doesn't work on tiles that have been there in the map pallete but works for tiles that got added at runtime
     
    Last edited: Oct 19, 2020
  5. spryx

    spryx

    Joined:
    Jul 23, 2013
    Posts:
    557
    Try calling Tilemap.RefreshAllTiles during Start. I have a feeling that if you've already painted them into the scene, GetTileData would be called prior to runtime. In other words, if they are already in the scene, there isn't any reason for a call to be made to get tile data.
     
  6. ChuanXin

    ChuanXin

    Unity Technologies

    Joined:
    Apr 7, 2015
    Posts:
    1,068
    You likely need to add TileFlags in your TileData (https://docs.unity3d.com/ScriptReference/Tilemaps.TileFlags.html), specifically TileFlags.LockColor or TileFlags.LockTransform. This will lock the color or the transform of each tile in the Tilemap to the values specified by GetTileData. Otherwise, it will retain any values previously set on the Tilemap, usually from previous Tiles.
     
    SYCrash likes this.
  7. SYCrash

    SYCrash

    Joined:
    May 5, 2019
    Posts:
    3
    sorry for being late on the reply, it seems that yes, the issue was the misuse of tiles.
    It seems that i misunderstood what these flags meant. So i decided to make a new fresh project to test it out.
    upload_2020-11-1_16-21-31.png
    Color red is the target color they should turn to.
    While i was locking them last time i tried this, it seems i wasn't double checking to make sure that the tile saved itself properly. Lock transform now also works properly as well.
    Thank you @ChuanXin and @Lo-renzo