How to store unique data in each tile (Unity 2D Tilemap tutorial)

แชร์
ฝัง
  • เผยแพร่เมื่อ 7 ส.ค. 2020
  • Learn to create a map that can represent stuff like heat, oxygen or stink. I chose the latter one as an example.... The underlying system here is a script that stores the data based on a tilemap, which we also use to visualize it.
    Get the full project here (free) : github.com/ShackMan2000/Tutor...
    To see the system in action we will create a skunk that spreads stink which will be visible with Unitys Tilemap. A pig will roam and read that data to navigate.
    Check out my newest game Idle Potato Power. It's a fun and relaxing game about exploring Potato Power in all it's multidimensional weirdness.
    play.google.com/store/apps/de...
    github.com/ShackMan2000
    Discord: / discord

ความคิดเห็น • 32

  • @FlipYourLearning
    @FlipYourLearning 2 ปีที่แล้ว +6

    This was amazingly useful. I had been trying to implement a similar system for days and honestly wasn't expecting to find a video showing how to do such a niche thing (I want to use it for immune cells and bacteria leaving trails of molecules as they move. My best bet at first was to look for temperature maps and heat transfer simulation videos, but it didn't work well. Suddenly I saw your miniature mentioning stink, and I realized that it was exactly what I needed, and the video did not disappoint). Thanks a lot.

  • @krissichan3127
    @krissichan3127 3 ปีที่แล้ว +1

    Beautiful my man, very good.

  • @pavelholub6275
    @pavelholub6275 2 ปีที่แล้ว

    Thanks for this! Now i am able to make humidity, organic matter and heat maps :)

  • @titushenson8227
    @titushenson8227 2 ปีที่แล้ว

    Took my like an hour to find this just what I was looking for

  • @gavin_is_gavin6292
    @gavin_is_gavin6292 2 ปีที่แล้ว

    oh man, thanks for the info about the color lock. I had no idea why my code wasn't working!

  • @manyfailsonewin4352
    @manyfailsonewin4352 ปีที่แล้ว

    This and the other video about tiles has been extremely helpful. I shot myself in the foot by accidentally making the darker stink tile on alpha 0 and kept wondering why my second and third clicks were making things fade lol. This will help me immensely in creating a tiled game with water and other flowing types of materials (I hope!). So far so good!

    • @ShackMan
      @ShackMan  ปีที่แล้ว +1

      That happens to everyone, Unity sets the alpha of new colors to 0. Must have wasted about a million hours of dev time in total...cool user name btw, great attitude :-)

    • @manyfailsonewin4352
      @manyfailsonewin4352 ปีที่แล้ว

      @@ShackMan ahh that makes me feel a little better. i was wondering how i managed to goof that up. thanks!

  • @bm3410
    @bm3410 2 ปีที่แล้ว

    "adjust it for the x and y, and add some stink" *thumbs up*

  • @322ss
    @322ss 2 ปีที่แล้ว +1

    Pretty nice stuff! I've done something similar, but I've never considered using updating tiles in runtime scenarios. Have you done any profiling/testing, how many updating tiles will bog your computer down?

    • @ShackMan
      @ShackMan  2 ปีที่แล้ว +2

      I didn't do actual profiling, just stress testing and it could handle a lot! I've found tilemaps to be quite performant in general, and my method here doesn't do that much work. And it could of course be optimized when you know what exactly your game needs.

  • @killhour
    @killhour 2 ปีที่แล้ว

    One thing that would be good to add is the ability to use the tilemap to create an initial state that the game can start with instead of having everything initialized to 0 - think cellular automata.

  • @HazebertM
    @HazebertM 2 ปีที่แล้ว

    Hi, great tutorial! I've been following along but, every time I run the first test, it never adds color. When I turn the alpha back up, clicking the just removes the tile. I've followed the code completely as shown in the video. Any idea of what I might be doing wrong? Thanks!

  • @obygdengaming
    @obygdengaming 3 ปีที่แล้ว +2

    Thanks for the tutorials!
    Quick question, how would I go about iterating through all the tiles in the tilemap, finding all tiles with a certain Int value set in their TileData, and add them to a list? I am new to Tilemaps and Scriptable objects, and get a bit confused with all the new terms lol

    • @ShackMan
      @ShackMan  3 ปีที่แล้ว +6

      Use Tilemap.origin to get the lower left corner and Tilemap.origin + Tilemap.size for the top right corner. Those are vectors giving you the positions. That way you can loop through all the tiles. Checking whether a tile has a certain stink value is then a simple if statement. In this video I only added the stinking tiles to the dictionary. So you could loop through all positions in the tilemap and check if the stinkingTiles dictionary has that position as a key, if so, get the value of that key to check the stink value.

    • @obygdengaming
      @obygdengaming 3 ปีที่แล้ว +2

      @@ShackMan Ahhh I figured it out! Thanks so much! I'm gonna post my solution here if anybody else needs it:
      //Get the map area
      BoundsInt bounds = map.cellBounds;
      // Get all tiles in map area
      TileBase[] allTiles = map.GetTilesBlock(bounds);
      //Iterate through all tiles
      foreach(TileBase tile in allTiles){
      //Make sure there is a tile at position and check a bool from the TileData
      if(tile != null){
      if(dataFromTiles[tile].switchTile == true){
      Debug.Log("Found Switchable Block");
      // adds it to a list based on a TileData int value
      if(dataFromTiles[tile].switchNumber == 0){
      switchableTiles0.Add((dataFromTiles[tile]));
      }
      }
      }
      }

  • @ausername4meplease
    @ausername4meplease ปีที่แล้ว

    Can this system be used in a farming game where you dig, water and plant things in tiles? What about performance? Thank you for a great tutorial!

    • @ShackMan
      @ShackMan  ปีที่แล้ว +1

      Sure. Performance depends on the size of the tilemap and how intensively you process it. My advice in general is to just try it out, once you have the core mechanics down, do a stress test where you increase the size/processing by 100x or 1000x and see what happens.

  • @TengizAdamashvili
    @TengizAdamashvili 2 ปีที่แล้ว

    Question. How would you store unique data in EVERY tile? I can iterate over each tile and that gives me row and col values, which do not correspond to grid_position values. I tried getting a world bounding box position, so I can find each tile by iterating coordinates but the tilemap bounding box is super janky and simply wrong and unreliable. Please advice

    • @ShackMan
      @ShackMan  2 ปีที่แล้ว

      Not sure, what what you mean... the row and col value should exactly correspond to a grid position, by which you can get the world position (the center of the tile).

  • @AlexFSTR
    @AlexFSTR 2 ปีที่แล้ว

    Hello, I have a tile animation, but I would like to place it in a tilemap, I would like to do all this in code.
    But tilemap has only one function which is SetTile() which asks me for a TileBase, so the question, how can I convert my TileAnimatorData to TileBase?

    • @ShackMan
      @ShackMan  2 ปีที่แล้ว

      You could write a script that just swaps the sprite every 0.x seconds with SetTile, looping through a list of your sprites. Sounds incredibly bad for performance, but I think it's always worth trying it out and see what happens. There are of course animated tiles that you can create in the inspector, but I've never tried out how to set the sprites through script.
      docs.unity3d.com/Packages/com.unity.2d.tilemap.extras@1.6/manual/AnimatedTile.html

  • @PeterMilko
    @PeterMilko 3 ปีที่แล้ว

    cool

  • @remilaurent2055
    @remilaurent2055 10 หลายเดือนก่อน

    Is it possible to change the sprite of tile when it's clicked ?

    • @ShackMan
      @ShackMan  10 หลายเดือนก่อน

      Yes, I have another video with how to change tiles at runtime, you can swap the tile at runtime through code.

  • @sjs_ujs
    @sjs_ujs ปีที่แล้ว

    very good ,tsk

  • @alexeyprosin6343
    @alexeyprosin6343 ปีที่แล้ว

    Dictionary? Ouch... That was painful.

    • @quadroninja2708
      @quadroninja2708 3 วันที่ผ่านมา

      idk, log2(n) seems like an okay efficiency

  • @rebelcrusader9973
    @rebelcrusader9973 2 ปีที่แล้ว +3

    Dude, it's all fine and good, but why do you add so many empty lines when coding? I have to constantly go back because your previous code gets constantly hidden by the absurd amount of empty lines you add for no reason.

    • @manyfailsonewin4352
      @manyfailsonewin4352 ปีที่แล้ว +1

      some reason some ppl prefer to put their { on same line and some don't, i would guess. just little things that makes it easier for ppl to read their own code.