Skip to content

Preprocessor

SyntaxDescription
#nullable enableEnabling null safety
#if FLAG / #else / #endifConditional compilation
#if UNITY_EDITOREditor-only code
#region / #endregionFoldable regions

Which symbols are defined depends on whoever compiles the source. Defined symbols, including UNITY_EDITOR, aren’t fixed to one set.

Null safety checks apply from the line you write it onward (Null and references).

#nullable enable
using UnityEngine;
using Tsukimi;
public class PreNullableEnable : TsukimiBehaviour
{
void Start()
{
string s = "a";
Debug.Log(s.Length); // => 1
}
}
#define FLAG
using UnityEngine;
using Tsukimi;
public class PreConditional : TsukimiBehaviour
{
void Start()
{
#if FLAG
Debug.Log(1);
#else
Debug.Log(2);
#endif
}
}
// Output
// 1

UNITY_EDITOR is a symbol for the Unity Editor. It isn’t defined on the path this example takes, so the enclosed code doesn’t compile.

using UnityEngine;
using Tsukimi;
public class PreEditorOnly : TsukimiBehaviour
{
void Start()
{
#if UNITY_EDITOR
Debug.Log(1);
#endif
Debug.Log(2);
}
}
// Output
// 2

It becomes a foldable region in the code editor. The compiled result doesn’t change.

using UnityEngine;
using Tsukimi;
public class PreRegion : TsukimiBehaviour
{
#region Body
void Start()
{
Debug.Log(1); // => 1
}
#endregion
}