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. Dismiss Notice

Merging two tilemap grids into one with script

Discussion in '2D' started by thatcraniumguy, Oct 4, 2020.

  1. thatcraniumguy

    thatcraniumguy

    Joined:
    Aug 23, 2020
    Posts:
    2
    I have a base tilemap grid prefab that I'm loading as a gameobject. The grid has two tilemaps separately; one for the floor, one for the walls. This first prefab is randomly generated in a chunk-by-chunk basis, and has a variable size. I also have a separately created tilemap grid prefab that I hand-created. It is the same size as the chunks that make up the first grid. I'd like to be able to merge the two prefabs into one so that the second prefab overwrites one of the chunks of the first prefab.

    I'm able to load the prefabs as gameobjects via script, but I'm not sure how to iterate through the tile coordinates in order to replace the tiles that I want. I can't seem to find a method that allows me to iterate through the tilemap coordinates.

    If someone knows a way to get the size of the tilemap grid, I could then use my existing method of creating tiles and simply create a new grid with the tiles copied from the prefabs. I also need to know what method I need to use to do this for both the floor tilemap and the walls tilemap, so I need to know how to diferentiate between the two when reading the gameobject data.
     
  2. Lo-renzo

    Lo-renzo

    Joined:
    Apr 8, 2018
    Posts:
    1,319
    https://docs.unity3d.com/ScriptReference/Tilemaps.Tilemap.html

    Get bounds in cell-space. Note, unless it's completely full of tiles then many tiles will be null
    https://docs.unity3d.com/ScriptReference/Tilemaps.Tilemap-cellBounds.html

    BoundsInt has convenient way to foreach through all of its contents: boundsInt.allPositionsWithin
    https://docs.unity3d.com/ScriptReference/BoundsInt-allPositionsWithin.html

    So you can loop through your sourceTilemap (walls), use GetTile and if not null apply to the targetTilemap (floor) with SetTile. If performance matters here and you're processing a large # tiles, then it may be more efficient to use GetTileBlock and SetTiles after pruning away the nulls b/c it sounds like you wouldn't want to clear your target where your source has a null tile.
     
    danidina330 and eses like this.
  3. thatcraniumguy

    thatcraniumguy

    Joined:
    Aug 23, 2020
    Posts:
    2
  4. Lo-renzo

    Lo-renzo

    Joined:
    Apr 8, 2018
    Posts:
    1,319
    Yeah, that will give you the tilemaps in order. Then it's a matter of treating the correct one as the source and the other as the target.

    edit: I should note that if your two tilemaps aren't using the same bounds, then perhaps your copy may need to add some Vector3Int offset so that when you copy from one it'll put the tiles into the correct position of the other. That will normalize the positions to the coordinates you want your tiles confined within.
     
    Last edited: Oct 5, 2020