How to know and control by script if it's runing in editor or within a build

Hello,

I need to have separate behaviours when I execute code :

  1. within the Unity editor play mode is off
  2. within the Unity editor play mode is on (scene is running)
  3. within a build (outside the Unity editor)

Is it possible ?
I only know and use [ExecuteInEditMode] but it’s not only executing in edit mode, but in all cases (1,2 &3).
Is there a kind of “ExecuteInEditModeOnly” or "“ExecuteInBuildModeOnly” ?

Also I’m using the “recorder” (video recorder) which is intended to be used only in the editor during play mode. This cause problem an error when I’m trying to do a build (The type or namespace name ‘VideoRecorderManager’ could not be found (are you missing a using directive or an assembly reference?). Is there a best practice to use such limited component?

Thanks for your help !

Ben

You can easily redirect or conditionally compile in these situations with either #if UNITY_EDITOR pre-processor directives, or using something like Application.isPlaying or similar to determine what environment the code is running in.

As @spiney199 said, you can put your code in #if UNITY_EDITOR if you have a portion of code to run only in editor. But you have more possibilities: if you want something guaranteed only be in the editor, you can put the scripts in a folder called Editor or make it part of an editor-only assembly. Depends on the surrounding code, if they have to be in the built app or they are editor-only too.