- Home /
Is it possible to mark MonoBehaviour as EditorOnly
I have a MonoBehaviour which shows only debug stuff. Like object bounds. Don't want it to be available in final builds. Like GameObjects can be tagged as EditorOnly is there such possibility for MonoBehaviours?
Answer by Jamora · Feb 26, 2014 at 05:58 PM
You can put your class in an Editor folder. All classes in Editor folders (or any special folder, for that matter) go to a different assembly. The Editor assembly is not included in the build. See Unity Special Folders or the wiki page.
$$anonymous$$onoBehaviours from Editor assembly are not visible in Add Component, that was what i found first and tried to figure out why this is happening. $$anonymous$$y debug monobehaviours became in editor folder after refactoring. So I tried to understand why, and this brings me to the question how should i manage editor only behaviours.
You cannot use Editor $$anonymous$$onoBehaviours in the game build, if you try this the game will not compile. You cannot use $$anonymous$$onoBehaviuors from Editor folder from outside the Editor assembly.
If you find Editor scripts hard to use, or for some reason require functionality from these debugging scripts from within the game build, you can also use Platform dependant compliation.
Basically, You put your scripts in any assembly that is included in the build, then within those scripts deter$$anonymous$$e which lines of code should be included in the compilation.
Any code you don't want to include should look like the following:
#if UNITY_EDITOR
Debug.Log("This debug will not show up in the standalone.");
int g = 5+1; //this variable won't be in the game build either
#endif
That's what I do for my editors too.
public class $$anonymous$$y$$anonymous$$B : $$anonymous$$B
{
// stuff...
#if UNITY_EDITOR
private bool debug;
#endif
}
public class $$anonymous$$y$$anonymous$$BEditor : Editor
{
....
var sp_debug = serializedObject.FindProperty("debug");
if (sp_debug.boolValue)
// show debugging stuff
}
Answer by IgorAherne · Sep 03, 2018 at 09:10 PM
I just dedicate 1 gameObject to hold all such "debug scripts".
Then, use this to auto-mark gameObject as EditorOnly, including its scripts https://forum.unity.com/threads/editor-only-monobehaviour.508844/#post-3635764