说到有关dllimport方法可能还有很多人比较陌生,其实我自己也说不太清楚,大概说说什么时候要用它。
事实上功能类似于调用android的第三包,我们想要使用苹果上特定的api或者第三方平台的一些东西(现在很少第三方平台会为unity做独立的插件吧),我们就会用到这个叫dllimport的功能。
今天接了一下腾讯的平台,就拿它来做一个记录好了,免得我到时候又忘记了。
ios用的是obj-c,说实话对这个语言一窍不通还,大概看了一下,把握了一下demo里面的使用方法,所以有一些SB的地方也请原谅。
说说原理吧先~
在xcode中创建一个类文件,里面写好我们需要调用的方法,比如登陆啊,获取个人信息啊之类的方法,这些都是调用第三方包里腾讯封装好的功能:
熟悉obj-c的应该一眼就明白了。这个就是需要登陆验证的方法,就先来试试怎么调用这个方法吧~
现在把您手上的unity项目切换一下平台到ios,然后把他build出来,事实上这个时候我们得到的是一个xcode项目。(我这里习惯这个顺序,您也可以等会儿把我的方法倒过来顺序做)。
用xcode打开他~
在class文件夹下建一个obj-c的普通类,取名叫TencentConector得了。在xcode我们会得到两个文件一个“TencentConector.h”一个“TencentConector.m”。
现在按照腾讯的在线文档说的加入sdk中的三个文件夹(怎么加入就不细说了,或者我有空会补充)。
接着在同样按照官方说明在头文件(.h那个)里面创建两个变量。
上截图:
到这里其实跟官方的demo是一样的,没有说明差别。
接着是书写.m这个文件了:
我这里需要调用到3个方法,分别是初始化、验证、和获取个人信息。
这三个方法其实除了名称我变一下,内容就是抄demo里面的了,都是调用腾讯自己的api。
我们可以把这里的方法理解成是ios自己的方法,这些方法由于unity里面不能调用,所以我们现在引用dllimport的方法引用这三个方法。
上dllimport关联的方法
注意我这里是在@end后面开始写外链方法,方法名都以Obj_开头表示是给unity引用的obj-c方法。
我创建了一个静态变量,用来调用上面所写的方法。
接着补充一下腾讯的一些回调方法,和调用方法放在一块:
大家可能注意到我这里使用了之前说的UnitySendMassage方法。
好吧,为了调用这个方法,我们需要同样dllimport他一下
于是整个文件就是这样了:
// // TencentConector.m // tencentOAuthDemo // // Created by MacMini on 12-12-14. // // #import "TencentConector.h"#if defined(__cplusplus) extern "C"{ #endifextern void UnitySendMessage(const char *, const char *, const char *);#if defined(__cplusplus) } #endif@implementation TencentConector-(void)TencentInit{NSLog(@"***********tencentInit");_permissions = [[NSArray arrayWithObjects:@"get_user_info",@"add_share", @"add_topic",@"add_one_blog", @"list_album",@"upload_pic",@"list_photo", @"add_album", @"check_page_fans",nil] retain];_tencentOAuth = [[TencentOAuth alloc] initWithAppId:@"100266567"andDelegate:self];_tencentOAuth.redirectURI = @"www.qq.com";}-(void)TencentOAuth{NSLog(@"***********tencentAuth");[_tencentOAuth authorize:_permissions inSafari:NO]; }-(void)GetUserInfo{[_tencentOAuth getUserInfo]; }/*** Called when the user successfully logged in.*/ - (void)tencentDidLogin {// 登录成功[self unity3dSentor:@"DidLogin" param:@" "]; }/*** Called when the user dismissed the dialog without logging in.*/ - (void)tencentDidNotLogin:(BOOL)cancelled {if (cancelled){//@"用户取消登录";[self unity3dSentor:@"LoginCancel" param:@" "];}else {//@"登录失败";[self unity3dSentor:@"LoginFail" param:@" "];}}/*** Called when the notNewWork.*/ -(void)tencentDidNotNetWork {//@"无网络连接,请设置网络";[self unity3dSentor:@"NotNetWork" param:@" "];}/*** Called when the get_user_info has response.*/ - (void)getUserInfoResponse:(APIResponse*) response {if (response.retCode == URLREQUEST_SUCCEED){NSMutableString *str=[NSMutableString stringWithFormat:@""];for (id key in response.jsonResponse) {[str appendString: [NSString stringWithFormat:@"%@:%@\n",key,[response.jsonResponse objectForKey:key]]];}[self unity3dSentor:@"UserInfoResponse" param:str];}else {[self unity3dSentor:@"UserInfoResponseFail" param:@" "];} }-(void)unity3dSentor:(NSString *)methonName param:(NSString *)message{UnitySendMessage("TencentReceiver", [methonName UTF8String], [message UTF8String]); }@end#if defined(__cplusplus) extern "C"{ #endifstatic TencentConector *sTencentConector;void Obj_TencentInit(){if(sTencentConector == NULL){sTencentConector = [[TencentConector alloc] init];}[sTencentConector TencentInit];}void Obj_TencentOAuth(){if(sTencentConector == NULL){sTencentConector = [[TencentConector alloc] init];}[sTencentConector TencentOAuth];}void Obj_GetUserInfo(){if(sTencentConector == NULL){sTencentConector = [[TencentConector alloc] init];}[sTencentConector GetUserInfo];}#if defined(__cplusplus)} #endif
需要的话可以下载:
头文件:http://www.kuaipan.cn/file/id_12421281643248500.htm
m文件:http://www.kuaipan.cn/file/id_12421281643248503.htm
最后就是在unity里面通过调用申明好的外链方法实现两部分关联了:
创建一个Tencent的类,直接上代码:
using System.Runtime.InteropServices; using UnityEngine;public class Tencent {[DllImport("__Internal")]private static extern void Obj_TencentInit();[DllImport("__Internal")]private static extern void Obj_TencentOAuth();[DllImport("__Internal")]private static extern void Obj_GetUserInfo();/// <summary>/// 初始化接入准备/// </summary>public static void TencentInit(){if (Application.platform == RuntimePlatform.IPhonePlayer){Obj_TencentInit();}}/// <summary>/// qq登陆验证/// </summary>public static void TencentOAuth(){if (Application.platform == RuntimePlatform.IPhonePlayer){Obj_TencentOAuth();}}/// <summary>/// 获取用户信息/// </summary>public static void GetUserInfo(){if (Application.platform == RuntimePlatform.IPhonePlayer){Obj_GetUserInfo();}}}
|
对于外链方法我们再用一个静态方法进一步封装,方便其他类调用。
比如我可以通过按钮点击触发:
if(GUILayout.Button("Tencent Init")) {Tencent.TencentInit(); }if(GUILayout.Button("QQ Author")) {Tencent.TencentOAuth(); }if(GUILayout.Button("QQ Info")) {Tencent.GetUserInfo(); }
现在在build一个版本到刚刚的位置,注意:不要覆盖(replace),而是整合(append)。
现在就可以实现基本的调用了,然后是回调。
回调时我们通过UnitySendMassage方法想一个叫TencentReceiver的gameobject发送了信息,所以我们在unity当前场景需要创建一个同名的gameobject,为他附上一个脚本,姑且也叫TencentReceiver。
using UnityEngine; using System.Collections; using LitJson;public class TencentReceiver : MonoBehaviour {public GUIText _Text;void DidLogin(string pa){this._Text.text = "login ok";}void LoginCancel(string pa){this._Text.text = "LoginCancel";}void LoginFail(string pa){this._Text.text = "LoginFail";}void NotNetWork(string pa){this._Text.text = "NotNetWork";}void UserInfoResponse(string message){this._Text.text = "UserInfoResponse";string[] tData = message.Split(new char[1]{':'});for(int i = 0; i < tData.Length; i ++){Debug.Log(tData[i]);}}void UserInfoResponseFail(string pa){this._Text.text = "UserInfoResponseFail";} }
姑且草草写了一下,赶着下班。现在发布吧,试试就知道了~
然后是刚刚几个源码文件:
Tencent.cs:http://www.kuaipan.cn/file/id_12421281643248499.htm
TencentReceiver:http://www.kuaipan.cn/file/id_12421281643248502.htm
好了,先拜了,有事qq我吧~344404266