One of the main hassles in 2D Game creation is level creation. In unity, people will usually create different scenes for each level. Imagine your 2D game have a few hundred levels(think puzzle games), are you going to create hundreds of scenes just for that and adding them to build settings? I will show you an easy way to create levels easily and yet only require one scene for all the levels. The key concept is adapted from quill18’s youtube video on Fast and Easy Level Editor
What is needed
All we need is to have a ScriptableObject to store all the information required and call it to generate the level. Simple! Wait, there is more. To make the process easier, we need to tweak a little by modifying the inspector of the ScriptableObject. Then all that’s left is to create a means to generate the level at will.
Explanation of the level generation
I highly recommend to look at quill18’s youtube video to better understand how the generation is done, after all, a video depicts a thousand pictures and a picture says a thousand words, so a video equals a million words. But if you find it a hassle, TL;DR it is to:
- Create an image file, read individual pixels in the file and gather all the unique colors
- Create a list of the unique colors assembled and mapped each color to a gameobject or tile/tilemap
- Read the image file and using the mapping created in step 2, generate the mapped gameobject or tile/tilemap against the color on the position on scene corresponding to the pixel position in the image file. (Pixel position 0,2 will have its mapped object created at position 0,2 in the scene)
Create the image file(map)
This part is actually out of scope, but I felt this could help people to understand how to create the level map easily. I will use the NES game Battle City as an example of this. We can get all the maps of the game Battle City from StrategyWiki website. Below is the stage 1 map extracted from the website. The maps of Battle City are of grid size 26 by 26, and the extracted map from the website is of size 208 by 208. So what we can do is to shrink the image by eight times(I used Paint.net’s Image->Resize…). We will get all the details on the map at the pixel level. We will get the below. (I removed the eagle from the map to simplify the image as I am only using this for demonstration purposes) You can also create your map by drawing pixel by pixel or use a map image you want and shrink them accordingly.
Create the ScriptableObject
Now we can create the scriptableObject. The information we will need is a list of texture2d(maps) to store all the maps we want to create the levels from. We will also need a list of color to gameobject or tile/tilemap mappings so we will know what is to be generated at each position. We will store this information into a separate class called ObjectList(I will use tile/tilemap). Lastly, we will need a single texture2D as a temporary placeholder. This single texture2D is to load a map then allow us to extract all the unique colors and add the map to the list of texture2D.
Custom Inspector for the ScriptableObject
I created a custom inspector for the scriptableobject so that we can store the information and extract the unique colors from the map all from the ScriptableObject itself.
Basically what we will do is load a map to the single texture2D and click a button named “Add to ScriptableObject”, a routine will read the map and extract the unique colors in the map. We will only add unique colors(colors are “rounded” such that the max number of colors we can use is to 1000. This is to prevent every so slight deviation of color resulting in another different object.
We will then store the map into the list of texture2D(maps). As the automated loading of next level is sequential, it is necessary to get the sequence in the list of map correct, so I have made that list a reorderablelist so that we can perform reordering of the sequence on the fly.
On reading the map, we will also modify the import settings of the image so that it can be read by the program(set read/write enable) and there will be no loss of quality of the image(Compression none and Filter Mode Point)
After creating the custom inspector and proceed to add some maps to the ScriptableObject, we should see something like the below.
Loading of the GameObjects or Tile/Tilemap mappings in the ScriptableObject
For each of the color, we need to map it to a corresponding GameObject or Tile/Tilemap(I am using tile/tilemap) so that the program will know what is to be generated at the position on the scene equivalent to that of the pixel position in the image. If you want to do the GameObject you should use GameObject in ObjectList instead of TileBase and GameObject; I am demonstrating for tilemap as it has extra steps on top of GameObject which you all can figure out by just looking at the tilemap example here.
Let’s start by adding a tile. A tile is created when you add a tile to a tile palette. This will create a tile asset which you can add to the mapping. As for tilemap, you will need to build in the scene, make the necessary settings(e.g. tilemap colliders 2d, order in layer, composite collider 2d) and make it a prefab. Then add the prefab as part of the mapping. After adding the tile/tilemap, your ScriptableObject should look like the below:
Load the levels (finally!)
Now onto how to load the levels. The code below is creating an Editor Window to load the ScriptableObject and loop the mapList to get the map to generate the level. The part of generation is at the GenerateLevel routine where we get the map and read through each of the pixels and compare its color against the color mapping and load its corresponding gameobject or tile/tilemap. As we will be reusing the same scene to generate the next level, we should use a parent GameObject(for tile/tilemap I used a Grid GameObject as parent) to create tiles/gameobject under so that when we generate the next level, we can destroy the parent GameObject and recreate it to clean up the scene. I am using the tilemap, so I will need to run a loop twice, first to create the tilemap then to do setTile on the corresponding tilemap based on the position of tile equivalent to that of the pixel position.
This whole thing is too long? No worries, I have created this as an Asset Store package(Lazy 2D Level Creator). I have also included demos on it, sometimes practical is better than theroey so looking at it live is probably a better way to learn this.