Unload a plugin

For a project we have to develop a C++ plugin for unity.

While developing, I have to repeatedly test the plugin. Thus I recompile it in XCode, let XCode copy the built .bundle to my ./Assets/Plugins folder and after that I press Play in unity editor.

Unfortunately unity is still using the old version of the plugin. To update the plugin in unity, I have to completely reload all assets leading to 80% reload time / 20% development time.

Is there any way to unload the .bundle when I stop the game (e.g. during OnApplicationQuit() )?

Things are worse on Windows: I cannot overwrite the old plugin since it is locked.

I need a solution that works both on Mac OS and Windows since we have to use both systems here.

Thanks in advance Tom

Kind of an old question, but here's a comprehensive answer:

Under MS .NET, DllImport'ed DLLs are never automatically unloaded, but you can manually call FreeLibrary to do so. You need to make sure, though, that you call LoadLibrary later before you make any other DllImport'ed function calls, because the runtime isn't expecting the assembly to be unloaded.

Unfortunately, this doesn't work with Mono, which maintains an internal hashtable of loaded native DLLs and their associated HMODULEs. If you try to FreeLibrary and LoadLibrary later, the returned HMODULE (which is different each time) will not be properly cached, and Mono will attempt to use the (stale, freed) HMODULE.

If your interface isn't too big, and you're willing to do a bunch of extra boilerplate work, you can make a wrapper DLL as Lucas Meijer suggested. Note, however, that this wrapper MUST access your main DLL functions through LoadLibrary/GetProcAddress, NOT through DLLImport, or you'll run into exactly the same problem.

Better solutions will require Unity Technologies to make a custom edit to the Mono sources. If anyone at Unity actually feels like doing this, let me know and I'll show you which lines to change.

As a workaround, you could implement your plugin to be a shallow wrapper around another dynamic library, and then have your plugin do the unloading and reloading of the dll containing "the meat". I don't think there's anything you can change to Unity's behaviour of how it loads and unloads plugins.

I'm doing the same thing with xcode and i get strange behaviours. Sometimes it does use the recompiled bundle, sometimes it doesn't.

On Windows it always uses the old dll. It even goes so far, that I delete the old dll from my project folder (within unity) and when i drop the new one in it somehow reimports the old one plus the new one... This is really annoying. The only "solution" i found for this besides the reimport all thing is to quit unity. Build a new dll automatically copy it into the project folder and then start unity again. This works and on my project is faster than reimport all, but still a huge pita if you have to write and debug your own dll.

I don't know why just reimporting the dll does nothing at all, that would be a sufficient solution for my problems.

I don't know for you but building the app is faster than restarting Unity for me and updates everything.

Clement.