This How to will show you how to create a randomly generated 8×8 map using C# code. The code does require some sprites to be useful, but any sprite will be able to be used to make your own version!
Hope this helps!
- Create your variables as public GameObject[] (name);. This will allow you to assign sprites to each individual group and will place them appropriately throughout the game board.
- Now, using for loops to prepare to generate a new board
- Now run a script, preperably named apprioprately (boardSetup), and set up a new GameObject to be the board. Then use for loops to fill in the outsides and insides of the board.
- Next, run a command that allows us to randomly layout objects, with a minimum and maximum set, which should be set as varibles at the beginning of the script. (ex. Public int maximum = 8;)
- Now we will actually be creating the board by calling the functions we created above and also adding how many enemies will be on per level, with a mathematic equation.
- Once done with the script, go to Unity and create a empty child under the GameObject tab. Rename the object GameManager and add the script to it.
- Then select the desired sprites for each category (outside wall, floor, food, enemy) and using shirt + click or cntl + click, drag and drop the prefabs onto the script.
- After filling in all the categories, drag the GameManager object into the prefabs folder and delete the object for the hierarchy.