Platform Dependent Compilation
When I wrote the article for the secondary fire powerup I needed a way to test the weapon without playing for long times to get enough energy. I decided to implement my first “cheat” into the game, but this would only be for testing purposes, but I also wouldn’t want this cheat to be accessible in any of my builds. I’ve thought of using some bool to check if “cheat-mode” is true but at this point, I didn’t want to go through all of that again remembering the horrors of using (or forgetting to use) such checks in my first ever Unity game.
Back Story Activated
When I just got into game development and unity I had to use the knowledge I had to get by with what I was working on. When I was working hard on a game that I build for WebGL and mobile, I had to set the bool to true if it was a mobile build. I used this to activate or deactivate mobile controls and sometimes I’d forget to check the bool for a mobile build and had to rebuild the project.
You see I didn’t want to make those past mistakes again by forgetting to set bools to true or false and allowing cheats into the game. As game developers or software engineers, we’re taught problem-solving skills and this was a problem I had to solve, so I jumped down the rabbit hole that is Google to find a solution to my problem.
I discovered something in the Unity documentation called “Platform Dependent Compilation”, and what that does is you can prevent or allow certain parts of your code from compiling depending on the parameters you assign to pieces of code. This really intrigued me and I did some more digging to find some articles and forums where people used it or asked about it to get a clearer picture.
So How Does It Work?
Unity has a bunch of platform define directives. They’re almost like booleans but they’re created by unity and they’re set to true or false depending on the platform you’re on. This is really useful if you want to compile certain parts of your code if you’re on a specific platform or stop that code from compiling.
In my case, I wanted to run some cheat buttons when I’m testing in the editor but didn’t want that code to be compiled in any of my builds regardless of platforms.
There was a define called “UNITY_EDITOR” that stood out for me. The function explained that the directive is to call Unity Editor scripts from your game code. This is what I needed. I only wanted that cheat function to work when testing in the editor.
Implementing the Define Directive
On my player script in my Fire() function, I had another if statement to check if I’m pressing the x key to fill the energy bar to the max and if I don’t have enough lives, it would give me an extra life. This was important for me to add since my secondary fire consumed 10 energy and 1 life.
I started by adding the “#if UNITY_EDITOR” right above the “if(Input.GetKeyDown(KeyCode.X))” and added the “#endif” after the closing parenthesis. Unfortunately, the first time implementing it I added the “#endif” below the incorrect parenthesis which broke my game when I ran a test build. So I would advise you to be careful how you implement these Define Directives and to double-check your code.
For me, this opened a whole world of ways to improve code performance when building cross-platform apps or games. If you’re building a mobile or PC game and want mobile controls to be active on Android or iOS builds, but not visible for PC then you can use Define Directives like “UNITY_IOS” and “UNITY_ANDROID”.
I hope you enjoyed this short article today and learned something new. I love learning about new ways to do things and love sharing my newfound knowledge with the world.