Game Development Reference
In-Depth Information
02 using System.Collections;
03
04 public class CubeScript : MonoBehaviour
05 {
06 // Use this for initialization
07 void Start ()
08 {
09 #if SHOW_DEBUG_MESSAGES
10 //runs ONLY if the Define SHOW_DEBUG_MESSAGES is active
11 Debug.Log ("Pos: " + transform.position.ToString());
12 #endif
13
14 //runs because it's outside the #if #endif block
15 Debug.Log ("Start function called");
16 }
17 }
Lines 09-12 feature the core functionality using the preprocessor directives
#if
and
#endif
conditional. This conditional is not executed at runtime like a regular
if
statement, but at compile time. At compile time, Unity will decide whether the global
define
SHOW_DEBUG_MESSAGES
is specified or active. If, and only if, it is, then lines
10 and 11 will be compiled, otherwise the compiler will ignore these lines, treating
them as comments. Using this feature, you can isolate all debug code within an
#if
#endif
block that checks for a debug define and activates and deactivates the code
on the basis of the
SHOW_DEBUG_MESSAGES
define, which applies project-wide to all
source files. The question that then remains is how is the define set. To set the global
define, navigate to
Edit
|
Project Settings
|
Player
from the application menu. Then,
enter the define name in the
Scripting Define Symbols
field, making sure that you
press the
Enter
key after entering the name to confirm the change, as shown in the
following screenshot: