CanEditMultipleObjects property not working with builds

I thought these properties were stripped from the code but seems like not this one in particular.
I have two properties, both related to Editor, but I had to add the precompilation directive to make it work:

    #if UNITY_EDITOR
    [ExecuteInEditMode, CanEditMultipleObjects]
    #endif
    public class...

Seems like the second one fails.

I’m in Unity 2022.1.4f1 while in Beta.

Well, yes, because CanEditMultipleObjects is in the UnityEditor assembly/namespace and anything within them should not be attempted to be built.

ExecuteInEditMode can be compiled into builds; it just does nothing in one.

1 Like

To elaborate, CanEditMultipleObjects when used in a non-editor context will still be attempted to be compiled into the current context, but the compiler will then not be able to find it’s definition (as it’s not being compiled) and throw an error.

If you want, say, an attribute to not be compiled where it’s declared in certain contexts, you can decorate it’s definition with System.Diagnostics.Conditional("UNITY_EDITOR")].

Funnily enough, Unity’s editor centric attributes (like Header) that live in the UnityEngine namespace aren’t decorated with this, and will be compiled into builds.

But all that aside, I believe CanEditMultipleObjects only does anything when decorating an UnityEditor.Editor derived class. Otherwise it does nothing.

If you want to include a custom editor into a runtime file (sometimes that’s convenient) I would always recommend to wrap it in a seperate namespace and wrap that whole namespace in preprocessor tags. A seperate namespace has the advantage that you can add more using statements inside that namespace. Something like this:

using UnityEngine;

// your runtime stuff here.

#if UNITY_EDITOR
namspace B83.Something.Editor
{
    using UnityEditor;

    // your editor only stuff here.

}
#endif

To me that’s much cleaner than wrapping individual lines of code in pre-processor tags all over the place. It also groups everything together.

2 Likes