要实现游戏SDK的接入,首先要解决的是Unity3D与原生IOS代码之间的相互调用问题。Unity使用C#作为开发语言,而IOS采用Objective-C作为开发语言,如何让C#调用OC代码,或者让OC调用C#代码。所幸OC和C#都支持直接嵌入C/C++代码,这里使用C作为两者之间的桥梁。 为了简化两者之间的接口调用和数据传递,在设计Unity与IOS SDK之间的接口时,Unity调用SDK只有一个接口,而SDK调用Unity也只有一个接口。由于平台方的SDK差异性较大,如何保证一个接口可以解决问题?这里我们开发了一个通用SDK层,游戏只会与通用SDK层交互,而由通用SDK层再与具体的平台SDK对接。
- 新建xcode工程,导入创建好的AppVest.framework
- 创建工具类文件AppVestManager(因为framework中含有c++源码所以需要更改为AppVestManager.mm)
- AppVestManager工具类中用c代码实现桥连,使SDK中方法能在unity3d工程中调用 具体代码如下:
#import "AppVestManager.h"
#import <AppVest/AppVest.h>
//固定代码
#if defined(__cplusplus)
extern "C"{
#endif
extern void UnitySendMessage(const char *, const char *, const char *);
extern NSString* _CreateNSString (const char* string);
#if defined(__cplusplus)
}
#endif
@implementation AppVestManager
// SDK中初始化方法
-(void)initAppVest{
[[AppVest new] initAppVest];
}
// SDK中启动代理方法(有参数均可以传递参数)
-(void)startProxy{
[[AppVest new] startProxy];
}
#if defined(__cplusplus)
extern "C"{
#endif
//字符串转化的工具函数
NSString* _CreateNSString (const char* string)
{
if (string)
return [NSString stringWithUTF8String: string];
else
return [NSString stringWithUTF8String: ""];
}
char* _MakeStringCopy( const char* string)
{
if (NULL == string) {
return NULL;
}
char* res = (char*)malloc(strlen(string)+1);
strcpy(res, string);
return res;
}
static AppVestManager *myManager;
//供u3d调用的c函数 ( 因测试的sdk调用初始化为不传参方法,如果调用的sdk需要传参调用,此处应相应修改为含参数方法)
void initAppVestAndStartIt()
{
if(myManager==NULL)
{
myManager = [[AppVestManager alloc]init];
}
[myManager initAppVest];
[myManager initAppVest];
}
#if defined(__cplusplus)
}
#endif
@end
复制代码
- 在创建的unity3d工程文件夹下面的Assets文件夹下面新建名为Plugins的新文件夹,并将创建好的AppVestManager工具类拷贝过来
5.打开unity 3d工程 (1)选择左下方的Assets—>create—>c#Script新建文件名为bridgeProject文件
(2)创建好之后,点击工程左上角的Main Camera,然后在右下角的Add Component中搜索BridgeProject文件进行添加,实现与Main Canera进行绑定,这样游戏启动起来可以调用该文件中方法,进行测试调用sdk情况
(3)然后使用MonoDevelop工具打开并编写如下代码,下载Unity3D时候会连带安装(这里为了测试写了一个小按钮)
代码如下:using UnityEngine;
using System.Collections;
public class bridgeProject : MonoBehaviour {
// Use this for initialization
void Start () {
print ("this is start");
// 调用AppVestManager初始化方法,进而调用ocAppVestManager中方法实现Unity3d调用ios SDK
AppVestManager.InitSDK ();
}
// Update is called once per frame
void Update () {
}
public void OnGUI() {
//开始按钮
if(GUI.Button(new Rect(100,100,100,100),"button"))
{
//System.Console.WriteLine("hello world");
print("check button to start the appvest !");
}
}
}
复制代码
- 创建unity3d中调用oc代码工具类AppVestManager文件的 AppVestManager文件 (1)选择左下方的Assets—>create—>c#Script新建文件名为AppVestManager文件 (2) AppVestManager中编写代码调用oc方法 (3)initAppVestAndStartIt()该方法为无参数方法,若sdk中需要传参调用,此处应修改为相应含有参数方法
using UnityEngine;
using System.Runtime.InteropServices;
public class AppVestManager : MonoBehaviour {
[DllImport("__Internal")]
private static extern void initAppVestAndStartIt();
//定义接口函数供游戏逻辑调用
public static void InitSDK() {
if (Application.platform == RuntimePlatform.IPhonePlayer){ initAppVestAndStartIt();
}
}
}
复制代码
- 全部文件创建好之后,Assets目录结构如下 8.按照上篇文章《Unity3D导出为Xcode工程》进行导出为xocde工程,测试调用结果 9.打开导出后的xcode工程,把AppVest.framework添加进来,然后运行,打断点调试结合log日志显示结果成功调用SDK (其中Plugins文件夹为以C为桥连,实现在unity3d调用ios sdk)