This is a thinking out loud post, but I have been thinking a lot recently about how I am going to build my level for my game. I have also been procrastinating a lot by reading the Unity3D forum on Reddit.
There was a post on there recently where someone was asking how they could work on their game project during their nine to five job. I was surprised to see the replies were mostly negative.
Back in the day when I was a programmer, I wrote a lot of dull financial / trading esque software. I had got to the point where I could do it with my eyes closed - which often happened. No surprise that clients don’t like spending x hundred a day on a contractor for them to appear at sleep at their desk all day. So to combat this, I did two things:
- Drank a blend of very strong French Arabic coffee which I now wouldn't be surprised to find out has now banned under the UN human convention against torture.
- Worked on small games within the software I was developing.
I should stress the word small here, mostly they were challenges I set myself. A common one was sorting a deck of cards as, from a programming perspective it is a genuinely interesting challenge, but if you have ever worked as an actuary and logged into a system to evaluate a pension portfolio only to find yourself playing a game of blackjack - that was me..
So with that in mind, could I use MS excel to generate a 3D level for Unity ? Yes, yes I can.
First things first, you will need to open Excel and make sure you have two spreadsheets in your workbook. The reason for this is; sheet 1 will be used to design your level and sheet2 will output the code for unity.
In the picture above, I have designed a copy of the Pac Man maze using "w" for walls and "d" for the dots. I have used conditional formatting to make it easier to visualise, but if you are trying to work in secret in an office, I would recommend not using this and use numbers instead of letters - no one is ever going to suspect anything if you are sitting in an office staring at a spreadsheet full of numbers - trust me.
Now open up the VBA editor for excel and copy the following code. What this code is going to to do is loop through all the cells in the range defined by the boundaries defined in intRow and IntCol and depending on the value; produce a string containing an Instantiation line of code and output it to a row on sheet2 - that’s all.
Sub subUnityCodeGeneratorForMaze()
'************************************
'AUTHOR:RSJS
'DATE: 26/10/2017
'VERSION 0.1
'WEB:https://intotheeaglesnest.blogspot.co.uk/
'DESCR:Draw a map on sheet1 using the letter w
'to represent walls and d for dots.
'The code will then loop through
'the rows and columns and output c# instaniate statements
'to sheet2 which can then be copied into unity.
'DEPEND: Spreadsheet requires two spreadsheets.
'************************************
Dim intRow As Integer 'row reference o
Dim intCol As Integer 'col refernece
Dim intRes As Integer 'this will be used to produce our result
Dim strOpenWall As String ' this holds the first part of the text for our code if it is a wall
Dim strOpenDot As String ' this holds the first part of the text for our code if it is a baLL
Dim strMid As String ' this holds the middle
Dim strClose As String ' this holds the end
Dim strResult As String ' this will hold all our final code
'////////////////////////
'end of declarations
'set up
strOpenWall = "Instantiate(wall, new Vector3("
strOpenDot = "Instantiate(dot, new Vector3("
strMid = ",0,"
strClose = "), Quaternion.identity);"
intRes = 1
'Set the following to the relevant end Row and Column boundaries of your maze
intRow = 63
intCol = 57
'////////////////////////
'end of set up
For x = 1 To intRow ' Loop through rows
For y = 1 To intCol 'Loop through col
'wall
If Trim(Sheet1.Cells(x, y)) = "w" Then
'Build our code line
strResult = strOpenWall & x & strMid & y & strClose & Chr(13)
'output code to a row on sheet2
Sheet2.Cells(intRes, 1) = strResult
intRes = intRes + 1
End If
'dot
If Trim(Sheet1.Cells(x, y)) = "d" Then
'Build our code line
strResult = strOpenDot & x & strMid & y & strClose & Chr(13)
'output code to a row on sheet2
Sheet2.Cells(intRes, 1) = strResult
intRes = intRes + 1
End If
Next
Next
End Sub
Open up Unity and start a new project.
1. Create a cube - make it a prefab named wall.
2. Create a sphere, make it a prefab named dot.
3. Add materials (optional)
4. Create an empty object in your scene, call it mazeGenerator and add a c# script called makeMaze.
8. Go back to excel and copy the contents from sheet2 into notepad. Do a search and replace to remove quotations marks, then copy the contents into the void start () of the makeMazeScript.
9. Build and run and hey presto a Pacman maze!
It is a bit of a kids magic trick as it is just a bit of simple string manipulation. I can’t imagine a twelve hundred line script being ideal nor the number of prefabs, but as a quick and dirty proof of concept, there you go.
The interesting thing is; I know that you can load text files into Unity and to change the VBA to write to a text file rather than a spreadsheet is not that complicated. To reduce the numbers of items that need to be created is again not that complicated.
One of the challenges I have in my game is there will be a series of locked doors for which you will need to find keys for. I can see myself adapting and improving the above, as I want a quick and dirty way to check the gameplay balance in where these locked doors and keys are located.
If you found the above useful and end up doing anything with it, please let us know in the comments.
Comments
Post a Comment