Editor Only Objects

Hey guys, quick question:

Is there a way to have editor only objects? For example, I have a Cubemap Renderer object that will only ever be used in the editor. It has a camera on it, so it can’t be in the game at run time. Disabling/enabling is a bit inconvenient, so I was hoping for an automated way of doing it. I figured either a DebugOnly behavior or maybe another way to use some sort of tag already included with Unity?

If I were to use a DebugOnly behavior, how could I make it not include it in the build? Perhaps and editor script?

Thanks, guys!

you could use something like this to disable the gameobject when in play mode

[ExecuteInEditMode]
public class CurveGenerator : MonoBehaviour {

#if UNITY_EDITOR
void Update () {
if(UnityEditor.EditorApplication.isPlayingOrWillChangePlaymode) {
this.enabled = false;
} else {
// editor code here!
}
}
#endif

}

Got the code from this link http://blog.brendanvance.com/2014/04/08/elegant-editor-only-script-execution-in-unity3d/comment-page-1/

Also remember to search the API reference Unity - Scripting API: ExecuteInEditMode

1 Like

I had a script like this. The problem with this script is that the object will still load just to be destroyed.

using UnityEngine;

public class DebugOnly : MonoBehaviour
{
  void Awake()
  {
    if (!Debug.isDebugBuild && Application.isPlaying)
      Destroy(gameObject);
  }
}

There is also the “EditorOnly” tag which should remove the object from actual builds, while still in the Unity Editor.

Thanks for the responses, guys. I figured an OnEnable function to disable the object during runtime would suffice as a last resort, but it would be nice to prevent the object from even making it into the build. I thought maybe a build preprocessor, but then how could I tell it to not include the object in the build at all?

The EditorOnly tag would not remove the entire GameObject from the build, if I am understanding it correctly. I am going to have quite a lot of objects in my hierarchy view at runtime, so minimal objects would be nice.

Thanks for the help! :slight_smile:

EDIT: Sorry, it’s a postprocessor actually: Unity - Scripting API: PostProcessSceneAttribute

Would calling Destroy in that callback destroy it in the hierarchy permanently, or just in the build?

EditorOnly should remove the GameObject from the build.