- Home /
Is there a define for "code is running in the editor"?
Is there some way for C# code to detect when it is running in the editor? For example, I want some debugging buttons to exist when running in the editor, but not when I build a web player version.
Answer by Mike 3 · Jun 11, 2010 at 09:29 AM
There isn't a define, but there is a property you can check for
if (Application.isEditor)
{
//do stuff
}
http://unity3d.com/support/documentation/ScriptReference/Application-isEditor.html
Answer by Molix · Jun 11, 2010 at 01:40 PM
Another one you can use for debugging is Debug.isDebugBuild. That is always true in the editor, but you can also see it in the runtime if you build with the "Development Build" option.
Answer by yoyo · Mar 05, 2011 at 01:45 AM
As of Unity 3 there is now a UNITY_EDITOR define you can use.
If you have code that you only want in the editor, you can now do things like (C#) ...
void OnGUI()
{
#if UNITY_EDITOR
if (GUILayout.Button("Edit"))
{
DoSpecialEditorStuff();
}
#endif
}
You can apply this to methods, variables, even whole classes. Just be careful not to use something defined inside the #if block from a location outside such a block or you will have compilation problems on platforms where the code is compiled out.
Check out MSDN for more info on the C# preprocessor.
For more details on Unity's preprocessor defines, see the docs. There are also defines for the Unity version.
Your answer
![](https://koobas.hobune.stream/wayback/20220613061906im_/https://answers.unity.com/themes/thub/images/avi.jpg)
Follow this Question
Related Questions
Do scripts in a folder called "Editor" get left out of a build? 1 Answer
Reference a script from Editor folder 2 Answers
Compressing in OnPostprocessTexture yields "Not yet compressed" 1 Answer
Compiling Scripts Popup Appears Every Time You Switch to Unity 0 Answers
DrawGizmo attribute: GizmoType arg with incompatible flag values 0 Answers