.dll and #if UNITY_EDITOR

I’m new in the world of dlls and i was doing a lib to add it to unity having my editor scripts and normal scripts.
Everything works fine but i have one problem and i think i’m not understanding something.

In my dll project i cannot have the reference of UnityEditor, if i add it i will have an error when building the project and if i add #if UNITY_EDITOR in my dll project it don’t detect it which is ok.
The problem is that when i put my dll in Unity, it don’t understand when i put that #if so basically i’m limited on doing stuff in my normal scripts.
What i need to use those #if in my dll inside Unity. Is there a way to do it or am i limited?

1 Like

Why can’t you have a reference to UnityEditor.dll??

Because if i add UnityEditor.dll in my solution when i build the dll, then in Unity i need to set the dll import settings to any platform and then i build it throws an error that cannot compile my dll because it has the UNityEditor.dll reference on it

1 Like

#if SOMEFLAG means that the code’s only compiled if that flag is defined.

That works great in scripts in Unity, but for a .dll, everything’s already compiled. This means that those #if-conditions have already been parsed and resolved as you build the .dll.

So what you’re trying to do here won’t work. If you want to put both editor code and non-editor code in dlls, you’ll have to build two different dlls, and set the editor one to only get imported in editor - you can do that by clicking the dll and unchecking “standalone” in the editor.

1 Like

Hi Baste!
I have my editors scripts on a dll and the normal scripts on another, they are called MGUILibEdito.dll and MGUILib.dll
My Editor dll has reference to UnityEngine and UnityEditor cause it need them and the normal scripts have reference to UnityEngine and has the #if unity_editor flag.
As you said those #if are compiled and in Unity they don’t have effect but i need in a way to have the same effect like if scripts are there.

I’ve ran into the same exact problem. Did you manage to find a solution to this or does anyone have a good suggestion!?

These days you can solve that by putting the code in a package rather than a dll.

@Baste Thanks for the help, realised I was resurrecting a pretty old thread. Gotcha, great I will look into that :slight_smile:

For anyone that comes across this I also solved at .dll level by ensuring the release dll stripped out if #UNITY_EDITOR code and was only included in standalone using .dll Inspector settings. The editor dll included everything rather than just the editor scripts.

@silentslack Could you share more in details? I’m having the same problem you have already solved…

You make two copies of the dll, one compiled with the #UNITY_EDITOR flag, one without it.

Then you add both to the project, and enable the editor .dll in the editor and the non-editor .dll in builds.

1 Like

Create two configurations to build your scripts into a .dll:

Editor version: Everything
Runtime version: Editor code stripped out

Ensure that the assembly name is the same for both .dlls that you build (otherwise you’ll get problems in the compiled app) - I put my runtime version in a ‘Release’ folder.

Copy both .dlls into the project - as they have the same name you’ll have to put them in different folders.
Note: it would be nice to put the editor .dll in an editor folder but that doesn’t seem to work for me…

Ensure your runtime version is set to only be included on standalone platforms while the editor version is set to editor only.

This setup seems to work for me so far.

1 Like

You both @Baste and @silentslack are my heroes! Freaking love ya! This has saved me so much headache. Thanks a million!