UNITY3D与iOS交互解决方案
时间:2014-05-02 16:58:42
收藏:0
阅读:555
原地址:http://bbs.18183.com/thread-456979-1-1.html
本帖最后由 啊,将进酒 于 2014-2-27 11:17
编辑
“授人以鱼,不如授人以渔”,以UNITY3D调用iOS版的91SDK为例,利用C# / C / OBJ-C 交互原理,本文将详细介绍UNITY3D与iOS之间交互的解决方案。
首先,打开XCODE新建一个空项目,参照91SDK开发文档将91SDK引入并进行环境配置。
配置完毕后创建一个名为“LsSdkConector”的OBJ-C
CLASS文件。完毕后,当前项目应如图:
<ignore_js_op>


LsSdkConector.h文件我们不用去改动它。保持原样即可。
<ignore_js_op>


接下来我们开始在LsSdkConector.m
文件中制作C接口 和 调用91SDK的iOS方法。
引入必要的头文件后,我把该文件的代码分为3个区域。
代码A区域用来定义C接口必须要实现的API。
<ignore_js_op>


代码B区域用来实现A区域代码的接口,以及要调用91SDK的iOS方法,直白点说,代码B区域写的方法就是用来调用代码C区域的方法。
<ignore_js_op>


代码C区域用来实现真正的91SDK的OBJ-C方法。
<ignore_js_op>


大家在自己的项目中:
代码A区域作为可直接复制套用;
代码B区域可编写工具方法和调用iOS的方法;
代码C区域编写91SDK的iOS方法,你需要什么API就写什么,写完了在代码B区域添加C的调用方法即可。
在编写代码中,有的人可能会问OBJ-C中有的API有回调,UNITY是如何知道OBJ-C回调结束呢?
答:OBJ-C调用UNITY的API,可以用“UnitySendMessage”的方法 ,具体使用方法
看UINTY的使用帮助。
这样我们完成了C接口和91SDK的API代码,接下来我们做UINITY3D方面的代码处理。
我们在UNITY3D项目的ASSETS下新建Code文件夹与Plugins文件夹。
<ignore_js_op>


将LsSdkConector.h 和
LsSdkConector.m 复制到 Plugins 文件夹中,并在 Code文件夹中编写我们的UNITY调用OBJ-C
方法。
新建C#文件“LsSdkConector.cs”,这个文件中,我们将代码分为A、B两区。
代码A区编写C语言的接口,这里的接口方法要对应”LsSdkConector.m”中代码区域B的方法名称。
<ignore_js_op>


代码B区编写UNITY3D调用OBJ-C的方法,这里的代码注意用来实现UNITY3D之间的方法调用,C#的方法内调用C的方法。
<ignore_js_op>


这样,UNITY3D接入91SDK-iOS版的解决方案到此完成。
<ignore_js_op>


下面,我们测试一下这个解决方案是否管用。
在Code文件夹中新建
“LisiTest.cs”文件,编辑如下代码:
<ignore_js_op>


并将该C#文件拖至Main Camera
绑定。
UNITY3D转iOS项目必须要设置BundleIdentifier,截图:
<ignore_js_op>


点击“Build”按钮创建iOS项目,打开该iOS项目,将LsSdkConector.h 和
LsSdkConector.m拖入iOS项目的Classes文件夹中
<ignore_js_op>


按照91SDK文档中环境配置章节,对SDK进行引用,添加系统库,做环境配置。
打包到iOS设备前,检查一下iOS项目的配置,注意OBJC要写在第1列,还有要注意大小写。
<ignore_js_op>


<ignore_js_op>


选择使用iOS设备打包,你会发现UNITY3D调用iOS 成功了!!
本文要告诉大家的是Unity3D与iOS交互的解决方案,代码规范问题不是本文重点,比如91SDK的初始化在SDK文档上推荐是在AppDelegate的文件中进行初始化。
Unity3D与Android之间的交互可以参考我的另一篇文章:《实操UNITY3D接入91SDK安卓版》
评论(0)