What day is it with Unity?
Time has been slipping away what with setting up the new Mac and going through my old Linux Mint hard drive to bring everything across. Everything, that wasn’t trashed when the old Linux PC slid off the desk onto the floor while I was trying to make space for the iMac… at least I’d backed up most of the stuff on the old PC before I started moving stuff around.
One of the things that caught me out about Unity is the use of reserved file and folder names. A few days ago, I created a new script file, after which I tried to compile the project. This resulted in a load of errors appearing from nowhere, with messages like “The name ‘Editor’ does not denote a valid type (‘not found’)”. Because this was immediately following the migration from the MacBook to the iMac, I spent some time checking the code to make sure everything had come across properly.
Eventually, I figured out that it was actually the name of the new script that was the problem: “ShapeEditor”. “Editor” is a reserved word which Unity uses to identify editor extensions, and any class that has the word in it’s name has to reside under a folder called “Editor”. Renaming the script file fixed all the compilation errors, but it’s an easy mistake to make, and the error messages it produces aren’t exactly obvious.
There’s other similar reserved words, such as the folder name “Resources” which Unity will look in whenever you call a function such as Resources.Load to instantiate an object at runtime.
Yet more plugins
Progress has been slow on my game because I’m continually backtracking and re-doing bits as I learn more about Unity. I’m not trying to make perfect code, what I am trying to do is to find the most efficient way of writing code for Unity so that the next project will be faster and easier to write.
For instance, I decided that I wanted to add a grid to the level editor so that I could get an accurate view of the where all the objects are placed on the 2D game area. Unity’s line drawing functionality doesn’t seem to be particularly useful – In fact, it’s much like Shiva, the only line drawing calls are used for debugging, and don’t show up in final code.
The logical solution is to create lines using vectors as part of an object. I started off by constructing a grid from an array of lines in Inkscape, then imported it as an SVG spline to bypass having to figure out how to create an object from scratch with Unity. By messing around with the scale I was able to get the grid to match the size of the blocks on the 2D game area, and then I implemented drag and drop, with the ability to paint new objects into the scene from a palette of pre-defined splines imported from Inkscape. This turned out to be a lot of work because of the calculations involved in lining everything up and have it all at the same scale.
The result was several hundred lines of code spread across a number of files.
Once I had it working, I realised that I needed to alter the size of the grid slightly to account for the relatively narrow screen aspect ratio of some iOS devices. My fiendish plans unravelled due to the difficulty of adjusting the lines on the grid, and then applying the same changes to all the objects in the scene. While Googling for solutions, I came across yet another Unity plugin called “Grid Framework”. It wasn’t free, but for $20, I was able to implement a much better solution that allows me to control the size of everything in the game world from one line of code. The plugin also halved the amount of code that was needed to run the editor… if only I’d thought of looking for a grid plugin before I’d started…
Speaking of purchases, I have just bought a license for creating iOS apps with Unity, in addition to the Android one I already have.
The only outstanding compiler now is the Blackberry one, which I hope will become available soon, and I also hope is compatibile with all the plugins I’ve purchased… I still haven’t heard if Blackberry still has plans to upgrade the old Playbook to BB10 – it would be silly for them not to do it because they need all the goodwill they can get when facing off against Android and iOS.