Edit, Save And Load Unity Tilemaps At Runtime!

Video Statistics and Information

Video
Captions Word Cloud
Reddit Comments
Captions
hey everyone in this video i'm going to show you how you can create a level editor using unity style map system well actually i'm not going to show this entire thing yet because this is going to be a tutorial series in this video i'm going to show you how you can place stylus to the tile map using your cursor and how you can save those tile maps let's go i've prepared a completely new unity project which only has a few things in it i imported my tools so i can create tiles very quickly and i also imported some tile maps so we have something to work with everything else is completely as it is when you create a new project so let's just set up a few things let's create a tile map which you can do by going to the hierarchy right clicking do the object tile map and just regular rectangular style map this will create a grid and a tile map for us and let's also create a tile palette which you can go by going to window 2d opening the tile palette window and then clicking on create new palettes now we can take our prepared sprites and just drag them into the tile palette now we have simple sprites which we can take and draw with them but let's make things a little bit easier for us and let's create a rule so i've created a really easy to use tool which you can use to create rule types really quickly and if you want to know how it's made you can check out my tutorial on it link in the description or the top right corner so let's go windows editor windows and router generator and as you can see it created a perfectly working rule time which we can use okay so we have everything set up so we can start creating the actual level editor let's create a new folder for our scripts and let's create a few scripts we'll create a level editor script and let's also create a level manager script let's open it before writing the code we need to add some using text first we need unity engineered tile maps let's create a few variables in order to edit a tile map we need a reference to the style map so let's create a reference to the tile map plyo map current tile map and we also need some tiles to place on the tile map so let's add just one tile for now it should be the current style we're using the tile base so we can put any kind of tile in it so let's say you have just a regular tile if you're doing this you will not be able to assign rule tiles for example so we need to use style base and we also need a reference to the camera to calculate the mouse position so we pretty much have every reference we need so let's create a play style function this function will take in the position where we need to place the tile which uses vector-free ins to place a tile on a tile map we just need to write the tile map so current style map the set tile which takes in the position so it's just pose and the tile which is the current style placing tiles is that easy but we need to figure out the position somehow so let's write something in the update function based on the camera and the mouse position we return the coordinates of the tile which we are hovering over so let's create a temporary vector free end calls pose and this pose variable will be equal to our current tutorial map dot verb to cell which converts the word position to a cell position and we'll get that word position using the cam screen to birth point and just using input mouse position we have our position now so let's make so if we left click we place down the tiles if input get mouse button 0 which is the left one play style at our position so we can now test this script so let's go to the grid for example and let's add the level the javascript we need to assign the variables so timer goes to the tile map main camera goes to the camera and for the current tile let's choose our rule tile and we can press play and test this and as you can see when i'm left clicking it places dump tiles which also i automatically update as these are rule tiles but yeah i cannot delete them so let's change that let's go back to visual studio and let's add another function which is going to be called delete tail deleting the tile is basically the same as placing one but instead of placing a tile we'll just place null let's also add an if statement if we are right-clicking let's delete the tile so we can now get rid of these styles by right-clicking and we can place new ones okay so let's say we created a level this is a pretty primitive level and we want to save it well we cannot because if we exit the game it's gonna get lost so let's make the saving let's open the level manager and also add the using tags as this is a manager we only want one of this script so we can make it a singleton so let's create public static level manager instance and in the awake function if instance equals null instance equals this and if they and if there's already an instance let's destroy this script let's set the reference to the title map as well here and let's create a save level function now in order to save the level we need some type of object that we can save it into so let's create a separate class in this script called level data and this level data will contain some variables so let's think of what we need in order to load or save levels well we need the tiles and it's going to be a list because we have multiple tiles in a level tiles and we need their positions so let's create the vector free int list called poses so let's go back to the sailor function we need to get all of the tiles from the tile map and somehow write those styles to a level data so we need to loop through every tile on the tile map and write it to level data so if we go to tile map there's not a function called load or tiles or get all styles there's only get style so we need to figure out where are the titles because the time map can be like infinite long luckily there is a function called time map that bounce which we return the area where tiles are placed so let's call this cell bounce function and save its output to a bounce end and now we can take this bounce and loop through every tile in it so for the x-axis we will start from bounds that minimum that x and we loop until we reach bounds that max dot x we'll do the same thing for the y axis before looping let's create a new level data level data equals new level data and i forgot to set these styles to new lists so they're basically empty and we can work with them so we can now use these x and y coordinates to get titles from the title map so tilemap dot get tile new vector free int x y and the grids have no left so the x coordinate will be zero we will save this style to a temporary tile and to not save empty spaces we'll check if the temperature is not equal to null and if it's not null we can add the position and the title to the level data now we just need to save it somewhere and the best way to save it is to convert it to a json and save that to somewhere on our pc to convert this level data to json files we can just use the built-in json utility string json equals json utility that took json and we just put the level data there there's also an option to pretty print so it will look like this not everything in one line let's enable it so we can look at that later and now we can vary this json to a text file you'll need the system that io namespace for that so file dot write all text and here we need the path and the contents the contents will be the json file and our path will be application dot data path plus slash our file name so let's call it test level we'll change this later to variables so we can input our level's name and that json we need to trigger this function somehow so in the update function let's assign this function to a key like every program you usually save by pressing ctrl s so let's do the same thing here so if input get key key code control and input key down key code s save level so when we are pressing the s while holding the control key we save the level unit is gonna complain that we cannot save during play mode so let's change the s to a let's also set the debug.load to let us know that level was saved and we can now test it okay we can now add manager object and add the level manager to it and let's also assign the tile map if we draw something and press ctrl a it's gonna let us zone at the level was saved so we can now exit play mode my mouse battery just died after we exit play mode and let you need to refresh our stuff test level file will appear this is our save level of course so let's create something to load it go back to the level manager script and create the load level function which will load the file at the exact same path as we save this string json equals the file the reverse of the write all text so read all text and the path i'm gonna just copy we can now convert this json file to a level data object so level data data equals json utility from json the reverse of the json function type of level data and the json file to make sure the time map is empty we can do tilemap that's clear all tiles and then we can loop through every tile and place it so four and zero data dot tiles count because it's a list we could also do pauses the account is the same so it doesn't matter and we can do tile map set style data that poses y and data dot titles y let's make a function to load the level let's use ctrl o for that as open loop level and we can go back to unity so if i press ctrl l it works until you close down your program so there's a problem with that it stores instance ids which change every time you open your game or in this case the unity editor so let's just close down the unity editor okay unity is open now i'm going to create a duplicate of this so we can compare it once we save it again and let's play the game so let's draw something and press ctrl l it cleared the screen so we know it's working but it didn't place anything we can see the instant side they changed that's the reason why we can store unity specific objects in json files because unity stores them as instances which change every time you reopen unity or the game so let's fix this let's use a data type which is always the same so the basic data types like bool and float these stay the same and are supported by json files so let's change the tile base to strings now we cannot use strings to play styles with your studio highlights this error we need to create an id system for the game so let's go back to unity to the scripts folder and create a new script which can be called custom tile this is going to be a scriptable object which we can create using the asset menu this style will store two things first of all the tile and its name or id in the level manager we need to store every custom tile which we'll be using in our level editor so list of custom tiles tiles should be a public list and it's equals to a fresh custom title list so we can just add temp to the list now we need to search through this styles array and if the tile is in the tiles array we need to add its id to the tiles so instead of checking if temp is equals to null we will check if we can find the tile in the tiles array and if it has an id so string temp id equals to tiles that find t where d the title equals temporary tile this will return a custom tile so let's change the custom tile and now we can check if time tile is equals to null and if it's not null we can use its id to save it in the load method we can do the opposite so instead of searching for the title we'll search for the name so titles that find t where t that name equals to our save style in the data that style and in theory it should work now so first let's create a new dirt tight is going to be dirt and the tile is going to be dirt rule tile and we can add this style to the tiles array in the level manager so we can draw press ctrl a to save the level exit refresh unity and now we can see instead of instance ids it stores strings which is actually more space efficient than the instance ids we could also change the poses to two separate integers poses x and pauses y and we also need to separate them so in the set tier method we use new vector3 int poses x pauses y and zero and here we just need to add the x and the y separately suppose this x that add x and pauses to try at y so let's draw something save it refresh unity and because it doesn't have to explain the x y and z property every time it's gonna be shorter as you can see if we enter play mode again and press ctrl l it's gonna load the same with no problem that's it for this user in the next part we'll implement the ability to place multiple types of styles and we'll also create an inventory to search and select those styles until then see ya
Info
Channel: Vinark117
Views: 155
Rating: undefined out of 5
Keywords: Unity Level Editor, Tutorial, Unity tutorial, Unity Tilemaps, Save Tilemaps, Load Tilemaps, Unity Save Tilemaps, Unity Load Tilemaps, Level Editor, Level editor Tutorial, Gamedev, Game Development, Unity, Tilemap, Tilemaps, Edit Tilemaps, Runtime
Id: snUe2oa_iM0
Channel Id: undefined
Length: 17min 3sec (1023 seconds)
Published: Sun Nov 28 2021
Related Videos
Note
Please note that this website is currently a work in progress! Lots of interesting data and statistics to come.