4

Get a Unity project with a native low-level plugin .. Mac version.

enter image description here

Make a small change in the Xcode plugin project, and build.

You now have the new plugin library in the Unity project.

If you "Build" again, the final Mac app of course now contains the new library - no problem.

However .....

if you hit Play in the Editor,

it does not pick up the change in the library.

In fact it seems:

Every time you change a library in Unity, you must restart Unity!

Everything has been tried, "Reimport all assets", AssetDatabase.Refresh, renaming, etc etc. It seems you literally must restart Unity.

What's the deal on this?


More information on this:

It would seem that mac shared libraries/bundles cannot be unloaded. Article:

https://docstore.mik.ua/orelly/unix3/mac/ch05_03.htm

Apparently this was fixed in 10.5:

https://developer.apple.com/library/archive/documentation/System/Conceptual/ManPages_iPhoneOS/man3/dlclose.3.html

Maybe Unity could solve this now. At their usual pace it should happen anytime around 2035.

Community
  • 1
  • 1
Fattie
  • 27,874
  • 70
  • 431
  • 719
  • Maybe you just have to [AssetDatabase.Refresh](https://docs.unity3d.com/ScriptReference/AssetDatabase.Refresh.html)? – derHugo Jan 03 '19 at 06:06
  • Howdy @derHugo - you know, I believe that's the same as the menu function "Reimport all assets" , but indeed I did try AssetDatabase.Refresh and it does not do it! Happy new year! – Fattie Jan 03 '19 at 11:26
  • yeah I see .. makes sense ^^ Happy new year, too ;) – derHugo Jan 03 '19 at 11:27
  • @Fattie Did the new information you found help with coming to a solution? – Remy Mar 04 '19 at 07:50
  • no, I believe there is no solution! @remy_rm – Fattie Mar 04 '19 at 11:31

1 Answers1

7

Sadly it comes down to you can't do anything about it.

As per .net's DLL handling DLL's cannot be unloaded individually without closing the application domain. And while Unity picks up the changes done to the DLL the old version is kept in memory and used at runtime in the editor. A "funny" thing you can do to see this in action is by deleting the native plugin from within the editor. Confirm that you want to delete the file. The file will dissapear from the inspector. However if you right click the folder and refresh (ctrl+r) you will see that the file gets reconstructed (this also happens when hitting "re-import all", as the application domain isn't closed, despite unity restarting).

When building the application it will however use the locally stored file, and not the memory stored file. Hence the plugin being updated on the build.

There is no way to unload an individual assembly without unloading all of the application domains that contain it. Even if the assembly goes out of scope, the actual assembly file will remain loaded until all application domains that contain it are unloaded.

source

This has been a problem for some time now, and people have made attempts for work arounds and/or fixes, but as far as I am aware the "work arounds" that exist now are for windows only. here are some links to discussions about it.

I suppose something that could be done is writing a wrapper that automatically restarts Unity when the dll has been edited... Although this won't solve the issue it'll atleast make it somewhat less of a hassle.

Remy
  • 4,843
  • 5
  • 30
  • 60
  • 1
    Yeah it is quite a niche thing that i had stumbled upon a while back myself. It may be worth to make a feature request out of it (though even then it still depends on wether or not it is something they have control over). just as a thought; it may be possible to write an editor script that adds some sort of versioning to uploaded/updated DLL's. This would load the "new" dll into memory and update all references to the old dll to the new correct "version" number. depending on the dll size though this could cause some serious memory issues. but if it can reduce the amount of restarts needed... – Remy Jan 03 '19 at 12:18
  • 1
    @Fattie a funny thing i noticed today that supports the above: If you delete an unmanaged/native plugin in the editor, while the editor is open. It will prompt you to "really delete it as this action is irreversible". Now if you click "delete" the icon will dissapear (making you believe it is actually deleted). However! If you right-click the folder and "refresh" you will see that the file gets reconstructed from memory, and isn't actually deleted! – Remy Jan 08 '19 at 15:57
  • Good Lord! thanks for reporting that, @remy_m . It is definitely a touchy issue. – Fattie Jan 08 '19 at 16:00
  • 1
    on this issue, @remy_m .. my colleague gave me some info about it; notice my edit "More information on this:". Do you reckon that's it? – Fattie Feb 23 '19 at 12:00
  • @Fattie That does seem interesting indeed, and possibly provides a method to completely unload the bundle. However `There are a couple of cases in which a dynamic library will never be unloaded: 1) the main executable links against it`. I think this still may be an issue as Unity.exe is the main executable, and will probably stay linked to it. I think it is definitely worth a try though. Unfortunately I can't test it myself as I don't have a osx/ios device. (Also you may want to add xcode/osx tag as they may know more about it!) – Remy Feb 25 '19 at 08:49