Platform/Editor Specific DLL

I have a DLL with stuff that are specific to one platform and another DLL for another platform.

How do I make sure DLL A is taken when building on platform B and DLL B is ignore. And vice versa for platform B.

Also, if possible, I would like a DLL to be used only in the Editor and another to replace it when not in the Editor. Is it possible?

Take a look at Unity special folders info, under Plugins. This will help with first part of your question.

Up to my knowledge, using one DLL in editor and replacing it when not in editor is not possible. But maybe someone with greater experience has an idea how to accomplish this.

To answer your question, yes it is possible, but with a few caveats.

On most platforms (Webplayer and Windows 8 Store do not allow this), you can dynamically load an assembly with System.Reflection.Load. If you change the extension of your DLLs to .bytes, you can then load them as a binary file using a TextAsset field. To get the byte string, call textAsset.bytes, then pass that to System.Reflection.Load to load the assembly. You can then use reflection to initialize it, get data, or whatever you need. Because Unity doesn’t even recognize the .bytes files as DLLs, you can have as many DLLs in your project as you want without Unity choking on them even if they have the same classes. It would be up to you to switch the DLLs before a build, which could easily be done with a script.

You may want to split your DLL up in case there are some classes in it you want easily accessible through scripting to other scripts in your project. So for most of your public interface, if possible, keep them in a real DLL that Unity can process, and only dynamically load functionality that is fairly self-contained with a simple interface. Also, designating Friend Assemblies can be helpful if you have to split your DLL into parts.

Needless to say, you can’t declare any MonoBehaviors or serialized classes that need to be assigned to ScriptableObject/MonoBehavior in the dynamically loaded assemblies.

The trick is to use the Plugin folder structure.

Create a subfolder for each platform you need, note that if you used any other plugin/SDK it’s quite possible that the subfolders (eg. Android, iOS) are already created.

Put your platform-specific dll:s within the specified subfolders, and enter the inspector. Here you can choose platform under “Select platforms for plugin”. Check the corresponding one for your dll.

NOTE: if you have a dll that you will use when running in the Unity Editor, don’t name the subfolder “Editor”! This is a special folder which only seems accessible from the actual editor, and not when in play mode.