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

Video Statistics and Information

Video
Captions Word Cloud
Reddit Comments

nice

👍︎︎ 3 👤︎︎ u/skellytongs 📅︎︎ Jul 24 2020 🗫︎ replies
Captions
in this video i'm going to show you how you can use scriptable objects to store any kind of information in a tile you don't have to install any extras or make special tiles you can use these standard tiles straight out of the box with unity's built-in time map system as an example i will show you how to make those little bugs that check how fast they can crawl based on the type of tile they are crawling over the main idea is that we use scriptable objects as a wrapper or box in that box we place the standard tile along with additional data this data is global meaning it will be the same for every type of time this is useful for information such as how sticky this type of tile is when your character is walking on it or if it is poisonous and so on the method i am showing you is not useful for where every single tile is supposed to have its own data that changes at one time like you might want to have that sticky tile lose its stickiness the more characters have just walked over it for that it is much better to use trucks which i will do in a future video so maybe you should subscribe okay back to our global data and scriptable objects i'm starting with a brand new project using unity 2019. but this will work for any unity version back to 2018 and since this is a long time supported version it will work even if you stumble upon this in the distant future let's start by creating a 2d timelapse simply by right clicking in the scene hierarchy and choose timelab next we need a palette so click window 2d type palette and dock it anywhere you want to create heights just drag in some sprites from your assets folder if you don't have any feel free to download mine from the link in the description those are two variations of grass and stone tides that fit seamlessly together now that we have a few types let's create these scriptable objects make a new script call it tile data and open it up to turn it into a scriptal object replace the monobehaviour with scripted object and add the create asset menu attribute so we can create instances of the script in the editor every kind of type is going to have its own instance of the scriptal object wrapped around it so the first thing we need is a reference to that tile make sure to use the unity time map namespace to have access to that class instead of using tile we will use tile base which is the base class for all tides that way it will work with custom scriptable tiles and we don't have to do any casting when using unity style functions later on some types might want to share the same data so we can turn this into a list for example i have two grass ties and two stone ties and the only difference is a bit of variation in the sprite so the data should be the same now in the scriptal object you can put anything you want i'm using just two floats walking speed and poison this is it for this script so save it and back in the assets folder it's time to create some instances of it by right clicking and then assign the tile references and set some values let's create the actual tile map next as you can see my tides are twice as big as the cells in the grid if that happens to you you can either change the grid size or double the pixels per unit in the sprite import settings i'm going with the first solution as i prefer bigger tides for this tutorial you can of course always use the transform of the time map and change the scale to resize the entire grid once your tiles fit in perfectly next we want to be able to take a tile for example by clicking on the map and get the information we just created for that we need a class that connects the two so create a new script call it map manager and open it up let's do the clicking first since it makes testing the other functions easier first of all we need a reference to the timelapse so once again use the unity engine tilemap namespace in update we check for mouse click and convert the mouse position to a vector3 integer because that is what the timelap uses with that vector we can use get tile on our tile map and that returns the tile base let's try that out by simply printing the name and position in the console back in the editor create a new game object call it map manager add the script and enter play mode okay that works only that it prints way too often because i use get mouse button instead of get mouse button down but that was just for testing anyway let's move on to get the connected tile data now create a list with all these scriptal objects we created before we are going to fill that up later in the editor the connection between each tile base and its style data can be stored in the dictionary and we can automate it to reduce the chance of accidental mix-ups in a wake we go through the tile datas and for each of the data we go through the tiles list for which we create an entry in the dictionary the tile will be the key and the tile data the value now when we click the tile we can use the tile base to look up its data with the help of the dictionary let's try that out by assigning the tile datas in our map manager and as you can see we are getting the data connected to the tides let's make use of this by creating a bug that will move based on the walking speed of the tides the walking speed will be used to multiply the base speed so the grass tiles will have a speed of 1 meaning no change but the stonetides will use 2 to double the speed make a new method in the map manager called get tile walkingspeed and use the vector2 as a parameter this will be a world position that objects in the game will provide so we can directly convert to a cell position get the tile get the info and get the walking speed which will be returned by the method right now the bugs can just move endlessly and there might not be a tile so let's account for that and simply return one if the tile they're on is null then make a new script called bug and get a reference to the map manager simply by finding it and yes it might not be the worst to make the map manager a singleton so you can easily access it from various scripts in the game personally i try to avoid singletons though i don't passionately hate them like some developers anyhow right now they are just a few bugs and they get the reference simply by looking for it let's make the bugs move around randomly by constantly moving them forward and rotate them every few seconds the speed is determined by base speed multiplied with the walking speed of the tie i'm making mistakey and not assigning the random rotation i fixed that in the end when all the bugs just walk in a straight line like an army the nice thing is that the script doesn't even need to know what tile it is on or that there even is a time map it just passes in its position and the map manager returns the walking speed let's create the bug by adding a bug sprite and dragging it into the scene at the box point maybe adjust the scale and drag it into a newly made prefabs folder to turn it into a prefab and because i hate to be done clicking i just make a quick function to spawn the bug prefabs randomly on the map for good practice let's even put that in a new script called boxboner this one is super simple it just has reference to the bug prefab and for every frame the space key is pressed down a bug will be spawned at a random location let's put it all together create a new game object add the bug spawner and assign the prefab in case you copied my mistake actually assign the when rotation in the back script i also moved the line in a wake that sets the counter to the move time because this way they will choose random rotation the moment they are spawned ok it looks like everything is working quite nicely and even on a crappy laptop like i'm using here the game runs smoothly despite every bug requesting the data every single frame okay this is pretty much it i hope you enjoyed the video and learned something please like and subscribe and leave a comment if you have any kind of question feedback or suggestion in the next video i will show you how to set the time up on fire because destroying is even more fun than creating goodbye
Info
Channel: Shackman creates art and games
Views: 25,261
Rating: undefined out of 5
Keywords: unity, unity tutorial, video game tutorial, tilemap, scriptable objects, unity3d, unity 3d, unity 2020
Id: XIqtZnqutGg
Channel Id: undefined
Length: 9min 33sec (573 seconds)
Published: Thu Jul 23 2020
Related Videos
Note
Please note that this website is currently a work in progress! Lots of interesting data and statistics to come.