Joris Eijmberts

Turning crazy ideas into games!

Pre-processor directives

To give developers the possibility to have platform specific code Unity has tools to help with this, these can be assembly definitions, or pre-processor directives.

What?

To keep it simple, pre-processor directives (from now on referred to as ‘directives’) can change parts of your source code before compiling it. They can do this my replacing parts of you actual source files, or importing completely new ones. This can be done by placing annotation inside you source code files.

Below is an example of these directives in use in CP Social. This example used 4 ‘if’-statements to check which platform is being targeted and implements the right implementations of a CP Social platform.

For a more detailed explanations take a look at Microsoft’s explanation and Unity’s explanation.

Why?

The reason directives were used in CP Social was to finalize the platform dependent compilation. As shown in the example above directives were used to implement platform specific implementations. These directive are also mandatory when you want different implementations of the same functionalities of a system on the same platform, as described in Steam VS Unsupported.

Conclusion

Pre-processor directives are a very powerful tool to use when creating multi-platform project in Unity. During development pre-processor directives helped a lot when used in combination with Assembly definitions to realize almost all of CP Social.