How to store data in tiles (Unity Tilemap + Scriptable Objects)

แชร์
ฝัง
  • เผยแพร่เมื่อ 15 มิ.ย. 2024
  • In this video I to show you how you can use scriptable objects to store any kind of information in a tile. You don’t have to install any extras or make special tiles, you can use the standard tiles straight out of the box with Unity’s built in Tilemap. As an example I show you how to make those little bugs that check how fast they can crawl based on the type of tile they are crawling over.
    If you want to store unique data for every single tile in the map, check out this tutorial:
    • How to store unique da...
    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

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

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

    I searching about how to custom scripting the grid and tile, and this is most understandable video i ever seen. Thank you!

  • @whiterice6016
    @whiterice6016 3 ปีที่แล้ว +4

    I found this right in time for Ludum Dare 48. Thank you very much

  • @hothaian9990
    @hothaian9990 3 ปีที่แล้ว +31

    Your tutorial gives me exactly the info I need in the simplest, easiest way. Definitely better than the Unity official guide haha. Have a sub from me and good luck on your TH-cam channel.

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

    Very helpful video. I've been slowly bootstrapping myself into Unity, this video ties a bunch of things together for me in a very concise way.
    Agree with earlier comments that speaking a little slower and slowing navigation down a bit so people can see what you typed would be beneficial. As it was I had to make prodigious use of pause and skipping back a bit.

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

    this video is so underrated, thanks for the great info!

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

    Really cool video. Many interesting methods and stuff.

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

    Thank you for this good and simple tutorial

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

    was looking around for this. nice to know tilemaps can do this. thanks a bunch :)

  • @adamfrank5183
    @adamfrank5183 9 หลายเดือนก่อน

    I think the singleton is a good call here. I'm working on a game right now that requires each tile to have a large amount of data. I thought about using scriptable objects with some kind of helper/manager, your video has me more confident in it. Thanks

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

    Thanks~
    i used this to learn how to give tiles information. I didn't think of this beforehand xD.
    Now my enemys and adventurers can move based on the tiles they move over.

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

    Thank you so much for this ! So useful ! Love it.

  • @user-sz1rm5vl8p
    @user-sz1rm5vl8p 3 ปีที่แล้ว

    A super clear explanation.

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

    I was having a lot of trouble getting this to work. Turns out that for some reason, my Mouse Position returned -1 as the Z value when converted to Vector3Int. Manually setting gridPosition.z = 0 before map.GetTile(gridPosition) fixed it for me. Thanks for the video!

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

    Great tutorial! Thank you.

  • @dianarune9704
    @dianarune9704 3 ปีที่แล้ว +9

    In case anyone is confused when he's like creating 'TileData' in the menu, that's the new 'instance' of his script, 'TileData',
    for you, it will be the name of your script. Like 'isSpikeTile'. You will want to make two or more of these.

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

      Thanks for clarifying that, I should have explained more as this is not something you see in beginner tutorials. I've worked for like a year in Unity before learning about non monobehaviour classes and how useful they can be.

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

      Thnaks for that, I was getting an error and it was bc of this!

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

      @@boltio3543 :D Yay!

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

    Nice one, didn't know about TileBase

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

    omg i was pulling out my hair trying to get the 'click reporting' portion to work, because VS autofilled in ScreenToWorldPoint with ScreenToViewpoint and I didn't notice for hours and hours. lmao. classic coder issues. thanks for the guide btw, very helpful.

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

      sorry for the random question, but why are there no curly braces required on line 71 etc in mapmanager script? never seen that...

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

    4:55 - i just want to clarify, I caught you adding the tile map to the script object, but i imagine many people who are very new to coding may miss this, as you don't actually say anything about assigning it, you just do it while saying "Add the script and enter play mode". Things seem a little rushed in these tutorials. like, you don't seem to give an awful lot of time for viewers to read the code you are putting before you change screen and continue talking. It can be a little frustrating having to pause, go back, pause go back every 2 seconds because of the speed you do things.
    Just some constructive feedback :), thanks BUNCH for the video!

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

    Many thanks for this tutorial! very helpful.
    I have a query - How would one get Tile and its position with a perspective camera ?
    You are surely using an orthographic cam.

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

    Looking forward to the struct implementation

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

      I was going to do that tomorrow. Any request on what to use as an example? Right now I was thinking of having the bugs take a crap on the tile map, which lets flowers grow. But let me know if you have a better idea so I can pivot.

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

      Here it is: th-cam.com/video/nRFZDmThbdE/w-d-xo.html I went with dictionaries instead of structs as they are much faster for lookups.

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

    Awesome tutorial! Helped me a lot to understand what's going on with the tilemaps.

  • @ONAPAL3HORS3986
    @ONAPAL3HORS3986 9 หลายเดือนก่อน

    This is perfect!

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

    thanks for the vid

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

    Great tutorial!!! I'm not sure if I did something wrong. It works great until I switch scenes. For some reason it remembers every tile from the first level and disregards the tile data in the second level.

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

    Genius. I can't praise you enough for this.

  • @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?

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

    Fast and furious! Thanks.

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

    I think a better approach will be to inherit from and create your own tilebase, that way the data is actually stored inside the tile, you have more flexibility when wanting to change the tile appearance and no import method is needed when you want to get the information on start.
    See for reference:
    th-cam.com/video/VV0NgnztIQg/w-d-xo.html

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

      Thanks, I'll check out the video, hadn't seen it.

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

      Exactly. This brute force approach isn't the best.

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

    It is great! I was looking for something like this. How about perfomance? Is it better solution than having more tilemaps and raycast it with tilemap collider?

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

      Very likely. Dictionaries in C# are actually really fast for looking up things (like looking for the connected data in this case). Collider are usually the hardest performance hit, because every frame everything that has a collider needs to be checked if it is touching or not.

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

      @@ShackMan Nice to hear that. Thanks for the answer!

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

    When I try the GetTile, I can only get information of the tiles I created in-game. Tiles that were already there are null somehow. Do you know what might cause that problem?

  • @user-be6ev8tt2w
    @user-be6ev8tt2w 10 หลายเดือนก่อน

    Hello, could you please show, how to add character to tile map? And make his position like Vector3Int.

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

    Good video

  • @PK-se2jh
    @PK-se2jh 2 ปีที่แล้ว

    thankyou sooo much :)

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

    thanks for the tutorial! i have a question tho, i have tried using this on a tilemap that has 2 layers, one of them is mostly empty and i intend to add the data within both layers to get the result for something else, but im getting an error that the key cant be null, i was thinking on making every empty tile a tile in itself that consisted of an empty image and use that as a tile, is there any other fix you know of?

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

      Where exactly do you get that error? In my example where we get the walking speed, I check if the tile is null, and if so, I simply return 1f. That way I never end up asking the dictionary with a null value. If you are using 2 tilemaps, you'd simply have to check for null twice.

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

      @@ShackMan thanks for the quick reply!
      Im getting the error in the console, im still trying out the click on tile part to test it out, but now that i think about it just doing the thing you said on the get tilewalkingspeed function may fix it for actual use
      thanks either way c:

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

    Hey man, I was wondering if you know of a way to create a tilemap with a limited size? When you create a tilemap directly in the hierarchy it seems to stretch to infinity in all directions. I wanted to know if I could somehow alter a value which would "bind" the tilemap to a certain size and set its origin at 0,0. Do you have any ideas?

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

      Sorry, no idea. I think it makes no difference in terms of performance. If you want it because it's tied to an ingame system, you could make a script that is between you and the tilemap, where you simply remove or add numbers. But that wouldn't have any influence in the editor of course.

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

    Could you use this scriptable object system to change the tile's sprite to something else? I'm thinking of trying to adapt it fit a season system for a project of mine (changing grass tiles to snow and vice versa).

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

      you could add this to the scriptable object:
      public TileBase winterVersion;
      public TileBase springVersion;
      etc. and assign the according tiles in the inspector
      Then in the MapManager:
      ChangeToWinter()
      {
      //go through every tile, get the connected scriptable object
      //check if it has a winter Tile, if so, replace the tile by using
      map.SetTile(position, winterTile)
      }
      You could even combine it with my other tutorial about storing unique data, by giving each tile a height. Then the map manager goes through the Change Winter function but only does it to tiles above a certain height, then a while later it does it again and lowers the height a bit.

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

      @@ShackMan Thanks so much! I'll check out the other video also.

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

    Thanks, that is an instructive video. One question, just a scenario, how could I remove some tile elements which the bugs run into.

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

      To remove a tile you simple set it to null. So you would get the tile world position the same way, and then
      tilemap.SetTile(new Vector3Int(0,0,0), null); // Remove tile at 0,0,0

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

      @@ShackMan Thanks!

  • @phillipwebb1104
    @phillipwebb1104 6 หลายเดือนก่อน

    Awesome video mate. Any chance there is a way to use this system to effect the player character when walking on water tiles?

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

      yes, pretty much the same way. Connect a special value to the water tiles and have a player script read it just like the bugs are reading the tiles.

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

    Great tutorial, managed to help me solve a few problems I was having. Don't know if you knew this but there is already a class in Unity called "TileData" which caused a bit of confusion on my end when I kept getting an error on the "foreach(var tile in TileData.tiles)" line.

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

      I'm also having problems with this line. How did you fix it?

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

      How did you fix this?

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

      @@tylerhughes4990 Been forever since I looked at this and I don't have the code. Is the error something along the lines of "TileData does not have attribute 'tiles'" or something? If so you need to change the name of the user-defined variable to something else.

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

    best video on youtube.

    • @mrpancakeguy
      @mrpancakeguy 10 หลายเดือนก่อน +1

      still the best.

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

    Really helpful, thank you.
    Just a suggestion, sometimes you finish typing in a line in quick-time but then you quickly switch to the next scene before we even have a chance to look at what you just typed ( eg. @ 4:46). Please try to leave content on the screen for more than 1/2 second ;)
    Another thing, you don't describe the step where you drag the tilemap into the Map property of MapManager. I missed it first time through. Just mentioning what you are doing there would be helpful, it's easily missed.

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

      sorry for the late reply, I really appreciate feedback like that. I hope I started doing a better job in newer videos because it annoys me too when I watch and follow along other people's tutorials and have to jump back and forth to catch that one second the line of code is showing...

  • @sanjay1994.
    @sanjay1994. 3 ปีที่แล้ว

    Hey, how to change tile palette color when an object passes on it.. Like coloring the path of the object in tiles.

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

      I explain that in this video: th-cam.com/video/nRFZDmThbdE/w-d-xo.html
      Greetings!

    • @sanjay1994.
      @sanjay1994. 3 ปีที่แล้ว

      @@ShackMan thanks a lot. Have searched everywhere. Finally found it.. Keep it up.👌

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

    How do you drag and drop your tiles into your TileBase array....?

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

      there is a tiny lock icon in the top right corner of the inspector. If you click it, the inspector will lock for whatever object it is showing. Then you can click other things and drag them in there. When you're done just unlock the inspector window.

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

    So helpful, only 489 subs you've been treated harshly

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

      It's 491 now, so it's all good :-)

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

      @@ShackMan I was one of them :)

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

    What if we do this without scriptable object?
    like creating TileDictionary(TileBase, SpeedValue) and creating TileBase List and in Start iterating through TileBaseList and if Tile's name is Ground then TileDictionary.add(TileBaseList[i],15). and if tile's name is Grass then TileDictionary.add(TileBaseList[i],5).
    [SerializeField]
    private Tilemap _tileMap;
    public List tileBaseList;
    public Dictionary TileDictionary = new Dictionary();

    void Start()
    {
    for (int i = 0; i < tileBaseList.Count; i++)
    {
    if(tileBaseList[i].name == "Ground")
    {
    TileDictionary.Add(tileBaseList[i], 10);
    }
    else if(tileBaseList[i].name == "Grass")
    {
    TileDictionary.Add(tileBaseList[i], 5);
    }
    }
    }

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

    Have you ever done something like this but using scriptable tiles? I feel as if this is the same thing but I was curious!

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

      Never used scriptable tiles for data like that, only for adjusting the sprite automatically (like when you paint water, the edges will turn into beach tiles)

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

      @@ShackMan yeah I was checking out the scriptable brushes and scriptable tiles (which are both amazingly rad) though I think our solution might need data as you've demonstrated here; so maybe a hybrid solution for us!

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

    This tutorial was quite simple and effective. Nice work. I just don't get why do you need to have so many blank lines when you code.

  • @xSephironx
    @xSephironx 6 หลายเดือนก่อน

    Thank you so much mr. Schwarzenegger

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

    sourcecode pls

  • @vincekerti7088
    @vincekerti7088 5 หลายเดือนก่อน

    Too many tile... variable name! It might be confusing

  • @DiegoTube85
    @DiegoTube85 3 ปีที่แล้ว +5

    c'mon bro slow down, I cant even follow your mouse cursor

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

    This brute force approach isn't the best. You should be able to associate a GO directly with a tile rather than having to scan potentially a long lookup list containing all SOs and tile. Not your fault, but Unitys.

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

    There are a lot of bugs in this code :D

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

      which bugs exactly? Cheers

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

      @@ShackMan the bugs which are walking with different walking speed. This was a bad joke 😅😂😂
      Sorry, if that was not clear.

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

      @@DominikBruehl lol

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

    in this block of code tileData.tiles gives me an error: 'type' does not contain a definition for 'tiles' and no accessible extension method 'tiles' accepting a first argument of type 'type' could be found (are you missing a using directive or an assembly reference?).
    did I do something wrong? or does unity no longer have a definition for 'tiles'? if so what do I use instead?
    foreach (var tileData in tileDatas)
    {
    foreach (var tile in tileData.tiles)
    {
    }
    }

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

      is using Visual Studio, select the error and hit ctrl + . Should suggest you to add the correct using statements above (Tilemap stuff)