Tile Map auto generation in Unity - TUTORIAL

Video Statistics and Information

Video
Captions Word Cloud
Reddit Comments
Captions
hello everybody Daniel Hoffman here with a detailed look on 2d time map features in unities beta version 2,073 in this video I will show you an easy way how procedural time map generation where you produced time apps of course stay fully editable and you can even save them as a prefab for for your own games at first we will take a quick look at the game I've written behind the scripts then write some small functions so you can test out different parameters and compare the outcome sounds good let's get started the algorithm we use today for generating time at terrain is based on Conway's Game of Life it was originally developed for simulating organic like structures and since its first application has been used in a lot of different fields during science history all starts by placing fill cells on a grid which are labeled as alive and the non fill cells are dead this is done by an initial chance for each cell to become alive or not after that we will conduct a neighbor check for each cell on the grid so the number of neighbors a living cell has is compared to its death limit and in case this number is lower than the parameter the cell will die in the next run of the simulation meaning getting not filled on the other hand a cell which is not yet filled on the map but has more neighbors than its birth limit demands will become a life meaning filled and the next run of the simulation the more often you run this simple algorithm across your time app the more it will develop from a random field chaos to a more and more organic like structure let's start to fill this gray theory with some life by creating a new scripts folder under the assets folder and within this folder we will create a new C sharp script I call mine tire automata because this is perhaps the wrong pronunciation but the right name for the whole machine behind this and open it up in visual studio of course the first thing I do here is to delete the start method after that I go to the top and we declare the Unity engine tile Maps namespace and the unity editor namespace as well as we will need both of them during our scripting and then we go on and declare some fields which are necessary for running the algorithm the first is the initial chance of a tile on the map to be alive we'll place a range selector above which will be visible and the inspector later on with values between 0% and 100 of course zero would result in well not really useful simulation the next parameters are the birth limit an integer and a death limit and here as well we give ourselves the opportunity to choose inside a range between one and eight which seems useful to me and then we have our next parameter which is a number of repetitions we want to run the algorithm we give it a range selector as will 1 and 10 as a maximum should be the values of choice and well a simple integer for counting the number of save files later on which I set to zero now and another integer array which will store our terrain tiles having one meaning they are alive or having a zero meaning they are not filled so then we need 2 vector 3 and 4 time app size and of course a time app class or two better one for the terrain and one for lava or water or whatever you want to put beneath so one we call our top map the other one will be the bottom map and two types of tiles we need to place on the maps the top tile and the bottom tile would be suitable names I think and then we have two integers representing the width and height of our time map we take from our time app size vector 3 int so yeah we have two short variable names we can loop through when we write our main and sub functions our first function is the main function I call it 2 sim for 2 simulation and it will take in an integer which is the number of repetitions and then we want to call a small sub function I call it clear maps which will take in a bool so if the bool is false this sub function we write now should only clear the time apps so that there are no tiles visible on the screen but our integer array of filled and non filled tiles should stay like it is and if the bubu takes in true value then we clear this integer area as well because it means we want to start a new simulation and not take the next step for an already existing time map both Maps heaven generic function called clear all tiles which we call here inside the sub function and as I said before if the bull named complete is true we want to set our integer array to null and now we start to fill our integer array for the first time with random tiles but before that we assign our time app size x and y value to from the vector 3 ends in the inspector to our width and height integers so we can easily build loops around them if our terrain map array is null we want to first create a new one with the size of our width and height variables and then we want to fill it with random tiles so we call our init position method which we write right now it's public void and it will as I said before loop through our width and height values of the time maps so let's name them x and y and change the width to height and so if we have done prepared our loops we can now create a new random number for our terrain map at a given value and this should be between 1 and 100 and 1 because 101 is exclusive and in case that this range random range is less than our initial chance we define later on in our game object we will set the value at this position of the array to 1 meaning the tile is life otherwise it will get a 0 meaning it is not field so let's continue to write our generate terrain method which we will call the number of times we specified for our do simulation method in a for loop so we will call this method with the terrain map array as an input and write back to the terrain map the processed vector 3 and type positions as a result of our neighbor check afterwards so let's declare that method the Andheri first gentle pause I call it and the int array as an input which I will call old map also let's declare this new map array which will be the output area after processing the old map at the top of this method again it will have the size of width and height of the time map and we will return it at the bottom but before I declare the number of neighbors isn't int and now we loop again through width and height of this old map int array meaning x and y values of the time map and inside this nested for loop we will do our neighbor check setting our neighbor to zero first and then I will declare bounce instruct which is nothing else than like a virtual rectangle of int positions in X Y and that direction around our current position starting from minus 1 and X Direction minus 1 and y direction 0 in that direction and giving a length of 3 + 3 + x + y + 1 in that direction so now we can place a for each loop inside our nested for loop and add this struct for each value to our X&Y positions to get the neighbors around our current position so we loop through our my bounce or might be our positions within and now come longer if statement first we exclude the x and y were you 0 because if x is 0 of the bounce we add to our position and Y then we have found our current position and this is not a neighbor so we will continue and now we want to exclude the values outside the map 2 so x + the bounce in x-direction have to be greater equal 0 less than the width of our map and y+ bounce why well you have to be greater equal zero to and smaller than the height of our map so if this is the case we will add the value of our old map to our neighbor counter and else we are at the border of our map and we simply want to count a neighbor here as well to draw a border around the terrain we generate so after we have done this check for a given tile at a given position it's time to compare the result with our well used for birth and death limit parameters if a toilet the position in the old map equals one and in that case if the number of neighbors is below the death limit then this type will not be on our new map meaning it will receive a zero value otherwise if it is equal or higher than the debt limit we will transfer this tie position to our new map giving it one and the same check with a different parameter has to be done for the tiles which are currently zero in the old map so if they have a neighbor count higher than the birth limit they will receive one and a new map and else they stay dead so that's it for our generate terrain method I think this was not too complicated so what is left behind for our main method is that we simply want to place the tiles from the protest list on our time app so one last time we loop through width and height and if we find a tile which has a value of 1 position and our terrain array and we will place it on the map using top map set tile function which needs a new vector3 int as an input we use the minus X plus with divided by 2 and minus y plus height divided by 2 because we don't want to stand start our time map at 0 0 and we place our top tile on the top map there's something wrong here [Music] okay it's not sent message but set tile of course so and for every position on the bottom map we will set the bot tile as well no matter if because we did not process this in another method we want to fill this map completely because it's our map for lava or water and other fluid like stuff so one last thing good way to start the simulation is to use our mouse button so we go to the update method and check for input get mouse button down and for the left mouse button it's 0 and in this case we call our dusim method for the given number of repetitions and in case we want to clear the time apps and start a brand-new simulation we can also place below our clear maps function associated with the event of hitting our right mouse button so in this case we call the clear maps function with a value of true because we clear the time apps and our in array as well so going back to unity we can now create an empty game object for our title to metaclass I name it like the class and drag our script onto this game object and now we see all the sliders for our values we need to specify for the different parameters we can already choose like for example the time app size and X&Y I'll take a small one for the beginning and we have four slots - for each time app and two for our tiles so it's easy for us to create the time apps now simply choose game object 2d and this will place a tile map with the corresponding grid in our scene and we click on the time map name the first one top map and then again we go to 2d objects game object menu and called that one our bottom map or bottom map we quickly change the order layer for our top map to one so it's above the bottom map and now we can drag both maps into the empty slots of our tight automatic script to create the tiles we click on assets create store them wherever we want to and one will be our top tire and the other one of course our bottom tile so what is next is we have to choose like a sprite for our tiles so we can display them in the scene for this purpose I've created a simple of 24 times 24 white box sprite which I drag into the folder and quickly adjust the import settings for this one with the pixels per unit of 24 a third amount of point no filter and compression of none now we can select this box texture and assign it to each of the ties we created with a difference in the color we choose so the first one gets a bluish light color from me so it's more like what a tie for the bottom map and the second one gets a brown color as it stands for the terrain which we simulate on our top map and now we simply drag our ready tiles into the slots prepared for them as well before we start our simulation I have decided to slightly adjust the values to give you a better impression what we are aiming for so I give the simulation and initial chance of 25 birth level of three death limit of two and a number of repetitions for each simulation of one and I resized the time map with an x value of 120 and an Y value of 70 I adjust the camera autographic view to 45 as well let me now enter play mode and hit the left mouse button tarah you see like the top tiles on top and the water toilets at the bottom and every time you click the left mouse button the structure gets ya rounder and more idn't like shaped so I strongly encourage you to play around with the parameters because you could be surprised in different scenarios of the outcome you receive and if you don't like the border just comment it out in our tile automatic script the last thing we want to do here is to save our map as a prefab so let's do this in the update function by calling a method in case we press the middle mouse button which is number two I call the function safe asset map and this is a reason why we implemented the unity editor namespace because we would like to show a quick response field so let's write this function it's a void and we start by declaring a string file name I call it safe name give him a name of your choice something perhaps which does not fear the danger of overwriting something else to prevent this I implemented the counter too and we simply search for our grid game object by name and if this search is successful returning true our set path under assets and safe name will be composed plus the ending prefab so now we call the prefab utility and if we successfully create a prefab under the safe path we will display a quick box on the screen using our editor utility and the function is called display dialogue and right in some short title and a quick message that the time app has been saved successfully to the folder and fine am we specified and complete this step by in dirtying field for the user or better button to continue and and this dialog so in encase the safe procedure was not successful we also want to react by displaying a dialog this time giving feedback that some error occurred so this is just one hint how to do the storing again this is not fire proved so it bears the danger that you can overwrite something so now if we try this out and put during play mode and you press the middle mouse button you should get some weird message so now you can find your saved effort prefab under the assets folder and if we delete a time app we have now in our scene and reckon the asset you see it there with the safe name we specified in the hierarchy and if you create a new palette you can go ahead perhaps start by dragging in our terrain and water tile and now you can choose to the brush and edit your Maps like you want to we are slowly coming to the end of this tutorial but the best is yet to come things get interesting and more appealing on the next level by using your own scripted tiles and multiple layer as demonstration I've put my own tiles into the project folder like a cave tile water and lava tile you just have to quickly adapt the fields in our class to the new tile types I've used the terrain tile and the water tire is an animated tile go back to our tile automatic class and drag in the new tire types then enter play mode start the simulation by clicking left and tada things look more appealing and at least like a raw in game time app you can even drag different tire types on the fly during play mode and get a complete new landscape if you want to learn how to do your own scriptable tiles check out my previous tutorials as always I've uploaded project assets to my Dropbox feel free to download and modify and if you like what you have seen please help me to take this channel to the next level by subscribing I promise I'll be back with more unity type stuff so thanks for watching and see you soon
Info
Channel: Daniel Hofmann
Views: 87,108
Rating: undefined out of 5
Keywords: Unity 2017, Tile map, Tilemap, Unity tutorial, procedural tilemap, Unity 2D, Game of life, cellular automata, algorithm, tilemap tutorial, animated tile, scripted tile, tile based game, procedural tile map, procedural, tile map tutorial, unity 2017.3, unity tile palette, unity 2017.2, unity tilemap editor, unity tile map auto generate, unity tilemap, 2017.2, 2017.3, unity3d, tiled game, 2d world generator, tilemap game, PCG, Procedural content generation
Id: xNqqfABXTNQ
Channel Id: undefined
Length: 32min 38sec (1958 seconds)
Published: Sat Nov 25 2017
Related Videos
Note
Please note that this website is currently a work in progress! Lots of interesting data and statistics to come.