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

How to programmatically add Tilemaps to Grid

Discussion in '2D' started by zHaytam, Jan 4, 2019.

Thread Status:
Not open for further replies.
  1. zHaytam

    zHaytam

    Joined:
    Jan 4, 2019
    Posts:
    3
    Hello,

    I want to make a map system entirely in code (since my game will have multiple maps and they will be binary files).

    I don't want to draw every map (or at least not in Unity directly) and I'm struggling right in the beginning (new to Unity).

    Can we add Tilemaps (layers) to an empty grid just by code?
    I know I can just draw layers without using a grid but it seems like 2D Tilemaps are widely used and I'm not sure what's the right way...

    Thank you.
     
  2. TonyLi

    TonyLi

    Joined:
    Apr 10, 2012
    Posts:
    12,545
    Yes, you can do that. Here's the basic code:

    Create a Grid GameObject:
    Code (csharp):
    1. var grid = new GameObject("Grid").AddComponent<Grid>();
    Create a Tilemap child GameObject:
    Code (csharp):
    1. var tilemap = new GameObject("Tilemap").AddComponent<Tilemap>();
    2. tilemap.transform.SetParent(grid.gameObject);
    Set a tile:
    Code (csharp):
    1. Tilemaps.TileBase tile; // Assign a tile asset to this.
    2. tilemap.SetTile(new Vector3Int(0,0,0), tile); // Or use SetTiles() for multiple tiles.
     
  3. MrShinyhelmet

    MrShinyhelmet

    Joined:
    Jan 10, 2020
    Posts:
    1
    What if I wanted to do this in reverse? I am wanting to find a tilemap in a grid at runtime.
     
    Krooq likes this.
  4. eses

    eses

    Joined:
    Feb 26, 2013
    Posts:
    2,637
    @MrShinyhelmet

    It is a different question, so why necro an old thread? Why not ask this in its own thread?

    Besides, Tilemap is just a GameObject that is a child of GameObject that has the Grid. So you can get it like any child GameObject in Unity. Google for "unity get child gameobject" (or transform).
     
  5. APSchmidtOfOld

    APSchmidtOfOld

    Joined:
    Aug 8, 2016
    Posts:
    4,473
  6. OneToMany

    OneToMany

    Joined:
    May 25, 2018
    Posts:
    6
    How do you find which cell(or tile) was clicked on?
     
  7. MelvMay

    MelvMay

    Unity Technologies

    Joined:
    May 24, 2013
    Posts:
    10,623
    This thread isn't about the question you're asking. You should start your own thread on the subject.
     
  8. gubcodes

    gubcodes

    Joined:
    Feb 3, 2023
    Posts:
    1
    Is there a more modern version of this that should be used? When trying to work with the code provided, this third line throws a "Argument 1: cannot convert from 'UnityEngine.GameObject' to 'UnityEngine.Transform'" error.

    I am ashamed to admit I have spent an entire day trying to figure out how to create a Grid + Tilemap via script, so I thought I'd revisit this old question. Any help is appreciated.
     
  9. TonyLi

    TonyLi

    Joined:
    Apr 10, 2012
    Posts:
    12,545
    That looks like a typo. Try SetParent(grid.transform)
     
    gubcodes likes this.
  10. MelvMay

    MelvMay

    Unity Technologies

    Joined:
    May 24, 2013
    Posts:
    10,623
    Please don't necro/hijack posts. Your post is a typo/misunderstanding of C# and again not related to the thread at all.
     
    gubcodes likes this.
Thread Status:
Not open for further replies.