Skip to content

Conditional compilation and UNITY_EDITOR

↑

The C# preprocessor can include or exclude code at compilation time. Unity defines symbols such as UNITY_EDITOR when compiling for the Editor. This lets you keep editor-only functionality out of player builds, but it is not a substitute for organizing editor tools into editor-only assemblies.

Use #if UNITY_EDITOR, not #ifeditor or #if editor. UNITY_EDITOR is a symbol, and preprocessor directives use the #if ... #endif syntax.

Unity C# | EditorOnlyLog.cs
using UnityEngine;
public sealed class EditorOnlyLog : MonoBehaviour
{
private void Start()
{
#if UNITY_EDITOR
Debug.Log("This message is compiled for the Unity Editor.");
#endif
}
}
Unity C# | SceneHelper.cs
#if UNITY_EDITOR
using UnityEditor;
#endif
using UnityEngine;
public static class SceneHelper
{
#if UNITY_EDITOR
[MenuItem("Tools/HCSTech/Print project path")]
private static void PrintPath() => Debug.Log(Application.dataPath);
#endif
}

UnityEditor APIs are not available in a normal player build. When making larger editor tools, place scripts in an Editor folder or an editor-only assembly rather than scattering many preprocessor conditions throughout gameplay code.

  1. Decide whether the feature belongs to runtime code or editor tooling.
  2. Use a compile-time symbol for a small conditional section only when appropriate.
  3. Check both the Editor and the target player build, as a successful Editor run does not prove the build compiles.
  4. Keep editor-only dependencies behind an assembly boundary when the code grows.

See Assembly definitions for larger editor/runtime separation.