I found on a tips guide in Unity that I can do:


And do_something() will only be executed on Unity editor. Even more, when I build the game, that section of the code won't be compiled.

I found that as something very interesting, I thought it could be useful for example to use it on DrawGizmos methods since they can only be used on Unity Editor, and you don't need them on a finished product.

What has it been designed for? Is there anything bad that could come from using it?

  • 1
    \$\begingroup\$ as you said it's useful for editor scripts what is your problem? \$\endgroup\$ – Seyed Morteza Kamali Jun 13 '19 at 2:39
  • \$\begingroup\$ @SeyedMortezaKamali, I think they are useful, but when I asked it to my teacher he said I shouldn't use them. That is why I'm asking about if there is any downside in using them. \$\endgroup\$ – Ender Look Jun 13 '19 at 2:46
  • \$\begingroup\$ It sounds like you should ask your teacher why he said that, then. We won't be able to read his mind. A tool exists to be used - if it was always a bad idea to use it, they wouldn't have included this macro in the first place. It sounds like you accurately understand where this can be of use, so I'm not sure what more answer you need from us? \$\endgroup\$ – DMGregory Jun 13 '19 at 4:05

For one game I created a level editor which didn't run in the editor but was actually integrated into the game itself. It was able to save levels as assets in the project directory. This required to reference classes from the UnityEditor namespace. But if you use such classes, then your game only runs in the editor and you can no longer build it for distributeion.

By using the #if UNITY_EDITOR preprocessor directive I was able to dummy out the map editor specific code and was again able to build my game without sacrificing the map editor.

Is there anything bad that could come from using it?


You now have a codebase which intentionally works differently in the editor than it works in the build. That means you need to do a lot more testing to make sure that your game still works on your target platform. Testing your builds is something you should be doing in regular intervals anyway. But the more you use platform-dependent code, the more likely it gets that you run into problems you can only reproduce in the build. And in that case you have a lot less tools available to troubleshoot the problem.

| improve this answer | |

these are called Preprocessor Tags and used for Platform Dependant Compilation. It is a feature of C# and Unity has its own directives (see here).

They are very useful and I use them quite a lot. There are various use-cases for them, especially if you are developping cross-platform (e.g. android and ios).

For instance, I use them when implementing Ads or InAppPurchases on mobile.

    private void InitializeUnityAds()
        string gameId = "2969998";
        string gameId = "2969999";
        string gameId = "";
        Advertisement.Initialize(gameId, TestMode);

(the #elif is not a typo, it's the syntax)

But I've also used it to exclude Editor code which is placed on a non-editor class (which may be a code smell, but still). Editor code cannot be built into a release and Unity will throw an error. Placing it in preprocessor tags will strip them from the build, but allow you to use the functionality inside the Unity Editor.

| improve this answer | |
  • \$\begingroup\$ I've used these to wrap some designer-friendly variables, like string names to show in inspectors that aren't actually used by the game, or a jump height variable that's actually just used to calculate the jump velocity under the hood. That way I get them in editor where they help, and strip them when they're unneeded. \$\endgroup\$ – DMGregory Jun 13 '19 at 11:21

Pre-processor defines like UNITY_EDITOR can be used to include or remove code from a file.

Before a file is compiled a it is "pre-processed." Code that falls between a preprocessor block like #if UNITY_EDITOR will get removed from your source code if UNITY_EDITOR is undefined.

UNITY_EDITOR is only defined when you run your game from within the editor. When you make a stand alone build of your game UNITY_EDITOR is not defined and any editor code for debugging, and development is removed.

| improve this answer | |

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.