Search Unity

  1. Welcome to the Unity Forums! Please take the time to read our Code of Conduct to familiarize yourself with the forum rules and how to post constructively.
  2. We have updated the language to the Editor Terms based on feedback from our employees and community. Learn more.
    Dismiss Notice

Question How to transform a tile of type tilebase

Discussion in '2D' started by dev-danno, Apr 27, 2023.

  1. dev-danno

    dev-danno

    Joined:
    Apr 27, 2023
    Posts:
    2
    Hello!
    I'm new to unity and i'm trying to get to grips with tilemaps and pooling. I have the following code:


    Code (CSharp):
    1. public class LevelGenerator : MonoBehaviour
    2. {
    3. public Tilemap tilemap;
    4.     public TileBase platformTile;
    5. public List<TileBase> tilePool;
    6.     private void Update()
    7.     {
    8.         if(currentTileY - player.position.y < distanceBeforeSpawnBlock)
    9.         {
    10.             SpawnTiles();
    11.         }
    12.     }
    13.     private void SpawnTiles()
    14.     {
    15.         tilePool[0].transform.position = new Vector2(Random.Range(-5, 5), currentTileY);
    16.         currentTileY += distanceBetweenTiles;
    17.      
    18.     }
    19. }
    My issue is that I cannot transform something of TileBase however i'm not very sure what to do instead. I tried changing it to a list of type Tile but then it errors at position instead of transform.

    My reason for using TileBase is because i want to use a Tile Rule to create the platforms

    Thanks!
     
    Last edited: Apr 27, 2023
  2. karderos

    karderos

    Joined:
    Mar 28, 2023
    Posts:
    376
    its hard to understand what you are trying to do and it doesnt make much sense

    to set tiles you should use tilemap.SetTile()
     
  3. dev-danno

    dev-danno

    Joined:
    Apr 27, 2023
    Posts:
    2
    I'm trying to get it to spawn tiles essentially. I tried
    tilemap.SetTile(new Vector3Int(Random.Range(-5, 5), Mathf.RoundToInt(currentTileY), 0), tilePool[0]); It adds it to the pool however doesn't draw the tile onto my game
     
  4. venediklee

    venediklee

    Joined:
    Jul 24, 2017
    Posts:
    148
    Heyo, I implemented object pooling and chunking for a tilemap level generator on the asset store.

    You don't need to "pool" the tiles of a tilemap unless you are making an infinitely large map. The chunk rendering* mode on the tilemap renderer is already fast enough.
    upload_2023-4-27_15-46-6.png

    If you really need to "pool" the tiles use something like this:
    Code (CSharp):
    1. var bounds = new BoundsInt(new Vector3Int(chunkX, chunkY, 0), new Vector3Int(_chunkSize.x, _chunkSize.y, 1));
    2.             var nullTiles = new TileBase[_chunkSize.x * _chunkSize.y];//you can fill the nullTiles array with your own tiles. it must match the size of the bounds
    3.            tilemap.SetTilesBlock(bounds, nullTiles);
    *the SRP batch mode is even better from what I heard

    Some additional notes:
    • the "tilemap.SetTile..." code you shared might not be working because the Z is 0, I might be wrong on this. Maybe your tilemap doesnt have a tilemap renderer, maybe your tile doesnt have a visible sprite etc.
    • tilemap.SetTile doesn't add a tile to YOUR pool like you mentioned, I am confused what you mean by that
    Cheers
     
  5. karderos

    karderos

    Joined:
    Mar 28, 2023
    Posts:
    376
    you also have to change from tilebase to tile, and you need to create tiles on your list