创建Unity3D的MacOS Plugin的正确姿势
http://www.tedlindstrom.se/how-to-link-dylibs-into-unity3d/
I was roaming around the net looking for a simple answer on how to link a dylib into Unity3D, without finding a simple answer. It seemed really complicated. I am used to developing on Windows and to add a DLL into Unity3D, you simply just drop the DLL int he Plugins folder and there you go.
For Mac OS X it turned out to be a bit more complicated but I managed to find a solution that worked and I will show it to you here so that You won’t have to go through the same trouble as I did.
On Mac OSX, plugins are deployed as bundles. Create the bundle project with XCode by selecting File->NewProject… and then selecting Bundle -> Carbon/Cocoa Loadable Bundle (in XCode 3) or OS X -> Framework & Library -> Bundle (in XCode 4)
My bundle will be named Cereproc. First thing to do is to go to Build settings and chose 32 bit architecture instead of 64bit. This is very important step. Unity3d will not read 64 bit bundles. Now go to the Build Phases tab for your active target.
Now open up the Finder and copy your dylibs to the project-folder. These dylibs will be manipulated by a script so be sure to save the old ones as backup.
Now link these binaries with the bundle by adding them to “Link Binaries With Libraries”. These libraries are now linked to library and xcode will compile the code, but there is still a few steps to make the bundle work with Unity3D.
Still in the Build Phases tab, add a new Build Phase step. Locate the button in the bottom right corner “Add Build Phase”. Add Run Script. The buildphases will be executed in a linear order as represented by the order they are listed in the Build Phases tab. We wan’t the script to be executed as early as possible so drag and drop the Run Script build phase to the top of the list, but below “Target Dependencies”

As you can see, I have added 5 dylibs. and have a “Run Script” build phase placed as early as possible in the list of build phases. Now we will do some scripting.
Add following code for each one of your dylib files. Replace libcerevoice_aud_shared.dylib with your respective dylib.
echo OTOOL BEGIN
otool -L libcerevoice_aud_shared.dylib
echo OTOOL END
Compile the code and go to the log navigator and watch the log.

Watch the OTOOL printout for libcerevoice_eng_shared.dylib.
The first line is the “ID” of the library itself. It’s also the working directory for it telling the library itself where it’s living. In my case it’s set to “/temp/trunk/cerevoice_eng/lib/libcerevoice_eng_shared.dylib”. This means that the dylib has to be located in this folder for my bundle to be able to accually load it.
Now read the rest of the lines. They are dependencies that this library rely on. Three of them is pointing towards other dylibs that I have located. This is the tricky part about building bundles and applications on Mac OS X. Atleast it seems tricky for who are used to working with DLLs on Windows. Anyway we need to modify these links now.
Mac OS X provides a tool for this called “install_name_tool”. We need to go through each one of our dylib files and modify the paths to point to a relative path instead of a fixed path as now.
Start by modifying all IDs of all the files. This is how my script looks like. I think you can figure out how to apply it on your files. Add it to the top of script in the the Run Script Build Phase.
install_name_tool -id @loader_path/libcerevoice_aud_shared.dylib libcerevoice_aud_shared.dylib
install_name_tool -id @loader_path/libcerevoice_shared.dylib libcerevoice_shared.dylib
install_name_tool -id @loader_path/libcerehts_shared.dylib libcerehts_shared.dylib
install_name_tool -id @loader_path/libcerevoice_pmod_shared.dylib libcerevoice_pmod_shared.dylib
install_name_tool -id @loader_path/libcerevoice_eng_shared.dylib libcerevoice_eng_shared.dylib
Note that this will modify the dylib itself, that is why I told you to save a backup incase something goes wrong.
Compile the code and watch the output by otool again.

If you did everything right, your output should be modified. You can see from my image that the id is now pointing to @loader_path/libcerevoice_eng_shared.dylib.
@loader_path is replaced with the path to the directory containing the mach-o binary which contains the load command using @loader_path. @loader_path is useful as the load path for a framework/dylib embedded in a plug-in, if the final file system location of the plugin-in unknown (so absolute paths cannot be used).
Next step is to modify the links to the dependencies. This is how my script looks like, apply it for your use. You will have to read the output from the otool to decide which links you have, if any at all. In my case I have a total of 7 links in all 5 dylibs that I fixed. If your lucky you got none. Here is my script:
install_name_tool -change /tmp/trunk/cerevoice/lib/libcerevoice_shared.dylib @loader_path/libcerevoice_shared.dylib libcerevoice_shared.dylib
install_name_tool -change /tmp/trunk/cerehts/lib/libcerehts_shared.dylib @loader_path/libcerehts_shared.dylib libcerevoice_eng_shared.dylib
install_name_tool -change /tmp/trunk/cerevoice/lib/libcerevoice_shared.dylib @loader_path/libcerevoice_shared.dylib libcerevoice_eng_shared.dylib
install_name_tool -change /tmp/trunk/cerevoice_pmod/lib/libcerevoice_pmod_shared.dylib @loader_path/libcerevoice_pmod_shared.dylib libcerevoice_eng_shared.dylib
install_name_tool -change /tmp/trunk/cerevoice_pmod/lib/libcerevoice_pmod_shared.dylib @loader_path/libcerevoice_pmod_shared.dylib libcerevoice_pmod_shared.dylib
install_name_tool -change /tmp/trunk/cerevoice/lib/libcerevoice_shared.dylib @loader_path/libcerevoice_shared.dylib libcerevoice_pmod_shared.dylib
install_name_tool -change /tmp/trunk/cerevoice/lib/libcerevoice_shared.dylib @loader_path/libcerevoice_shared.dylib libcerehts_shared.dylib
Compile the code.

And here it is. All pointing to a relative path of @loader_path. Just a few more steps now!
Load up Build Phases tab again and Add a new Build Phase, Add Copy Files.
Set the destination to “Executables” and then add the dylibs.
Last step now, add a new C-fle to the project. It does not contain any code but its needed for the linking to work. I added a c-file named “Cereproc.c”. Go to Build Phases and add the script to “Compile Sources” build phase.
Compile and there you have it! Your bundle is ready for use with Unity3D.
Load up Unity and add the bundle to the Plugins folder.
Create a c# script and hook up all the functions in the dylibs by using the name of the bundle. In my case it looks like this:
[DllImport (“Cereproc”)
private static extern CPRCEN_engine CPRCEN_engine_new();
This function is accually in one of the dylibs but it works by importing “Cereproc”.
I hope this helped you out. Good luck creating plugins for Unity3D! 
创建Unity3D的MacOS Plugin的正确姿势的更多相关文章
- Ubuntu创建新用户的正确姿势
作者按:因为教程所示图片使用的是 github 仓库图片,网速过慢的朋友请移步<Ubuntu 创建新用户的正确姿势>原文地址.更欢迎来我的小站看更多原创内容:godbmw.com,进行&q ...
- Python创建二维列表的正确姿势
Python创建二维列表的正确姿势 简介 Python中没有数组,使用list结构代替,并且list结构的功能更加强大: 支持动态扩容,无需担心元素过量 对list内的元素类型不做一致性约束 提供丰富 ...
- 程序员取悦女朋友的正确姿势---Tips(iOS美容篇)
前言 女孩子都喜欢用美图工具进行图片美容,近来无事时,特意为某人写了个自定义图片滤镜生成器,安装到手机即可完成自定义滤镜渲染照片.app独一无二,虽简亦繁. JH定律:魔镜:最漂亮的女人是你老婆魔镜: ...
- 开发函数计算的正确姿势 —— 使用 Fun Local 本地运行与调试
前言 首先介绍下在本文出现的几个比较重要的概念: 函数计算(Function Compute): 函数计算是一个事件驱动的服务,通过函数计算,用户无需管理服务器等运行情况,只需编写代码并上传.函数计算 ...
- 代码走查25条疑问 C# 跳转新的标签页 C#线程处理 .Net 特性 attribute 学习 ----自定义特性 看懂 ,学会 .NET 事件的正确姿势-简单版
代码走查25条疑问 代码走查(Code Review) 是一个开发人员与架构师集中讨论代码的过程.通过代码走查可以提高代码的 质量,同时减少Bug出现的几率.但是在小公司中并没有代码走查的过程在这 ...
- 程序员取悦女票的正确姿势---Tip1(iOS美容篇)
代码地址如下:http://www.demodashi.com/demo/11695.html 前言 女孩子都喜欢用美图工具进行图片美容,近来无事时,特意为某人写了个自定义图片滤镜生成器,安装到手机即 ...
- 剖析和解决Python中网络粘包的正确姿势
目录 1.粘包及其成因 1.1.粘包产生 1.2.粘包产生的原因 2.尝试解决粘包 2.1.指定数据包的长度 2.2.固定数据包的长度 2.3.用函数实现多次调用发送数据 3.解决粘包问题的正确姿势 ...
- laravel-nestedset:多级无限分类正确姿势
laravel-nestedset:多级无限分类正确姿势 laravel-nestedset是一个关系型数据库遍历树的larvel4-5的插件包 目录: Nested Sets Model简介 安 ...
- Ubuntu 解决wifi无法打开的问题 安装NVIDIA显卡驱动的正确姿势
游戏本型号Y7000 win10 Ubuntu16.04双系统 解决wifi无法打开的问题 解决方法: 1.打开终端输入:rfkill list all 出现如下提示:: 可以看到,优先级 ...
随机推荐
- UITextField set placeholderColor and UITextField set clearButton Image
self.usernameTextField.tintColor = [UIColor whiteColor]; [self.usernameTextField setValue:UIColorFro ...
- 如何解决inline和linline-block在浏览器中的间距问题
写页面时,如果想要元素从左到右排列,但又不想使用浮动,那么很多人都会用到display:inline或者display:inline-block. 但是每次一用到这个两个属性,浏览器中浏览的时候就会有 ...
- ThinkPHP 隐藏URL中的 index.php
去掉 URL 中的 index.php 通常的URL里面含有index.php,为了达到更好的SEO效果可能需要去掉URL里面的index.php ,通过URL重写的方式可以达到这种效果,通常需要服务 ...
- 现代软件工程作业 github使用
Github使用 版本库的创建与同步 第一步:创建远程版本库并同步到本地 创建远程版本库 在地址栏输入www.github.com 并sign in 进入到个人主页,如下图示: 创建远程版本库:点击N ...
- [从产品角度学EXCEL 03]-单元格的秘密
这是<从产品角度学EXCEL>系列——单元格的秘密. 前言请看: 0 为什么要关注EXCEL的本质 1 EXCEL是怎样运作的 2 EXCEL里的树形结构 或者你可以去微信公众号@尾巴说数 ...
- rpm查询命令摘要
任务 命令 显示软件包的相关信息 rpm -q -i NAME 列出软件包中含有的所有文件 rpm -q -i NAME 列出软件包中含有的配置文件 rpm -q -c NAME 列出软件包中含有的文 ...
- C和指针 第十五章 错误报告perror和exit
15.1 错误报告 perror 任何一种程序都存在出错的可能,包括系统的函数库,当出现错误时,系统提示发生错误,标准库函数在一个外部整型变量中保存错误代码,然后把错误代码传给用户程序,提示错误原因. ...
- pycharm快捷键及一些常用设置
pycharm快捷键及一些常用设置,有需要的朋友可以参考下. Alt+Enter 自动添加包 Ctrl+t SVN更新 Ctrl+k SVN提交 Ctrl + / 注释(取消注释)选择的行 Ctrl+ ...
- Linux学习之九--mysql的简单使用
mysql创建数据库时指定编码很重要,很多开发者都使用了默认编码,但是我使用的经验来看,制定数据库的编码可以很大程度上避免倒入导出带来的乱码问题. 一.创建数据库: 指定UTF-8: CREATE D ...
- 【Unity3d】3d网页游戏场景打包与加载
http://www.cnblogs.com/dosomething/archive/2012/04/07/2436353.html 3d游戏中 一个场景往往比较大 如果游戏的进行需要下载一个10 ...