x


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

more ▼

asked Jan 15 '10 at 04:00 PM

Tom gravatar image

Tom
36 1 1 3

Have you tried deleting the plugin from the project prior to replacing it?

How about right clicking on the plugin and selecting 'reimport'?

Does quitting the IDE and reopening it solve the issue?

Jan 15 '10 at 04:20 PM Brian Kehrer

Deleting and reimporting does not help nor does a 'reimport' of only the plugin. Quitting and reopening unity helps exactly the same way a 'reimport all' does. But restarting unity everytime I recompiled the plugin isn't a solution just more work around the problem...

Jan 19 '10 at 07:45 AM Tom
(comments are locked)
10|3000 characters needed characters left

4 answers: sort voted first

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.

more ▼

answered May 17 '11 at 04:41 PM

sneftel gravatar image

sneftel
1.7k 7 9 20

(comments are locked)
10|3000 characters needed characters left

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.

more ▼

answered Jan 15 '10 at 04:19 PM

Lucas Meijer 1 gravatar image

Lucas Meijer 1 ♦♦
8k 19 43 85

I'm curious as to what Unity's behavior is for loading an unloading plugins. Is Tom correct in stating that the project must have all the files reimported in order to reload a plugin? Does this persist between quitting and reopening Unity?

Jan 16 '10 at 02:52 AM Brian Kehrer

Quitting and reopening helps but I would have to do it every time I recompiled the plugin... (see above)

Jan 19 '10 at 07:47 AM Tom
(comments are locked)
10|3000 characters needed characters left

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.

more ▼

answered Jan 16 '10 at 09:51 PM

StephanK gravatar image

StephanK
6k 40 53 93

(comments are locked)
10|3000 characters needed characters left

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

Clement.

more ▼

answered Sep 14 '10 at 08:47 AM

Clement 1 gravatar image

Clement 1
1

A dirty hack that could help in this case it to create a batch file after compile that copies your dll thirty times.

Then all you have to do instead of restarting Unity is up the filename of your dll and it will work like magic! (Eventually you'll need to restart to release all the locks but it should save some time overall)

Sep 03 '12 at 09:10 PM dvochin2
(comments are locked)
10|3000 characters needed characters left
Your answer
toggle preview:

Up to 2 attachments (including images) can be used with a maximum of 524.3 kB each and 1.0 MB total.

Follow this question

By Email:

Once you sign in you will be able to subscribe for any updates here

By RSS:

Answers

Answers and Comments

Topics:

x409
x246

asked: Jan 15 '10 at 04:00 PM

Seen: 3229 times

Last Updated: Sep 03 '12 at 09:10 PM