說到有關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"{ #endif extern 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"{ #endif static 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