Let's take our Grid System and make a Custom Tilemap with Saving and Loading so we can easily share Levels! 🎮 Play 7 Awesome Games (Action, Strategy, Management) and Help Support the Channel! ✅ Get the Game Bundle 67% off unitycodemonkey.com/gameBundle.php
I started following your channel and learned a lot of things I didn't know before. Thanks for all the detailed explanation which helps me to make my codes cleaner and easier to use. I used to hard code all the logic and waste tons of time on unnecessary places, and now I start to use generic class and namespace to store methods that can be reused in other projects, plus some cool system features I learned from you. It's great to find an awesome mentor like you on the internet.
I like this because it makes it much easier to build levels outside of Unity and just import them. As long as the JSON is in the right format, it'll just work.
I wonder how does the constructor at 3:46 does work? new TilemapObject(grid,x,y). At this time grid should be null. But with finishing this line the variable grid is overwritten. But the grid of the tilemap object is still pointing to null or how is the reference changed to the current grid? (I'm not sure about wording here.. )
Yes in there it should be new TilemapObject(g, x, y); to use the grid that is passed in the function and not the one in the field. I fixed it later but it gets cut off in the video because that line ended up way too big.
@@CodeMonkeyUnity Thanks for the comment though, following this tutorial with sleepy eyes because the learning value is so addictive, can't stop, but I had nullpointers at runtime. This was the problem. Thank you for this series by the way!
I am having some difficulties at 5:07, when I tried out the script I got a nullreferenceexception: NullReferenceException: Object reference not set to an instance of an object Tilemap+TilemapObject.SetTilemapSprite (Tilemap+TilemapObject+TilemapSprite tilemapSprite) (at Assets/Scripts/Tilemap.cs:48) Tilemap.SetTilemapSprite (UnityEngine.Vector3 worldPosition, Tilemap+TilemapObject+TilemapSprite tilemapSprite) (at Assets/Scripts/Tilemap.cs:21) Testing.Update () (at Assets/Scripts/Testing.cs:20) And I can't for the life of me find where it comes from or how to fix it, help would be very much appreciated.
Update, I found the problem and made a makeshift solution but I would love to know what I did wrong because it is messy code, my problem is that the variables grid, x, and y are never assigned. I followed the tutorial step by step but I still must have missed somthing, thanks in advance.
@@CodeMonkeyUnity Thank you for responding so quickly! Yes, I think I forgot to set the grid value because that was the one that was null. I have finished the tutorial now but I have another problem. In between the tiles you can sometimes see lines, after some research I found that this was also a problem with the tilemap from unity. But all solutions I could find were to add pixel bleed (wich I am not sure how to add) or something else you can only do in the built in unity tilemap system. Do you have any idea how to fix these lines? Thank you so much for your help, I would not have been able to make my current project without you.
When you create the constructor for the tilemap on the function part you send as a parameter grid. when it should be g. Just letting other know since it took me a while to figure out.
Hey Code Monkey - I'm interested to hear how you'd implement collisions into the tilemap? What if you were creating these tilemaps for players to explore and didnt want them running through wall tiles?
You could dynamically place a BoxCollider on each Tile position that you wanted to block. Or you can just make another Grid System to hold the walkable state, and when the player moves ask the Grid System is the player can move there, so pretty much just like the Pathfinding system unitycodemonkey.com/video.php?v=alU04hvz6L4
Hello! I followed the steps until 4:50 but when I click on the Grid I get an exception (NullReferenceException: Object reference not set to an instance of an object Testing.Update () (at Assets/Scripts/Testing.cs:17)) Any ideas?
if you're the same as me, he didn't show it in the video but in the Tilemap.cs, in the Tilemap constructor it needs to look like this: grid = new Grid(width, height, cellSize, originPosition, (Grid g, int x, int y)=>new TilemapObject(g, x, y)); but in the video, he shows: grid = new Grid(width, height, cellSize, originPosition, (Grid g, int x, int y)=>new TilemapObject(grid, x, y)); I'm not sure why he doesn't either explain the change or show in the video when he fixes it
Please help me! Im writing the code at 1:41 ive checked literaly letter by letter, because it throws this: error CS0308: The non-generic type 'Grid' cannot be used with type arguments even when i downloaded the script (allone) from the project files, it still threw that error with some others.
If, like me, you tend to use 'var' for most variables you may notice that your Ground and Path sprites aren't appearing correctly at around 16:05, then be sure you have the textureWidth and textureHeight variables declared as float. If not, they will become int values and will not contain the correct normalized values to select and display the sprites. It took me a bit to realize so hope this helps someone!
So, the one downside I can see with this implementation is that it only works at runtime. Is there a way to get similar functionality as the Unity tilemaps where you can build a level in the editor using this grid and then save the information with the scene? I've been trying to develop my own custom tilemap for this purpose and keep hitting brick walls. The Unity tilemaps are far too restrictive and the given tools are cumbersome to say the least.
Hello sir, I have a question about the function at 8:36. My TilemapVisual only works(visible) in scene view, and its location is at the top right corner of the screen. I download the project file and it seems like my code is basically the same as the project file. No error in Console either. I wonder what could be the problem here.
@@CodeMonkeyUnity Thanks for your response, I fix the problem. I have another question. I wonder why use Mesh in a 2D game and why not just use sprite and spriteRenderer?
A SpriteRenderer is a mesh, it's a flat quad, it just has a different name. You can put your Scene view into Wireframe mode to see the SpriteRenderer mesh
Quick questions: What would be "better" a big mesh with all the sprites like in your tutorials or game objects with sprite renderers for each tile but with frustum culling to cut all the not visible tiles away and pool older ones? The second question would be: How could I make smooth transitions between the tiles without drawing every transition, I want to lay the different tiles on top of each other.
A big mesh is a lot more performant. You can easily render 1000 tilemap quads, very difficult to get 1000 sprite renderers. For smooth transitions you could draw the transitions as half transparent and put them on another tilemap on top of the base one.
@@CodeMonkeyUnity I almost thought so. But unfortunately you seem to have updated your utility classes, they are no longer congruent as in the video. So I wasn't able to follow the video with your utils. Thanks for the quck anwser!
hello/ i imported the Project files and Utilities from package in link, and when i run the game i just see gray screen. Can you help me? what can be a problem?
Depends if you want the inventory to have "fixed" positions or not. I also covered a inventory with a grid system here ruclips.net/video/fxaDBE71UHA/видео.html All the positions are fixed, whereas the other inventory has a virtual list so its unlimited.
Hey, I just wanted to know if there any significant performance differences between your tilemap system VS Unity's built-in tilemap. I'm thinking about digging into some large scale procedural generation and this system seems practically perfect for that, but at the same time, I want to know if there's a significant performance difference between the two because then I'll have to choose the built-in system instead to decrease the performance hit (even though jerry rigging procedural tile generation in the built-in will be more difficult in the short term). Which do you think will be better from an optimization perspective? This or built-in?
I'm not sure since I never tested them side by side, I know this one is very performant since it's based on a single generated Mesh. Do a quick prototype to test it. But unless you have really small tiles I doubt there would be any performance issues
@@CodeMonkeyUnity Ah...see you absolutely nailed the problem. I'm trying to do a tilemap similar to the game Starbound (give it a quick search if you haven't seen it). The problem is that each tile in Starbound is made up of four 'sub' tiles. That obviously means that each tile is 1/4th the size of a regular tile but consequently there are 4x as many tiles in the world (It results in very smooth terrain almost to the point where sometimes it doesn't even look like a block game). I'm sort of stuck on how to make a system like this which is actually efficient. I know it's likely possible through the use of some trickery but I'm drawing a blank. I know this is a bit off topic here, but how would you go about making a system like that, theoretically speaking?
@@dragonlord1935 Oh right for something like that you definitely need a very performant approach. The biggest challenge with that is more with keeping the state of all those nodes. If I were making that I would probably use my system since it was made specifically to work on top of my Grid class which would be perfect for that type of game. But with that scale you cant have just a single Grid for the whole world so one very tricky challenge is how to break the world into tons of separate grids and handle any connections between them.
@@CodeMonkeyUnity Do you by chance know of a way to only load parts of an object? So essentially, only loading the tiles around the player into active memory while keeping everything else in a "lazy" data only format? how would you only load parts of a mesh? Is that even possible?
@@dragonlord1935 You can't load parts of an object but you can organize your world into multiple objects as opposed to a single one. So instead of a giant mesh occupying the whole entire world you split the world into say 100 meshes and only show the 5 nearest to the player.
Is there an easier method to fix the texture scaling than adding padding to all textures (see at 16:34)? I assume by adding padding you mean editing each texture to have padding around it manually. Or is there some way to do that in code? It appears that this happens when creating a material from the a texture. The edges are no longer pixel perfect but are blending into each other. The main issue with the padding is that then you can't have pixel perfect tiles can you? Never mind, my issue was setting the filter mode in the texture to anything but the correct one. Which is none ;) i.e. "Point (no filter)"
If you don't enable filtering and don't enable mip maps then yeah it will be pixel perfect, but that method is only suitable for pixel art games where the sprite size doesn't change. If you have non-pixel art or zoom in/out then you really need that padding.
It's not shown in the video, but towards the end of adding saving and loading, you also need to add the Tilemap parameter to the calling of the SetGrid function. It threw me off I didn't see it mentioned in the video, so I went back and rewatched it because I thought I missed something.
@@CodeMonkeyUnity Hey CM i'm new here but would love to show you my implementation of your grid for use with isometric maps, i currently use it alongside the default tilemap to make both drawing previewing and interactions easier (i use procedurally generated terain, so all of the tiles come from code anyway)
Hello Code Monkey! I have a question. How do we convert this for a hex grid like in the latest videos? I tried to use this for my hex grid but the tilemap is not created correctly. Thanks in advance and have a nice day!
You would need to modify the Mesh code to create a Hex shape. I covered the math for getting all the hex points in this video unitycodemonkey.com/video.php?v=dqframYHfws Then you would use those points as the vertices and set up the triangles
I'm sorry for this noob question, but how do I start to make my Grid from the bottom of the camera? Mine always starts at the center, and grows to the top right corner, so I can only a quarter of the grid.
Hello, I tried making the tilemap account for variable tile sizes, and therefore render the quads at different sizes, but it leads to overlaying quads. Is there a way to control the rendering order of individual quads? I tried sorting them by the Z axis and also by flipping the order in which they're updated, but nothing seems to work. As it stands right now tiles higher on the y axis always render in front. Thank you for all hypothetical responses!
The order is based on the vertices array. vertices[1] will show up on top of vertices[0] The best approach might be to have a base tilemap, and then make a second one on top that only contains the tiles you want overlayed.
I'm having some trouble at 23:50. When i save the scripts i get this error code: "Assets\Scripts\Tilemap.cs(25,23): error CS7036: There is no argument given that corresponds to the required formal parameter 'grid' of 'TilemapVisual.SetGrid(Tilemap, Grid)'" The problem seems to be with the "SetGrid" but i can't figure out how to fix it. " public void SetTilemapVisual(TilemapVisual tilemapVisual) { TilemapVisual.SetGrid(grid); } " The SetGrid function in TilemapVisual is: " public void SetGrid(Tilemap tilemap, Grid grid) { this.grid = grid; UpdateHeatMapVisual(); grid.OnGridObjectChanged += Grid_OnGridValueChanged; tilemap.OnLoaded += Tilemap_OnLoaded; } " Any ideas as to how i fix it?
@@CodeMonkeyUnity Thank you very much for your fast response. When i change it to: " public void SetTilemapVisual(TilemapVisual tilemapVisual) { TilemapVisual.SetGrid(Tilemap, Grid); } " I instead get the CS0119 error: "Tilemap"/"Grid
I want to implement a system in my game where a player can share their level that they build in-game to others as you said at 24:40. How can I achieve this? If anyone reading this has any idea about it Could you Please share it
@@CodeMonkeyUnity thanks for your reply. As an expert can u recommend what should I use in this particular situation (Just to share save files). I didnt find any useful information on internet.
I don't understand how do you make your tilemapvisual to be visible in play mode? I added the scripts and the visuals but still cannot see the black grid that you have in your video
Click on TilemapVisual object that you created in Object Hierarchy make sure the transform x and y are set to 0). The visual will then appear on top of the text grid when you do the click to change from None to Ground.
How would you go about placing a "tile" that is made out of multiple tiles? I was trying to make some sort of building system and was looking at placing entire rooms at the same time. Is it similar to your building system video but in 2d?
Just want to say: thanks for all the amazing videos! Took me a bit of tinkering to get everything right but that thought me so much more than any of the superficial yt videos other creators put out! Really love the fact that you don't use the unity standard functions for everything bc those are really limited when it comes to making something truly custom. One question though: currently the enum is implemented in the tilemap object and is then serialized in the tilemap visual. what would you suggest to do for multiple tilemap sets (i.e.: ground, props, walls etc)? create a custom tilemap object for each and everyone? Or forego the specific naming altogether and just name the enums after quadrants of same sized materials? Would rly appreciate your input on this!
Hi, I can't figure out how to this to work in a 3D environment / along the z axis and not the y? When I try to fiddle with the MeshUtils I can't get it to work. When I just use your code as is, then all the vertices "stand upright" but along the x and y axis. I have tried looking through your 2d to 3d video, Mesh and Grid Building (and through your project files) but I can't find a solution.
The solution: create a GetWorldPostion where you return z as the y value and pass that to the tile map. This will create the entire tile map as if it was 2D. Then simply rotate the x 90 deg in the editor on the tilemapVisual gameobject. This will create a tile map along the x,z and it works with the Mouse3D positions.
If you want more control over how the Tilemap works or is rendered or if you're already using the same Grid class to handle logic objects. If all you want is a basic visual Tilemap then the built-in works just fine.
Has anyone found a solution they like for determining and updating the transitions between different tilemap types, such as corners and edges of dirt up against path or grass tiles etc?
It won't help you when using the CodeMonkey tilemap class, but if you use the Unity tilemap tool and download/add the 2D Extras package, you can use Rule tiles to control auto-updating of tiles to respond to other nearby tiles.
Any solution to this? I have ideas .. but seems tricky... Perhaps multiple passes over the grid to generate the joining parts after by checking neighbours? Would love your help @Code Monkey
hey small problem when I click on the square its not changing from none to ground the part that it is not working is the SetTilemapSprite update fixed it
Hi @CodeMonkeyUnity! Can you please give some advice - how to get array of gridNode objects that you creating in test script? Or may be is exist different metod to get coordinates of all gridNode objects with isWalkable false value?
@Code Monkey I reviewed the video and the solution is that Grid creating GridNodes is a 2D-array in final form C# is new for me and I never seen 2D-arrays before, but now its clear) Thank you!
Thank you as for your tutorial. I have a question. How does this save load system work with assets? Like sprite, sound, prefab,... For example, I want to save and load sprite at each grid position, and when I save sprite, in the file it writes: "sprite":{"instanceID":29168} I am able to save and load normally. But I want to know if it's still the same when I build and run game in other devices? And does it apply to other assets as well?
Don't do that, it will break when you make a build or just change something in the editor that causes the InstanceID to regenerate. Instead save something like an Enum, then in your code you know which Enum represents which Sprite Or simply ask yourself do you need to save the Sprite at all? Perhaps it's part of something else. For example in my upcoming game Dinky Guardians I don't save a Prefab reference for the Dinky objects, instead I just save the dinky save data in a save data list, then when loading I already know it's a Dinky so I spawn the Dinky prefab
@@CodeMonkeyUnity thanks, I guess I will try a different approach. I wanted to load the sprite, then get the terrain reference from the sprite, since one terrain "Road" may have many different visuals, different sprites. I will try making enum for sprite to see if it works. Thanks for your advice.
So I think I’m going to use scriptable object to make a 3D dungeon. This should work pretty well as long as I don’t try to overlap pieces. The scriptable object will just save the prefab and height.
With my tilemap, some of the tile textures are off by one pixel on certain x values like x = 5 when cellSize is 10f. It seems like you had the same problem at 16:34. How did you fix it?
You mean the gaps? That's not being off by one pixel, it's just the automatic scaling of the texture. If the tile next to it is transparent that can happen. You can fix it by adding a tiny bit of padding on each tile texture.
@@CodeMonkeyUnity My textures where off by a pixel, I could see one pixel the previous texture in the atlas but only on certain columns. Im also using my own textures so that could be an issue but my current theory is a rounding error maybe.
I had the same problem. They were off by one pixel. The pixel count starts at 0, so the 64th pixel is pixel 63. Lower uv11Pixels.x by 1, e.g. 64 = 63, 128 = 127, 192 = 191 etc, etc.
Do you have any tips on how to apply bool values to all tiles in Unity's built in TileMap? I need to be able to set a tile with a Vector3Int to false after placing an object on it and setting that same tile to true if i sell the object placed on it. i've tried implementing this tutorial into my game but i couldnt figure out how to adapt it to work with my other functions. Any help is appreciated, and love your videos btw keep up the good work! :)
You mean store extra metadata in each Tilemap cell? I haven't used it much so not sure but maybe you can use tilemap.SetTile(); and make a custom class that extends TileBase
First of all, Code Monkey, Thanks for these awesome tutorials! Now I'd like to ask for some help: how can we add collision to these tiles? So that a 2d character would be able to stand on them. I tried adding a MeshCollider to the TilemapVisual object, then setting it's sharedMesh to our mesh, but it didn't work. Tested with a rigidbody + box collider 2d. At least point me at the right direction, please!
The official Unity Tilemap uses the Composite Collider component which lets you build a collider shape from various other shapes like BoxColliders. So in their implementation they add a BoxCollider to each tile which then is used by the Composite Collider.
@@CodeMonkeyUnity Thanks for the answer! I've looked into the Composite Collider and it seems to be a great solution! Looks like I need create BoxCollider2D's at the tile positions and it will all be handled by this thing. BUT I don't know if it's okay to have around a 1000 BoxCollider2D elements at the same time. It might have an impact on performance. (Talking about small tiles like in Duck Game, Terraria, Starbound, ...). It would probably be a good idea to create one BoxCollider2D for multiple tiles in a row as a way to optimize it.
I made it so that Unity generates all the BoxColliders when grid's loaded. Then I can just enable/disable them. Would need to change them for non-square shaped tiles though... This approach makes the game fly when you're setting regular square-shaped tiles. But it makes the game lag when it needs to load a big grid.
Would there be a reasonably ... "simple"? way of building colliders into this to prevent players going through walls? You might already have a video on it but I couldn't find anything
With any grid above 100*100 my width is staying stuck at 100 and height increasing only it seems. The mesh starts to look all distorted... any idea? Is this something like max vertices in a single mesh? I would have to create multiple mesh' at this point?
@@CodeMonkeyUnity 100x100 works fine, but above around 140~ the tiles start to look distorted/stretched/squished.... is this the point where I need to do chunking of 100*100 grids?
Not sure what you mean by that, your pathfinding logic should be separate from whatever you use for visuals, I covered pathfinding here unitycodemonkey.com/video.php?v=alU04hvz6L4 Which you could then combine with this tilemap Or you can look at how I did it in my TBS game unitycodemonkey.com/video.php?v=QDr_pjzedv0
@@CodeMonkeyUnity/videos In the method call for adding the quad, the position is set to [grid.GetWorldPosition(x, y) + quadSize * 0.5f] (without the brackets) but if I remove the * 0.5f from the end it makes it worse. This is exactly how it is in the video, though, so I don't know why it's different for me. It worked before I tried converting it into the tilemap visual object, so I don't understand why it's not working now. also, thanks for the quick reply. You're always good for that.
@@CodeMonkeyUnity Okay so if I set the cell size to 1 when I'm creating the tilemap, the gap disappears, but the visual is still half a cell width up and to the left of the cells I click on.
I've been following your great tutorials around the grid system & pathfinding, thank you so much for these tutorials! This is my next hurdle. My first question is should I make another grid entirely for this tilemap, or edit the PathNode object to include the sprite stuff and just have one grid of objects? Second question: I have my tilesets set out seperate for grass, dirt, water, with corners, sides, etc for each, I've imported them using normal unity tilemap and cutting them out with multiple on sprite editor and painting a scene just for testing purposes. But whats the best way to use these or turn them into what you've done? Should I have all water/grass/dirt on one big mesh renderer thing and use a vector to select which one? Is there a smart way to group them by grass/dirt/etc?
Making one grid to handle pathfinding and another separate one for the Tilemap is the better approach. Yes, with this system instead of slicing the sprites directly in the sprite editor, you just have one big texture and you "slice" it manually by defining the UV Vector2s for each tile.
@@CodeMonkeyUnity Thanks for your response. My tileset is quite complicated with around 6-10~ tiles for each dirt, grass, water with all the corners & joining edges etc. What would be the best method for making these joining sprites work? Some kind of multiple pass system? Where it draws the basics dirt/grass tile first then does a second pass checking neighbours and changing them accordingly? Edit: Do you think I would be better using unity 2d extras rule tiles? And generating a grid of rule tiles? I assume this will be more taxing, but if just loading a map on initial game load is that ok? OR will it cause gameplay lag?
@@pepsipwns666 Making edges join smoothly is quite tricky, you could have a second tilemap on top of the base one and on that second tilemap you would draw the edge smooth texture for all combinations which in that case would be quite a lot. Best approach would probably be multiple tilemaps for each type and make tiles for the transitions that fade away. So the water tilemap would be on the bottom, then the grass above that one with a grass tile that smoothly fades away which would look like it fades into the water.
@@CodeMonkeyUnity Thanks so much for your responses really giving me food for thought. To give you an idea of what I'm trying to achieve, its a top down game that I would like to randomly generate maps for (on initially loading your game, so doesn't have to be super quick). It doesn't need to be massively random just different enough... So your saying one tilemap per type (water, dirt, grass) etc, have it draw the water first (entire map covered in water?), then next tilemap draws dirt and can detect when its placing the last tile on x/y axis (the edge/corner tiles) and place edge sprites accordingly? How/where/when would I detect this? xD My other idea was to generate entire tilemap of water, then run another pass over the same tilemap changing 'chunks' of it to grass / dirt sections. Somehow 'saving' blocks of dirt/grass as preset areas of dirt/grass? and just blopping them about? If that makes sense... maybe if I could draw/save some chunks and have them spawn randomly ... Edit: Would I be saving myself a lot of hassle using the Unity 2d RuleTiles? I was hoping I could make use of the MeshRenderer system you have going... but it seems very difficult to involve smooth edges... I have managed to create a 1000*1000 randomly generated map with 'center' tiles of dirt/grass/water using your MeshRenderer system & UV00/UV11 to select sprites. I've thought about looping over it again with a SmoothMap() method, checking every tiles neighbours and seeing if they are a different tile from itself. Thats about as far as i've got... I'm not sure without writing tons of if statements how to detect when to replace the tile with a corner/side piece etc...
@@CodeMonkeyUnity Hey Dude, Thanks to your assistance I got it working, I have multiple layering tilemaps, all with rules for each joining tile on the edges etc. Its working great! I have another issue now though. I have been working on a 100*100 grid as I sorted this out, its perfect on that, but now when i changed to 200*200 for example, it seems like only the height is increasing and the width is still 100, anyway thats my assumption. The sprites look all squished and bizarre.
Could anyone share a working version of the .cs files or the project by a git repo? I am pretty sure that many visitors are unable to make it work after hours of researching... thank you!
@@CodeMonkeyUnity Should I directly set the walkability (this is a word?) of the Path Node in the method of changing the sprite of the tile (this I know how to do), or would it be more optimal to add the walkability method to the Trigger Grid Object Changed event (this I don't)?
The grid system is a bit complex so it depends on just how much of a beginner you are. If you're a beginner in Unity but not in C# you should be able to follow it. Check the playlist: ruclips.net/p/PLzDRvYVwl53uhO8yhqxcyjDImRjO9W722
I'm late, but to the others wondering the same: use Unity's built-in Tilemap Editor. As Code Monkey said, you likely don't - and _shouldn't_ - need more, as a beginner. It's a bit like, "If you need to ask [the price], you can't afford it." If you need to ask whether you can make this, you probably can't.
Hey CodeMonkey, I love your videos but sometimes you go a bit too fast and fix things without explaining and I think it is damaging for people like me who are trying to follow and are still relatively new to more advanced programming technics like lambda expression inside a constructor. For example, at 3:42 you change the constructor to use the TilemapObject but in the Func part, you write: (Grid g, int x, int y)=>new TilemapObject(grid, x, y)); instead of: (Grid g, int x, int y)=>new TilemapObject(g, x, y)); Notice the new TilemapObject argument, you only write "grid" in the video but you never mention or even show that you changed it to "g". I think it would be useful for everyone if you would show these things and explain a bit about common problems that can happen. Again, I love your videos and I hope you will appreciate my constructive criticism.
Hey CodeMonkey, I doubt you'll see this comment on a 4 year old video, but if you do, I'm, having trouble around the 8 minute mark. I've gone through this video and all your others several times, and I cant seem to figure out whats wrong. I'm not getting any errors, and all the data is changing correctly, however the mesh visual is not updating when clicked.
Add some Debug.Log to see what your code is doing. Is the code testing for Input running? Is SetTilemapSprite(); being called? Does it have the correct world position? Is the event being fired and then updating the visual?
Thanks for the reply! I actually realized that it was because I forgot to put "girdValueUV" in the "AddToMeshArrays" constructor at the end of "UpdateXMapVisual"@@CodeMonkeyUnity
@@CodeMonkeyUnity I'd like to second this question. I've been able to convert your grid class to handle isometric so far, tile picking is done, but the rendering is always bottom left to top right, instead of lowest y always on top. Any ideas? I asked in discord and CPU thought it was a uv problem, and I tried all his suggestions, nothing affects the way the mesh is drawn so far.
Hello CM! I was wondering if you could help me out. I am currently trying to do this Tutorial, but some things just dont want to work sadly. Following the tutorial step by step (Using your Grid system implemented in another video) i get an Error: "The non-generic type 'Grid' cannot be used with type arguments" This is spat out for multiple lines. Even if I copy your script from the UnityPackage provided for download (awesome btw!), it does not work. Trying to see if i have done something wrong i tried the "GameScene_Tilemap" which is in the tilemap assets folder where everything seems to be set up. Sadly the GameHandler and other Objects are missing Scripts. Since there is no README included i want to ask you if you know of a possible solution for this project. I really love the idea and would love to get it working. Keep up the great work CM!
Do you have the Grid class with generics? It was made in this video ruclips.net/video/8jrAWtI8RXg/видео.html Based on the error it sounds like you're using the built-in Unity Grid class instead This video was made a long time ago so I don't remember what's in the package, you can download the project files for the video that I just released which also uses the tilemap ruclips.net/video/n-R-mg0Svlo/видео.html
are you talking about horizontal scroller map? then put 3 grids side by side and slide them past the camera and when the left is off screen move the grid to the right side
I added the MeshUtils and followed everything but somehow all I get are a few yellow lines that are drawn over my grid randomly, I can't get the color from the black red yellow green texture to be applied in the grid, it's all transparent with None/Ground, and then those weird yellow lines. Any idea what I'm missing?
You should have as many classes as you need and no more. If something makes sense to be in its own class then it should be in its own class. Definitely don't put everything in one giant megaclass just because you want to avoid making more classes.
If anyone here hasn't heard of Rule Tiles (2D Extras) or Advanced Rule Tiles (asset store), you should learn about them because they make developer and player editing much easier. There's also lots of other tiles such as Animated and Random that are very useful.
Feel free to pause as much as you need, remember this is your learning journey, you're not competing with anyone but yourself. So take your time and just focus on learning
I don't like the packaging of the Util class with everything in every project. Tons of useless junk in packages. Also the Utils class is just a nice way to hide the fact Camera.main is used everywhere. This is not code you should use. Nor should you hide it in such a manner. Shame on you.
Feel free to write your own Utilities or just write the specific function you need, there's nothing special about my particular utilities, it's just so I don't write the same code over and over again. Shame on me for giving you free education? Ok then
Let's take our Grid System and make a Custom Tilemap with Saving and Loading so we can easily share Levels!
🎮 Play 7 Awesome Games (Action, Strategy, Management) and Help Support the Channel!
✅ Get the Game Bundle 67% off unitycodemonkey.com/gameBundle.php
So good
Your videos are always interesting but very hard to follow.
@@technofeeliak What couldn't you understand?
Why don't you just use Tiled? It's a free full blown application for building tilemaps.
Does it work witth unity 32 bits
I started following your channel and learned a lot of things I didn't know before. Thanks for all the detailed explanation which helps me to make my codes cleaner and easier to use.
I used to hard code all the logic and waste tons of time on unnecessary places, and now I start to use generic class and namespace to store methods that can be reused in other projects, plus some cool system features I learned from you.
It's great to find an awesome mentor like you on the internet.
I'm glad the videos helped you! Thanks!
I like this because it makes it much easier to build levels outside of Unity and just import them. As long as the JSON is in the right format, it'll just work.
I wonder how does the constructor at 3:46 does work? new TilemapObject(grid,x,y). At this time grid should be null. But with finishing this line the variable grid is overwritten. But the grid of the tilemap object is still pointing to null or how is the reference changed to the current grid? (I'm not sure about wording here.. )
Yes in there it should be new TilemapObject(g, x, y); to use the grid that is passed in the function and not the one in the field. I fixed it later but it gets cut off in the video because that line ended up way too big.
@@CodeMonkeyUnity Thanks for the comment though, following this tutorial with sleepy eyes because the learning value is so addictive, can't stop, but I had nullpointers at runtime. This was the problem. Thank you for this series by the way!
THX this comment helped me
Helped me too... this was driving me mad!
you should make a tutorial for isometric editors with elevation like hills and cliffs
I am having some difficulties at 5:07, when I tried out the script I got a nullreferenceexception: NullReferenceException: Object reference not set to an instance of an object
Tilemap+TilemapObject.SetTilemapSprite (Tilemap+TilemapObject+TilemapSprite tilemapSprite) (at Assets/Scripts/Tilemap.cs:48)
Tilemap.SetTilemapSprite (UnityEngine.Vector3 worldPosition, Tilemap+TilemapObject+TilemapSprite tilemapSprite) (at Assets/Scripts/Tilemap.cs:21)
Testing.Update () (at Assets/Scripts/Testing.cs:20)
And I can't for the life of me find where it comes from or how to fix it, help would be very much appreciated.
Update, I found the problem and made a makeshift solution but I would love to know what I did wrong because it is messy code, my problem is that the variables grid, x, and y are never assigned. I followed the tutorial step by step but I still must have missed somthing, thanks in advance.
I seem to have fixed the problem but I have no idea how...
You mean when creating the object? Those variables (grid, x, y) are set in the TilemapObject Constructor 3:02
@@CodeMonkeyUnity Thank you for responding so quickly! Yes, I think I forgot to set the grid value because that was the one that was null. I have finished the tutorial now but I have another problem. In between the tiles you can sometimes see lines, after some research I found that this was also a problem with the tilemap from unity. But all solutions I could find were to add pixel bleed (wich I am not sure how to add) or something else you can only do in the built in unity tilemap system. Do you have any idea how to fix these lines? Thank you so much for your help, I would not have been able to make my current project without you.
@@oystudios3052 Maybe try enabling Anti aliasing
When you create the constructor for the tilemap on the function part you send as a parameter grid. when it should be g.
Just letting other know since it took me a while to figure out.
Thanks for pointing it out! This was keeping me from going forward with the rest of the tutorial!
Hey Code Monkey - I'm interested to hear how you'd implement collisions into the tilemap? What if you were creating these tilemaps for players to explore and didnt want them running through wall tiles?
You could dynamically place a BoxCollider on each Tile position that you wanted to block.
Or you can just make another Grid System to hold the walkable state, and when the player moves ask the Grid System is the player can move there, so pretty much just like the Pathfinding system unitycodemonkey.com/video.php?v=alU04hvz6L4
@@CodeMonkeyUnity Thank you for such a quick reply, great ideas to consider 😄
Hello! I followed the steps until 4:50 but when I click on the Grid I get an exception (NullReferenceException: Object reference not set to an instance of an object
Testing.Update () (at Assets/Scripts/Testing.cs:17)) Any ideas?
Use Debug.Log to find what is null unitycodemonkey.com/video.php?v=5irv30-bTJw
if you're the same as me, he didn't show it in the video but in the Tilemap.cs, in the Tilemap constructor it needs to look like this:
grid = new Grid(width, height, cellSize, originPosition, (Grid g, int x, int y)=>new TilemapObject(g, x, y));
but in the video, he shows:
grid = new Grid(width, height, cellSize, originPosition, (Grid g, int x, int y)=>new TilemapObject(grid, x, y));
I'm not sure why he doesn't either explain the change or show in the video when he fixes it
Thanks!
I am tormented by vague doubts))) 😆 now I will try )
Please help me! Im writing the code at 1:41 ive checked literaly letter by letter, because it throws this:
error CS0308: The non-generic type 'Grid' cannot be used with type arguments
even when i downloaded the script (allone) from the project files, it still threw that error with some others.
You need to be using the Grid class made in the previous video, not the Unity one
ruclips.net/video/8jrAWtI8RXg/видео.html
@@CodeMonkeyUnity I'm using it and it still doesn't work :/
If, like me, you tend to use 'var' for most variables you may notice that your Ground and Path sprites aren't appearing correctly at around 16:05, then be sure you have the textureWidth and textureHeight variables declared as float. If not, they will become int values and will not contain the correct normalized values to select and display the sprites. It took me a bit to realize so hope this helps someone!
for me, the tiles are still displaced... any idea why ?
@@gabypounou I had this problem too, for me the issue was that the testing and tilemapvisual game objects were not at (0,0,0)
So, the one downside I can see with this implementation is that it only works at runtime. Is there a way to get similar functionality as the Unity tilemaps where you can build a level in the editor using this grid and then save the information with the scene? I've been trying to develop my own custom tilemap for this purpose and keep hitting brick walls. The Unity tilemaps are far too restrictive and the given tools are cumbersome to say the least.
Use the Attribute [ExecuteInEditMode]
Hello sir, I have a question about the function at 8:36. My TilemapVisual only works(visible) in scene view, and its location is at the top right corner of the screen. I download the project file and it seems like my code is basically the same as the project file. No error in Console either. I wonder what could be the problem here.
Doesn't sound like a problem, just move your camera
@@CodeMonkeyUnity Thanks for your response, I fix the problem. I have another question. I wonder why use Mesh in a 2D game and why not just use sprite and spriteRenderer?
A SpriteRenderer is a mesh, it's a flat quad, it just has a different name. You can put your Scene view into Wireframe mode to see the SpriteRenderer mesh
Quick questions:
What would be "better" a big mesh with all the sprites like in your tutorials or game objects with sprite renderers for each tile but with frustum culling to cut all the not visible tiles away and pool older ones?
The second question would be: How could I make smooth transitions between the tiles without drawing every transition, I want to lay the different tiles on top of each other.
A big mesh is a lot more performant. You can easily render 1000 tilemap quads, very difficult to get 1000 sprite renderers.
For smooth transitions you could draw the transitions as half transparent and put them on another tilemap on top of the base one.
@@CodeMonkeyUnity I almost thought so. But unfortunately you seem to have updated your utility classes, they are no longer congruent as in the video. So I wasn't able to follow the video with your utils.
Thanks for the quck anwser!
Nevermind, I just followed your other videos and created the utils by myself. Keep it up great work! Hope you stay safe and healthy tooo!
Me again! Puuh I'm stupid! You uploaded the MeshUtils but it was in a diffrent folder.. lmao.
hello/ i imported the Project files and Utilities from package in link, and when i run the game i just see gray screen. Can you help me? what can be a problem?
Hey CM do you reccomend using this grid system for inventory and quick item slots? Or should I just use the other tutorial you made?
Depends if you want the inventory to have "fixed" positions or not. I also covered a inventory with a grid system here ruclips.net/video/fxaDBE71UHA/видео.html
All the positions are fixed, whereas the other inventory has a virtual list so its unlimited.
Hey Monke, is there any chance of getting this to work with tilemap chunks and auto-tiling?
Hey, I just wanted to know if there any significant performance differences between your tilemap system VS Unity's built-in tilemap. I'm thinking about digging into some large scale procedural generation and this system seems practically perfect for that, but at the same time, I want to know if there's a significant performance difference between the two because then I'll have to choose the built-in system instead to decrease the performance hit (even though jerry rigging procedural tile generation in the built-in will be more difficult in the short term). Which do you think will be better from an optimization perspective? This or built-in?
I'm not sure since I never tested them side by side, I know this one is very performant since it's based on a single generated Mesh. Do a quick prototype to test it.
But unless you have really small tiles I doubt there would be any performance issues
@@CodeMonkeyUnity Ah...see you absolutely nailed the problem. I'm trying to do a tilemap similar to the game Starbound (give it a quick search if you haven't seen it). The problem is that each tile in Starbound is made up of four 'sub' tiles. That obviously means that each tile is 1/4th the size of a regular tile but consequently there are 4x as many tiles in the world (It results in very smooth terrain almost to the point where sometimes it doesn't even look like a block game). I'm sort of stuck on how to make a system like this which is actually efficient. I know it's likely possible through the use of some trickery but I'm drawing a blank. I know this is a bit off topic here, but how would you go about making a system like that, theoretically speaking?
@@dragonlord1935 Oh right for something like that you definitely need a very performant approach. The biggest challenge with that is more with keeping the state of all those nodes.
If I were making that I would probably use my system since it was made specifically to work on top of my Grid class which would be perfect for that type of game.
But with that scale you cant have just a single Grid for the whole world so one very tricky challenge is how to break the world into tons of separate grids and handle any connections between them.
@@CodeMonkeyUnity Do you by chance know of a way to only load parts of an object? So essentially, only loading the tiles around the player into active memory while keeping everything else in a "lazy" data only format? how would you only load parts of a mesh? Is that even possible?
@@dragonlord1935 You can't load parts of an object but you can organize your world into multiple objects as opposed to a single one.
So instead of a giant mesh occupying the whole entire world you split the world into say 100 meshes and only show the 5 nearest to the player.
Is there an easier method to fix the texture scaling than adding padding to all textures (see at 16:34)?
I assume by adding padding you mean editing each texture to have padding around it manually. Or is there some way to do that in code?
It appears that this happens when creating a material from the a texture. The edges are no longer pixel perfect but are blending into each other.
The main issue with the padding is that then you can't have pixel perfect tiles can you?
Never mind, my issue was setting the filter mode in the texture to anything but the correct one. Which is none ;) i.e. "Point (no filter)"
If you don't enable filtering and don't enable mip maps then yeah it will be pixel perfect, but that method is only suitable for pixel art games where the sprite size doesn't change.
If you have non-pixel art or zoom in/out then you really need that padding.
It's not shown in the video, but towards the end of adding saving and loading, you also need to add the Tilemap parameter to the calling of the SetGrid function.
It threw me off I didn't see it mentioned in the video, so I went back and rewatched it because I thought I missed something.
Wow, excellent! Any chance to do something similar with isometric tilemap?
Yeah Isometrics and Hex maps are something I'd like to look into.
@@CodeMonkeyUnity Pathfinding for isometric maps would be really nice.
@@CodeMonkeyUnity Hey CM i'm new here but would love to show you my implementation of your grid for use with isometric maps, i currently use it alongside the default tilemap to make both drawing previewing and interactions easier (i use procedurally generated terain, so all of the tiles come from code anyway)
Hello Code Monkey! I have a question. How do we convert this for a hex grid like in the latest videos? I tried to use this for my hex grid but the tilemap is not created correctly. Thanks in advance and have a nice day!
You would need to modify the Mesh code to create a Hex shape. I covered the math for getting all the hex points in this video unitycodemonkey.com/video.php?v=dqframYHfws
Then you would use those points as the vertices and set up the triangles
@@CodeMonkeyUnity Thank you, I will try to that!
I'm sorry for this noob question, but how do I start to make my Grid from the bottom of the camera?
Mine always starts at the center, and grows to the top right corner, so I can only a quarter of the grid.
It starts where the GameObject is located so just move that game object
Alternatively expand the script to add a simple Vector2 offset
Hello, I tried making the tilemap account for variable tile sizes, and therefore render the quads at different sizes, but it leads to overlaying quads. Is there a way to control the rendering order of individual quads? I tried sorting them by the Z axis and also by flipping the order in which they're updated, but nothing seems to work. As it stands right now tiles higher on the y axis always render in front. Thank you for all hypothetical responses!
The order is based on the vertices array. vertices[1] will show up on top of vertices[0]
The best approach might be to have a base tilemap, and then make a second one on top that only contains the tiles you want overlayed.
I'm having some trouble at 23:50.
When i save the scripts i get this error code:
"Assets\Scripts\Tilemap.cs(25,23): error CS7036: There is no argument given that corresponds to the required formal parameter 'grid' of 'TilemapVisual.SetGrid(Tilemap, Grid)'"
The problem seems to be with the "SetGrid" but i can't figure out how to fix it.
" public void SetTilemapVisual(TilemapVisual tilemapVisual) {
TilemapVisual.SetGrid(grid);
} "
The SetGrid function in TilemapVisual is:
" public void SetGrid(Tilemap tilemap, Grid grid) {
this.grid = grid;
UpdateHeatMapVisual();
grid.OnGridObjectChanged += Grid_OnGridValueChanged;
tilemap.OnLoaded += Tilemap_OnLoaded;
} "
Any ideas as to how i fix it?
Your SetGrid() function expects two arguments, a Tilemap and a Grid but you're only passing in a single grid parameter
@@CodeMonkeyUnity Thank you very much for your fast response.
When i change it to:
" public void SetTilemapVisual(TilemapVisual tilemapVisual) {
TilemapVisual.SetGrid(Tilemap, Grid);
} "
I instead get the CS0119 error: "Tilemap"/"Grid
I want to implement a system in my game where a player can share their level that they build in-game to others as you said at 24:40. How can I achieve this? If anyone reading this has any idea about it Could you Please share it
You have the save file stored, how you share that depends on what kind of server infrastructure you have.
@@CodeMonkeyUnity thanks for your reply. As an expert can u recommend what should I use in this particular situation (Just to share save files). I didnt find any useful information on internet.
I don't understand how do you make your tilemapvisual to be visible in play mode? I added the scripts and the visuals but still cannot see the black grid that you have in your video
Pause the game while running and look at the object, has the mesh been created?
Click on TilemapVisual object that you created in Object Hierarchy make sure the transform x and y are set to 0). The visual will then appear on top of the text grid when you do the click to change from None to Ground.
How would you go about placing a "tile" that is made out of multiple tiles? I was trying to make some sort of building system and was looking at placing entire rooms at the same time. Is it similar to your building system video but in 2d?
Saving my spot 9:01
you’re an amazing person
Just want to say: thanks for all the amazing videos!
Took me a bit of tinkering to get everything right but that thought me so much more than any of the superficial yt videos other creators put out!
Really love the fact that you don't use the unity standard functions for everything bc those are really limited when it comes to making something truly custom.
One question though: currently the enum is implemented in the tilemap object and is then serialized in the tilemap visual. what would you suggest to do for multiple tilemap sets (i.e.: ground, props, walls etc)? create a custom tilemap object for each and everyone? Or forego the specific naming altogether and just name the enums after quadrants of same sized materials? Would rly appreciate your input on this!
Hi, I can't figure out how to this to work in a 3D environment / along the z axis and not the y? When I try to fiddle with the MeshUtils I can't get it to work. When I just use your code as is, then all the vertices "stand upright" but along the x and y axis.
I have tried looking through your 2d to 3d video, Mesh and Grid Building (and through your project files) but I can't find a solution.
The solution:
create a GetWorldPostion where you return z as the y value and pass that to the tile map. This will create the entire tile map as if it was 2D. Then simply rotate the x 90 deg in the editor on the tilemapVisual gameobject. This will create a tile map along the x,z and it works with the Mouse3D positions.
Can you make a tutorial about streamable enormous grid map? 10000x10000 or combine with ECS ? Thanks !
Is there a reason you would use this over unity's built-in tilemap system?
If you want more control over how the Tilemap works or is rendered or if you're already using the same Grid class to handle logic objects.
If all you want is a basic visual Tilemap then the built-in works just fine.
@@CodeMonkeyUnity Thanks for the response! :P
Has anyone found a solution they like for determining and updating the transitions between different tilemap types, such as corners and edges of dirt up against path or grass tiles etc?
It won't help you when using the CodeMonkey tilemap class, but if you use the Unity tilemap tool and download/add the 2D Extras package, you can use Rule tiles to control auto-updating of tiles to respond to other nearby tiles.
Any solution to this? I have ideas .. but seems tricky... Perhaps multiple passes over the grid to generate the joining parts after by checking neighbours? Would love your help @Code Monkey
hey small problem when I click on the square its not changing from none to ground
the part that it is not working is the SetTilemapSprite
update fixed it
Hi @CodeMonkeyUnity!
Can you please give some advice - how to get array of gridNode objects that you creating in test script?
Or may be is exist different metod to get coordinates of all gridNode objects with isWalkable false value?
What part of the video are you referring to? The Grid class? That was made in the previous video unitycodemonkey.com/video.php?v=waEsGu--9P8
@Code Monkey I reviewed the video and the solution is that Grid creating GridNodes is a 2D-array in final form
C# is new for me and I never seen 2D-arrays before, but now its clear)
Thank you!
Please tell me that he speeds up his video while coding? Lol, otherwise I need to go crawl into a corner and cry.
I write fast but not that fast!
Practice!
Thank you as for your tutorial.
I have a question. How does this save load system work with assets? Like sprite, sound, prefab,...
For example, I want to save and load sprite at each grid position, and when I save sprite, in the file it writes:
"sprite":{"instanceID":29168}
I am able to save and load normally. But I want to know if it's still the same when I build and run game in other devices? And does it apply to other assets as well?
Don't do that, it will break when you make a build or just change something in the editor that causes the InstanceID to regenerate.
Instead save something like an Enum, then in your code you know which Enum represents which Sprite
Or simply ask yourself do you need to save the Sprite at all? Perhaps it's part of something else.
For example in my upcoming game Dinky Guardians I don't save a Prefab reference for the Dinky objects, instead I just save the dinky save data in a save data list, then when loading I already know it's a Dinky so I spawn the Dinky prefab
@@CodeMonkeyUnity thanks, I guess I will try a different approach. I wanted to load the sprite, then get the terrain reference from the sprite, since one terrain "Road" may have many different visuals, different sprites.
I will try making enum for sprite to see if it works. Thanks for your advice.
So I think I’m going to use scriptable object to make a 3D dungeon. This should work pretty well as long as I don’t try to overlap pieces. The scriptable object will just save the prefab and height.
My tilemap cells are larger in size in compare to grid cells size. Any idea why that happens.
With my tilemap, some of the tile textures are off by one pixel on certain x values like x = 5 when cellSize is 10f. It seems like you had the same problem at 16:34. How did you fix it?
You mean the gaps? That's not being off by one pixel, it's just the automatic scaling of the texture. If the tile next to it is transparent that can happen.
You can fix it by adding a tiny bit of padding on each tile texture.
@@CodeMonkeyUnity My textures where off by a pixel, I could see one pixel the previous texture in the atlas but only on certain columns. Im also using my own textures so that could be an issue but my current theory is a rounding error maybe.
I had the same problem. They were off by one pixel. The pixel count starts at 0, so the 64th pixel is pixel 63. Lower uv11Pixels.x by 1, e.g. 64 = 63, 128 = 127, 192 = 191 etc, etc.
Do you have any tips on how to apply bool values to all tiles in Unity's built in TileMap? I need to be able to set a tile with a Vector3Int to false after placing an object on it and setting that same tile to true if i sell the object placed on it. i've tried implementing this tutorial into my game but i couldnt figure out how to adapt it to work with my other functions.
Any help is appreciated, and love your videos btw keep up the good work! :)
You mean store extra metadata in each Tilemap cell? I haven't used it much so not sure but maybe you can use tilemap.SetTile(); and make a custom class that extends TileBase
@@CodeMonkeyUnity Thanks for putting me on the right track! Managed to find a solution for it! :)
First of all, Code Monkey, Thanks for these awesome tutorials!
Now I'd like to ask for some help: how can we add collision to these tiles? So that a 2d character would be able to stand on them.
I tried adding a MeshCollider to the TilemapVisual object, then setting it's sharedMesh to our mesh, but it didn't work. Tested with a rigidbody + box collider 2d.
At least point me at the right direction, please!
The official Unity Tilemap uses the Composite Collider component which lets you build a collider shape from various other shapes like BoxColliders. So in their implementation they add a BoxCollider to each tile which then is used by the Composite Collider.
@@CodeMonkeyUnity Thanks for the answer! I've looked into the Composite Collider and it seems to be a great solution!
Looks like I need create BoxCollider2D's at the tile positions and it will all be handled by this thing.
BUT I don't know if it's okay to have around a 1000 BoxCollider2D elements at the same time. It might have an impact on performance. (Talking about small tiles like in Duck Game, Terraria, Starbound, ...). It would probably be a good idea to create one BoxCollider2D for multiple tiles in a row as a way to optimize it.
I made it so that Unity generates all the BoxColliders when grid's loaded. Then I can just enable/disable them. Would need to change them for non-square shaped tiles though...
This approach makes the game fly when you're setting regular square-shaped tiles. But it makes the game lag when it needs to load a big grid.
I've had this same question for 4 hours
Would there be a reasonably ... "simple"? way of building colliders into this to prevent players going through walls? You might already have a video on it but I couldn't find anything
You could add BoxCollider2D's to each tile and combine them with a Composite Collider
That's exactly what the built-in Tilemap does
With any grid above 100*100 my width is staying stuck at 100 and height increasing only it seems. The mesh starts to look all distorted... any idea? Is this something like max vertices in a single mesh? I would have to create multiple mesh' at this point?
The mesh limit is 65k vertices so 100x100 should work with 40k vertices
@@CodeMonkeyUnity 100x100 works fine, but above around 140~ the tiles start to look distorted/stretched/squished.... is this the point where I need to do chunking of 100*100 grids?
@@pepsipwns666 140x140 requires 78k vertices which is above the 65k limit so yes at that point you need to handle multiple chunks
How can i do this with job system ?
i ended up creating my own system, but the tutorial help me anyways :D
Sir how can i use this base class to use in my 2d farming game.
Hi Code Monkey! i want to do a PathFinder with Custom Tilemap, can i do that? did you cover it?
Not sure what you mean by that, your pathfinding logic should be separate from whatever you use for visuals, I covered pathfinding here unitycodemonkey.com/video.php?v=alU04hvz6L4
Which you could then combine with this tilemap
Or you can look at how I did it in my TBS game unitycodemonkey.com/video.php?v=QDr_pjzedv0
Can the rule tiles be applied to this custom tilemap?
You mean Unity Rule Tiles? Those use a completely different system so not really, unless you write the code to make it compatible
and it will work like saving the level and the game itself with loading the game back?
Yes
My visual grid has huge gaps in between each cell but my code is exactly how it is in the video. How do I fix this????
Check how you are creating the mesh for each grid position, maybe you're using half the cellSize
@@CodeMonkeyUnity/videos In the method call for adding the quad, the position is set to [grid.GetWorldPosition(x, y) + quadSize * 0.5f] (without the brackets) but if I remove the * 0.5f from the end it makes it worse. This is exactly how it is in the video, though, so I don't know why it's different for me.
It worked before I tried converting it into the tilemap visual object, so I don't understand why it's not working now.
also, thanks for the quick reply. You're always good for that.
@@CodeMonkeyUnity Okay so if I set the cell size to 1 when I'm creating the tilemap, the gap disappears, but the visual is still half a cell width up and to the left of the cells I click on.
My game 2D is love date i learn with this channel
Another Awesome tut......sir please make a video on Damage Indicator for FPS/TPS
I've done a video on Damage Popups, to make it work in 3D just rotate the Text towards the camera
ruclips.net/video/iD1_JczQcFY/видео.html
@@CodeMonkeyUnity yes sir i view this its just popup.....i want to detect from which direction the player getting shots...just like pubg.....
Unity's Tilemap not having a simple hide/unhide feature for the tiles was ridiculous. I guess i'm building my own now :(
I've been following your great tutorials around the grid system & pathfinding, thank you so much for these tutorials! This is my next hurdle.
My first question is should I make another grid entirely for this tilemap, or edit the PathNode object to include the sprite stuff and just have one grid of objects?
Second question: I have my tilesets set out seperate for grass, dirt, water, with corners, sides, etc for each, I've imported them using normal unity tilemap and cutting them out with multiple on sprite editor and painting a scene just for testing purposes. But whats the best way to use these or turn them into what you've done? Should I have all water/grass/dirt on one big mesh renderer thing and use a vector to select which one? Is there a smart way to group them by grass/dirt/etc?
Making one grid to handle pathfinding and another separate one for the Tilemap is the better approach.
Yes, with this system instead of slicing the sprites directly in the sprite editor, you just have one big texture and you "slice" it manually by defining the UV Vector2s for each tile.
@@CodeMonkeyUnity Thanks for your response. My tileset is quite complicated with around 6-10~ tiles for each dirt, grass, water with all the corners & joining edges etc. What would be the best method for making these joining sprites work? Some kind of multiple pass system? Where it draws the basics dirt/grass tile first then does a second pass checking neighbours and changing them accordingly?
Edit: Do you think I would be better using unity 2d extras rule tiles? And generating a grid of rule tiles? I assume this will be more taxing, but if just loading a map on initial game load is that ok? OR will it cause gameplay lag?
@@pepsipwns666 Making edges join smoothly is quite tricky, you could have a second tilemap on top of the base one and on that second tilemap you would draw the edge smooth texture for all combinations which in that case would be quite a lot.
Best approach would probably be multiple tilemaps for each type and make tiles for the transitions that fade away. So the water tilemap would be on the bottom, then the grass above that one with a grass tile that smoothly fades away which would look like it fades into the water.
@@CodeMonkeyUnity Thanks so much for your responses really giving me food for thought. To give you an idea of what I'm trying to achieve, its a top down game that I would like to randomly generate maps for (on initially loading your game, so doesn't have to be super quick). It doesn't need to be massively random just different enough... So your saying one tilemap per type (water, dirt, grass) etc, have it draw the water first (entire map covered in water?), then next tilemap draws dirt and can detect when its placing the last tile on x/y axis (the edge/corner tiles) and place edge sprites accordingly? How/where/when would I detect this? xD
My other idea was to generate entire tilemap of water, then run another pass over the same tilemap changing 'chunks' of it to grass / dirt sections. Somehow 'saving' blocks of dirt/grass as preset areas of dirt/grass? and just blopping them about? If that makes sense... maybe if I could draw/save some chunks and have them spawn randomly ...
Edit: Would I be saving myself a lot of hassle using the Unity 2d RuleTiles? I was hoping I could make use of the MeshRenderer system you have going... but it seems very difficult to involve smooth edges... I have managed to create a 1000*1000 randomly generated map with 'center' tiles of dirt/grass/water using your MeshRenderer system & UV00/UV11 to select sprites. I've thought about looping over it again with a SmoothMap() method, checking every tiles neighbours and seeing if they are a different tile from itself. Thats about as far as i've got... I'm not sure without writing tons of if statements how to detect when to replace the tile with a corner/side piece etc...
@@CodeMonkeyUnity Hey Dude, Thanks to your assistance I got it working, I have multiple layering tilemaps, all with rules for each joining tile on the edges etc. Its working great! I have another issue now though. I have been working on a 100*100 grid as I sorted this out, its perfect on that, but now when i changed to 200*200 for example, it seems like only the height is increasing and the width is still 100, anyway thats my assumption. The sprites look all squished and bizarre.
Could anyone share a working version of the .cs files or the project by a git repo? I am pretty sure that many visitors are unable to make it work after hours of researching... thank you!
There's a link in the description to download the project files
@@CodeMonkeyUnity My bad. Thank you soooo much!
Can i combine this with the a* pathfinding?
Sure, if they both use the same underlying grid then its very simple to make a tile for a rock and make it unwalkable on the pathfinding grid
@@CodeMonkeyUnity Should I directly set the walkability (this is a word?) of the Path Node in the method of changing the sprite of the tile (this I know how to do), or would it be more optimal to add the walkability method to the Trigger Grid Object Changed event (this I don't)?
@@umarmohd3511 You should probably keep the Visual and Logic separate and have another script that listens to visual changes and applies path changes.
can the begginner like me if i follow steps in this video make level editor in 2d platform game?
The grid system is a bit complex so it depends on just how much of a beginner you are. If you're a beginner in Unity but not in C# you should be able to follow it.
Check the playlist: ruclips.net/p/PLzDRvYVwl53uhO8yhqxcyjDImRjO9W722
I'm late, but to the others wondering the same: use Unity's built-in Tilemap Editor. As Code Monkey said, you likely don't - and _shouldn't_ - need more, as a beginner.
It's a bit like, "If you need to ask [the price], you can't afford it." If you need to ask whether you can make this, you probably can't.
Hey CodeMonkey, I love your videos but sometimes you go a bit too fast and fix things without explaining and I think it is damaging for people like me who are trying to follow and are still relatively new to more advanced programming technics like lambda expression inside a constructor.
For example, at 3:42 you change the constructor to use the TilemapObject but in the Func part, you write:
(Grid g, int x, int y)=>new TilemapObject(grid, x, y));
instead of:
(Grid g, int x, int y)=>new TilemapObject(g, x, y));
Notice the new TilemapObject argument, you only write "grid" in the video but you never mention or even show that you changed it to "g". I think it would be useful for everyone if you would show these things and explain a bit about common problems that can happen.
Again, I love your videos and I hope you will appreciate my constructive criticism.
Hey CodeMonkey, I doubt you'll see this comment on a 4 year old video, but if you do, I'm, having trouble around the 8 minute mark. I've gone through this video and all your others several times, and I cant seem to figure out whats wrong. I'm not getting any errors, and all the data is changing correctly, however the mesh visual is not updating when clicked.
Add some Debug.Log to see what your code is doing. Is the code testing for Input running? Is SetTilemapSprite(); being called? Does it have the correct world position? Is the event being fired and then updating the visual?
Thanks for the reply! I actually realized that it was because I forgot to put "girdValueUV" in the "AddToMeshArrays" constructor at the end of "UpdateXMapVisual"@@CodeMonkeyUnity
Thanks for the reply, turns out I forgot to swap "gridValueUV" into the "AddToMeshArrays" constructor@@CodeMonkeyUnity
How can I use it isometrically?
If its just rotated then you can leave the grid as rectangular and just move the camera.
Otherwise you just have to do some math
@@CodeMonkeyUnity the sprites are rotated (actually drawn isometrically )
@@CodeMonkeyUnity I'd like to second this question. I've been able to convert your grid class to handle isometric so far, tile picking is done, but the rendering is always bottom left to top right, instead of lowest y always on top. Any ideas? I asked in discord and CPU thought it was a uv problem, and I tried all his suggestions, nothing affects the way the mesh is drawn so far.
It is possible to do in 3d?
Not sure what you mean by 3D, this is a flat mesh you can orient it in any way
First my guy
Hello CM!
I was wondering if you could help me out.
I am currently trying to do this Tutorial, but some things just dont want to work sadly.
Following the tutorial step by step (Using your Grid system implemented in another video) i get an Error:
"The non-generic type 'Grid' cannot be used with type arguments"
This is spat out for multiple lines.
Even if I copy your script from the UnityPackage provided for download (awesome btw!), it does not work.
Trying to see if i have done something wrong i tried the "GameScene_Tilemap" which is in the tilemap assets folder where everything seems to be set up. Sadly the GameHandler and other Objects are missing Scripts.
Since there is no README included i want to ask you if you know of a possible solution for this project. I really love the idea and would love to get it working.
Keep up the great work CM!
Do you have the Grid class with generics? It was made in this video ruclips.net/video/8jrAWtI8RXg/видео.html
Based on the error it sounds like you're using the built-in Unity Grid class instead
This video was made a long time ago so I don't remember what's in the package, you can download the project files for the video that I just released which also uses the tilemap ruclips.net/video/n-R-mg0Svlo/видео.html
Someone has a clue about how to use this system with a Tileset? because this system is a little bit hard to use with 2d games with a side look :(
are you talking about horizontal scroller map? then put 3 grids side by side and slide them past the camera and when the left is off screen move the grid to the right side
I added the MeshUtils and followed everything but somehow all I get are a few yellow lines that are drawn over my grid randomly, I can't get the color from the black red yellow green texture to be applied in the grid, it's all transparent with None/Ground, and then those weird yellow lines. Any idea what I'm missing?
What part of your texture is yellow? Sounds like a UV issue, add some Debug.Log's to see what UV's you're using.
OnChangeTile(TypeTile typeTile) => if(typeTile != TypeTile.None){ addCollider();} or something like that, :D
I have one best system capacity to save Transform config and Scriptable configs
I LOVE YOU
NICEEE!
great!
vektör graphics video please
vektor graphics for ui video please
Nice
Why o why would you have so many classes inside of other classes? Its bad practise to have a class within a class
You should have as many classes as you need and no more. If something makes sense to be in its own class then it should be in its own class.
Definitely don't put everything in one giant megaclass just because you want to avoid making more classes.
If anyone here hasn't heard of Rule Tiles (2D Extras) or Advanced Rule Tiles (asset store), you should learn about them because they make developer and player editing much easier. There's also lots of other tiles such as Animated and Random that are very useful.
Hi
Oh man so many nice features and I cant get it done for hexes whyyy :-(
Dude slow down, wtf
Feel free to pause as much as you need, remember this is your learning journey, you're not competing with anyone but yourself. So take your time and just focus on learning
I don't like the packaging of the Util class with everything in every project. Tons of useless junk in packages. Also the Utils class is just a nice way to hide the fact Camera.main is used everywhere. This is not code you should use. Nor should you hide it in such a manner. Shame on you.
Feel free to write your own Utilities or just write the specific function you need, there's nothing special about my particular utilities, it's just so I don't write the same code over and over again.
Shame on me for giving you free education? Ok then
@@CodeMonkeyUnity I think packing shortcut functions in a class is a good way to save time and typing. Bravo!