What is the purpose of the number sign within Unity-official scripts?
I'm sure it's present in other unofficial scripts, but I've only encountered them in the ones provided with Unity's StandardAssets folder. Right now I'm studying the RigidbodyFirstPersonController script to create my own, and I'm seeing these:
#if !MOBILE_INPUT
if(this) { dothat(); }
#endif
I'm fairly new to C# programming (and Unity programming in general) so I'm unfamiliar with this. What do they mean? Are they an equivalent to a standard if statement? Are they some form of comment system? The preceding ! makes me think it's handling input that ISN'T mobile-based.
Answer by tanoshimi · Jan 01, 2016 at 08:09 AM
They are called preprocessor directives, and they're a standard part of C# (and other programming languages).
#if is like if, but they are evaluated at different times.
if is evaluated at runtime - it provides a way to branch your code logic based on the current state of a variable.
#if is evaluated at compile-time - it provides a way to include (or discard) entire sections of code from your build based on whether a directive is defined.
As you correctly guessed, in the example you provided, the following section of code would only be included if MOBILE_INPUT was not defined. This could not be changed once the game was built.