Search Unity

Always use tilemap ?

Discussion in 'Scripting' started by blober81, Nov 11, 2017.

  1. blober81

    blober81

    Joined:
    May 6, 2016
    Posts:
    97
    Hi Everyone,

    Regarding the new tilemap option in unity I was wondering if it's the best way to make a random map or could I just use tiles from a spritesheet and create a map that way. From what I understand the tilemap is especially usefull when creating maps manually but do I really need it when I want to create a random map ? Does it increase performance when I use tilemap instead of loading sprites from a spritesheet ?

    Thanks in advance
     
  2. Akzafield

    Akzafield

    Joined:
    Aug 20, 2017
    Posts:
    17
    I am using tilemap to make "big" 2d random map . and its very optimized.
     

    Attached Files:

    blober81 likes this.
  3. blober81

    blober81

    Joined:
    May 6, 2016
    Posts:
    97
    How many tilemaps do you have in your grid ? For example you have a ground tilemap with order in layer 0 and a water tilemap with order in layer 1. What code do you use to add a tile in either ground tilemap or water tilemap ?
     
  4. Akzafield

    Akzafield

    Joined:
    Aug 20, 2017
    Posts:
    17

    Yes i am using multiple tilemap to display layer (may be a ineficient way of doing it but i dont realy have other idea for this?)
    I have 65536(256*256 map) tile on the layer 0 and around 20000 tile on layer 1 (tree) running at a constant 60 fps with vysnc.

    To do this I am using a 2D array representing my map , the array is filled with "Case" object that can contain "CustomObject" that inherit from TileBase but each case has a default TileBase to draw IF there is not any CustomObject contained in that case (on my screen the dirt is the default thing to display).
    Doing this allow me to ask to a case what is inside that case Where is that case and what to display.

    https://docs.unity3d.com/2017.2/Documentation/ScriptReference/Tilemaps.TileBase.html

    then I just use a Tilemap and use TileMap.SetTile(Case.GetWhatToDisplay()) (something like that)

    https://docs.unity3d.com/2017.2/Documentation/ScriptReference/Tilemaps.Tilemap.SetTile.html

    sorry but i am not explaining thing well I hope it will help you ^^. If needed i can provide you some code.
     
  5. blober81

    blober81

    Joined:
    May 6, 2016
    Posts:
    97
    Thanks for your reply. What i was thinking is to make a loop with like 3 sets of tiles (water, grass, dirt). The water will be on WaterMap (layer 2), the grass on GrassMap (layer 1) and the dirt on DirtMap (layer 0). The thing is that I don't know how to place a tile on a specific map. Obviously I want to place the water tiles on the WaterMap and so forth.

    For example the loop chooses a random value which turns out to be a grass tile. How do I create a new grass tile and place that tile on the GrassMap ?

    Cheers
     
  6. Akzafield

    Akzafield

    Joined:
    Aug 20, 2017
    Posts:
    17
    The idea is to make a 2D array of Object that are going to represent your 2D world and a List of TileMap representing layer
    Lets call those object "Case".those object are going to contain a List of other object to display lets call those "CustomTile" . so if you Whant to Add a object at a location in your worldmap you are going to use something that look like this

    Code (CSharp):
    1.  public void CreateCustomTileAt(int x, int y,CustomTile objToAdd)
    2.     {
    3.       // getCase will return m_CaseArray[x, y] OR null if out of range
    4.         Case curCase = GetCaseAt(x, y);
    5.         if (curCase != null)
    6.         {
    7. // so lets call a function that is basicly going to do a List<CustomTile>().add(objToAdd)
    8.             curCase.AddObject(objToAdd);
    9.         }
    10.  
    11.     }


    So now how to Display those thing? , after generating your map you are going to do a For that is going to parcour your 2D Case Array and ask "What can I display at LayerList[index]" ? and this function will return the first CustomTile where
    CustomTile.GetLayer() == layer; after that you have a CustomTile in your hand, then just ask to it CustomTile.GetWhatToDisplay() (function that return the tile to Display) and then use LayerList[index].SetTile(new Vector3Int(CustomTile.x,CustomTile.y,0),CustomTile.GetWhatToDisplay()).


    (something like this)

    Code (CSharp):
    1.   public void InitializeMap()
    2.     {
    3.         for(int y = 0; y < m_MapSize; y++)
    4.         {
    5.             for (int x = 0; x < m_MapSize; x++)
    6.             {
    7.                 // for each layer you have
    8.                 for(int i = 0; i < m_TilemapList.Count; i++)
    9.                 {
    10.                     // look if something can be displayed at the layer i
    11.                     CustomTile custTile = Case[x,y].GetWhatToDisplay(i);
    12.                     if(custTile != null)
    13.                     {
    14.                         // if something has een found add it to the tileMap
    15.                         m_TilemapList[i].SetTile(new Vector3Int(custTile.GetPosX(), custTile.GetPosY(), 0), custTile.GetTileToDisplay());
    16.                     }
    17.  
    18.                 }
    19.             }
    20.         }
    21.     }
     
    Last edited: Nov 14, 2017
  7. blober81

    blober81

    Joined:
    May 6, 2016
    Posts:
    97
    Ok first of all thanks for the effort you put in to help me out I really appreciate it.

    So there are a few things I don't understand. You say you make an Object called "Case" so I expect something like; GameObject Case; But your line is Case curcase. Case is in blue letters, were did Case itself get initialized ? And the line 'GetCaseAt' is that called from the library because I don't know were it's coming from.

    Also you have a method called GetWhatToDisplay() and GetTileToDisplay() but I don't understand how it is used and I can't find the methods in your example code to figure that out.

    CustomTile custTile = Case[x,y].GetWhatToDisplay(i);

    And when you create custTile it's generated from CustomTile but were is CustomTile initialized ? Is CustomTile a gameObject like; GameObject CustomTile; ?

    And is the code CustomTile custTile = Case[x,y].GetWhatToDisplay(i); a faster way of writing it down because normally when I retrieve a value from a method I would do;
    GetWhatToDisplay(i); and then add the retrieved value to the created cusTile but you did this; CustomTile custTile = Case[x,y].GetWhatToDisplay(i);

    Also I don't see were you put your random values. Were you also creating a random map ?

    Is it OK if I can see the full code ? It makes it easier to figure this out because like I said above there are some things you do I am not familiar with like calling a method within an '=' statement. (CustomTile custTile = Case[x,y].GetWhatToDisplay(i) )

    Cheers !
     
  8. Akzafield

    Akzafield

    Joined:
    Aug 20, 2017
    Posts:
    17
    Yea no problem I will create an example today i and will send it to you ^^.


    EDIT : here it is :

    https://nofile.io/f/dxoTZH7uADY/LifeGameUEx.zip

    notice that this is a dirty implementation of it, and there is no "clean" layer but there is a hardcoded solution for layer , but this may give you a global idea to make it ;)))

    (dont mind script about task order this is an old test )
     
    Last edited: Nov 15, 2017
    blober81 likes this.
  9. Roy1337

    Roy1337

    Joined:
    Aug 18, 2015
    Posts:
    7
    Akzafield,

    I see you mentioned you have trees on your big generated map.
    I am having trouble with the Unity 2017 Tilemap with my trees. When the tilemap draws a tree with a particular tree sprite, it draws all trees that share that sprite at the same time. So the sorting order of each tree is getting messed up, and trees that are different sprites are drawing on top of one another.
    What was your solution for trees in your map?

    Thanks!
     
  10. Akzafield

    Akzafield

    Joined:
    Aug 20, 2017
    Posts:
    17
    I am using multiple tilemap to "represent" layer o tilemap 0 = ground, tilemap 1 = Tree, tilemap 2 = cloud, etc..