Unity3D 开发游戏需要接入平台方的SDK才能够正式发布,本文记录IOS SDK接入的流程。
Unity与IOS的互调
要实现游戏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对接。
Unity中调用SDK层的功能:
using System.Runtime.InteropServices;
using Cross;
using UnityEngine;
namespace MuGame
{
public class IOSPlatformSDK : IPlotformSDK
{
//!--IOS插件声明,所有unity调用ios SDK插件走这里
[DllImport("__Internal")]
public static extern void CallSDKFunc(string type, string jsonpara);
通用SDK调用返回结果给Unity
//!---------------------------外部接口声明--------------------------------------------
#if defined(__cplusplus)
extern "C"{
#endif
extern void UnitySendMessage(const char*, const char*, const char*);
#if defined(__cplusplus)
}
#endif
SDK暴露的C接口的声明与定义
#if defined(__cplusplus)
extern "C" {
#endif
//游戏层访问SDK的接口
void CallSDKFunc(char *type, char * jsonpara)
{
if(connector == NULL)
{
connector = [SDKConnector sharedInstance];
}
[connector _CallSDKFunc :CreateNSString(type) :CreateNSString(jsonpara)];
}
#if defined(__cplusplus)
}
#endif
这里CallSDKFunc即上述Unity调用SDK的接口,在OC层,直接由SDKConnector类接收,并分发处理。
通用SDK
SDKConnector负责消息的分发
- (void)_CallSDKFunc :(NSString*)type :(NSString*)jsonpara
{
NSLog(@"[SDK] Recevie cmd = %@ jsonpara = %@\n",type,jsonpara);
if ([type isEqualToString:@"login"])
{//登录
[LanPlatform login:[SDKListener sharedInstance]];
}
else if ([type isEqualToString:@"loginout"])
{//登出
[LanPlatform logout:[SDKListener sharedInstance]];
}
else if ([type isEqualToString:@"switchAccount"])
{//切换账号
[LanPlatform switchAccount:[SDKListener sharedInstance]];
}
else if ([type isEqualToString:@"pay"])
{//充值
[LanPlatform pay:[SDKListener sharedInstance]];
}
通用SDK层的功能
目前包含以下常见的模块:登录,登出,切换账号,充值,用户中心,用户论坛,用户反馈,防沉迷,实名认证。以及各项游戏数据的上报:选服,进入游戏,创建角色,升级等等。
应用生命周期SDK
SDK中比较特殊的一类,基本上也是所有SDK都需要接入的API是生命周期API,本文处理项对比较特殊一点。
@protocol SDKLifeCycleListener <NSObject>
@optional
- (void)didFinishLaunching:(NSNotification*)notification;
- (void)didBecomeActive:(NSNotification*)notification;
- (void)willResignActive:(NSNotification*)notification;
- (void)didEnterBackground:(NSNotification*)notification;
- (void)willEnterForeground:(NSNotification*)notification;
- (void)willTerminate:(NSNotification*)notification;
@end
//注册生命周期回调函数
void SDKRegisterLifeCycleListener(id<SDKLifeCycleListener> obj)
{
#define REGISTER_SELECTOR(sel, notif_name) \
if([obj respondsToSelector:sel]) \
[[NSNotificationCenter defaultCenter] addObserver:obj \
selector:sel \
name:notif_name \
object:nil \
]; \
REGISTER_SELECTOR(@selector(didFinishLaunching:), UIApplicationDidFinishLaunchingNotification);
REGISTER_SELECTOR(@selector(didBecomeActive:), UIApplicationDidBecomeActiveNotification);
REGISTER_SELECTOR(@selector(willResignActive:), UIApplicationWillResignActiveNotification);
REGISTER_SELECTOR(@selector(didEnterBackground:), UIApplicationDidEnterBackgroundNotification);
REGISTER_SELECTOR(@selector(willEnterForeground:), UIApplicationWillEnterForegroundNotification);
REGISTER_SELECTOR(@selector(willTerminate:), UIApplicationWillTerminateNotification);
#undef REGISTER_SELECTOR
}
@implementation SDKListener
//加载函数,实现在加载该类时,注册生命周期监听函数
+(void)load
{
NSLog(@"[SDK] load\n");
SDKRegisterLifeCycleListener([SDKListener sharedInstance]);
}
小结
至此,大概介绍了一下所实现的IOS SDK的方法。简而言之,unity通过CallSDKFunc调用通用SDK功能,通用SDK通过SDKConnector分发消息给具体的通用SDK模块,再由平台SDK处理。SDKListener负责接收平台处理的结果和生命周期事件,并将需要的结果返回给Unity。
就目前的实现来看,能够较好的相对解耦游戏与SDK之间的联系,不会因平台的差异性导致游戏代码的频繁改动。当然目前接入的SDK还相对较少还需测试。
亿速云「云服务器」,即开即用、新一代英特尔至强铂金CPU、三副本存储NVMe SSD云盘,价格低至29元/月。点击查看>>
免责声明:本站发布的内容(图片、视频和文字)以原创、转载和分享为主,文章观点不代表本网站立场,如果涉及侵权请联系站长邮箱:is@yisu.com进行举报,并提供相关证据,一经查实,将立刻删除涉嫌侵权内容。