Search Unity

Question Should I use default Unity Tilemap for rendering?

Discussion in 'Graphics for ECS' started by Klusimo, Jan 5, 2021.

  1. Klusimo

    Klusimo

    Joined:
    May 7, 2019
    Posts:
    76
    Hi, I want to make 2D tile-based ground, but I dont know if the classic unity tilemap is good with DOTS.

    Should I use the classic tilemap or should I make my own from code? Are there any issues like slower performance? If I can/should, how exactly, because it is made specifically for editor purposes and DOTS operates from code so how do I edit the tilemap from code and how do I get the references to the tilemap itself and the sprites for the tiles?
     
  2. msfredb7

    msfredb7

    Joined:
    Nov 1, 2012
    Posts:
    168
    Tilemaps do not convert to entities at the moment. You can write your own conversion script to convert the tilemap data to runtime entities. See Conversion Systems and IConvertToEntities for that.

    That gives you the ability to chose how you want to represent your tiles in entities. 1 entity with a dynamic buffer of 'tiles'? A series of entities for each tile ? There are already threads discussing that I think.
     
  3. Klusimo

    Klusimo

    Joined:
    May 7, 2019
    Posts:
    76
    Actually I already have fully functional system where I have one chunk (not unity chunk) as entity and it holds data for every tile as dynamic buffer, what I need is just visual tiles that work with these entites.
     
  4. msfredb7

    msfredb7

    Joined:
    Nov 1, 2012
    Posts:
    168
    Ok, I understand the issue better. It's about rendering right? Unfortunately I haven't worked with the hybrid renderer enough to give advices on that.

    On my project, we use normal gameobjects (and MonoBehaviour Tilemaps) to render the view. Only the simulation is ECS. So we basically have a Tilemap MonoBehaviour controller that observes the tile entities and polulates the 'view tile map'
     
    DotusX likes this.
  5. Klusimo

    Klusimo

    Joined:
    May 7, 2019
    Posts:
    76
    Does hybrid renderer have the function to work with tilemaps? (if you know)
     
  6. msfredb7

    msfredb7

    Joined:
    Nov 1, 2012
    Posts:
    168
    I don't think so no.
     
  7. Klusimo

    Klusimo

    Joined:
    May 7, 2019
    Posts:
    76
    And do you think making a tilemap object and then getting references of that tilemap as well as all the necessary sprites into systembase script and then using Tilemap.SetTile would work/be plausible?
     
  8. Krajca

    Krajca

    Joined:
    May 6, 2014
    Posts:
    347
    Yes, it's possible. ECSSample project has an example of that.
     
    msfredb7 likes this.
  9. Klusimo

    Klusimo

    Joined:
    May 7, 2019
    Posts:
    76
    Ok but I need specific code, i mean what functions to call, because I dont know how to get references from asset folder in order to get the needed sprites and from what I know they need to be specific "TileBase" thing and I dont know how to transform them into that either
     
  10. Krajca

    Krajca

    Joined:
    May 6, 2014
    Posts:
    347
  11. Klusimo

    Klusimo

    Joined:
    May 7, 2019
    Posts:
    76
    You mean like create the hybrid component or that it is the one in the code, also if I understand it correctly, that code creates its own tilemap? I am sorry, but I just didnt fully understand that code, I a not exactly expert in this stuff, I would be thankful if you explained it to me.
     
  12. Krajca

    Krajca

    Joined:
    May 6, 2014
    Posts:
    347
    You need to create a normal OOP MonoBehaviour class i.e. "TileMapManager". Then create its conversion system, just like the example does with WireframeGizmo and WireframeGizmoConversionSystem. That allows you to find it in an ECS environment, i.e. like that:

    Code (CSharp):
    1. public class TestSystem : SystemBase
    2. {
    3.     protected override void OnUpdate()
    4.     {
    5.         Entities.WithoutBurst().ForEach((Entity entity, Grid2DController grid2DController) =>
    6.         {
    7.             grid2DController.count++;
    8.         }).Run();  
    9.     }
    10. }
    Grid2DController is my script that manages a Tilemap. So, in there, you can have a method that set your tiles.
    Of course, it's managed code so no burst and you need to use Run() but it's integrated into ECS so EntityQuery and such works.
     
    DotusX likes this.
  13. Klusimo

    Klusimo

    Joined:
    May 7, 2019
    Posts:
    76
    Ok thanks.
     
  14. illinar

    illinar

    Joined:
    Apr 6, 2011
    Posts:
    863
    Of hybrid rendering? Or TilemapRenderer specifically? I don't see a Tilemap example, and I have an issue with that one specifically if I put it into a subscene and declare it as Hybrid. I can see the tilemap, but it looks different.

    Both shots are in play mode.

    upload_2021-8-22_13-55-53.png

    upload_2021-8-22_13-56-49.png

    Outside of a subscene it looks fine in play mode.

    Again makes me wonder what dark magic is going on with subscene game obgects.

    Seems to be rendering all tiles in the middle. Maybe it lost connection with the Grid..


    Yep.. So here is a little guide to using tilemaps in subscenes.

    1) Have this code somewhere to declare the tilemap components as hybrid so that they are kept in the converted scene:
    Code (CSharp):
    1. [ConverterVersion("unity", 1)]
    2. public class DeclareHybridComponentsSystem : GameObjectConversionSystem
    3. {
    4.     protected override void OnUpdate()
    5.     {
    6.         Entities.ForEach((Grid c) =>
    7.         {
    8.             AddHybridComponent(c);
    9.         });
    10.         Entities.ForEach((Tilemap c) =>
    11.         {
    12.             AddHybridComponent(c);
    13.         });
    14.         Entities.ForEach((TilemapRenderer c) =>
    15.         {
    16.             AddHybridComponent(c);
    17.         });
    18.         Entities.ForEach((SpriteRenderer c) =>
    19.         {
    20.             AddHybridComponent(c);
    21.         });
    22.     }
    23. }
    24.  
    25.  
    2) Have Tilemap, TilemapRenderer and Grid components on a same Game object, unlike the way Unity creates it by default with Grid as a parent. I assume the hierarchy breaks upon conversion, and you could restore it in code, but you can just have it all on one object.

    3) Well, put your tilemap object in a subscene and it will work.

    4) You might have to have URP installed I am not sure. also Unity 2020 just in case since they say 2021 is not officially supported for Entities.
     
    Last edited: Aug 22, 2021