Global Conditional Compilation with Unity3D
I am developing a Unity(4.3.4) game and need to toggle features on and off between the Lite and Standard version. The Lite version contains ads and is free, while the Standard version costs 99 cents.
One technique for doing this in C# is to use preprocessor directives. Unity does support globally defining preprocessor directives. Create a file called smcs.rsp
at <Project Path>/Assets/
. In my example use case of making a Lite app version toggle I would put the following text in the smcs.rsp
file:
-define:LITE
Then later in my C# script files I can conditionally compile code by using #if
in my code. Here is a sample C# script file that demonstrates using the LITE
flag.
using UnityEngine;
using System.Collections;
public class TestDefines : MonoBehaviour
{
void Start ()
{
#if LITE
Debug.Log("**** LITE version ****");
#endif
#if UNITY_IPHONE
Debug.Log("Iphone");
#endif
}
}
The **** LITE version ****
message will only be output if the preprocessor directive LITE
has been defined. Sometimes it is not obvious to new programmers that the Debug.Log("**** LITE version ****");
is NOT in the compiled code if the LITE
flag has not been defined.
References
https://docs.unity3d.com/Documentation/Manual/PlatformDependentCompilation.html