Use [ExecuteInEditMode] script inside Editor asmdef

I have a structure like so for an editor tool I’m working on:

Scripts

Tool classes
Tool.asmdef
Editor

Tool editor classes
Tool.Editor.asmdef

I want to conditionally exclude all of these classes from any builds, so I made the only platform for both asmdefs the Editor. I also added a UNITY_EDITOR constraint to the asmdefs too. I have a MonoBehaviour in the Scripts folder that is marked [ExecuteInEditMode], however this script stopped loading and if I try to add it to a game object I get an error about it being an Editor script. I assume this is the asmdef that is causing this, is there a way to get this script to load while conditionally excluding this code from builds? Thanks

No, that’s just because I guess that step makes it an Editor script, which you also cannot add to a GameObject.

I usually just #ifdef UNITY_EDITOR any blobs of code in my MonoBehaviours… you kinda have to if they do any editor-y stuff.

Partial classes lets you keep it nice and tidy. For instance, here is the base declaration of my Datasack, which is a ScriptableObject:

https://github.com/kurtdekker/datasacks/blob/master/datasacks/Assets/Datasack/Core/Datasack.cs

But it has a bunch of custom editor code all within that class, and I put that here, ifdeffed out:

https://github.com/kurtdekker/datasacks/blob/master/datasacks/Assets/Datasack/Core/DatasackInspector.cs

1 Like