code style

9/30/2016

How to load dynamic library with Unity plugin on Mac OSX

When develop Unity plugin on Mac, you might want to load third party dynamic library. But it usually occurs "fail to load" situation. That is because loading path is different between development and production environment.

For instance, when you develop in Edit mode, the loading path will be
 ./Assets/Plugins/osxplugin.bundle/Contents/Resources/thirdparty.dylib

But when you build a release version, the loading path will be
./Contents/Plugins/osxplugin.bundle/Contents/Resources/thirdparty.dylib

Mac OSX provide a command line tool to modify that.

$ install_name_tool -id @loader_path/thirdparty.dylib thirdparty.dylib

You could use otool to check dylib status.

eg.
$ otool -L libcurl.dylib 
libcurl.dylib:
 @loader_path/libcurl.dylib (compatibility version 0.0.0, current version 0.0.0)
 /usr/lib/libSystem.B.dylib (compatibility version 1.0.0, current version 1226.10.1)
 /usr/lib/libz.1.dylib (compatibility version 1.0.0, current version 1.2.5)

Now, you can develop and build a unity app without dylib loading issue.

沒有留言: