Unity IOS打包,定制UnityAppController,添加微信支付宝SDK库

来源:互联网 发布:宽带已连接但是无网络 编辑:程序博客网 时间:2024/05/18 07:19
http://www.xuanyusong.com/archives/4026

http://blog.csdn.net/blog_lee/article/details/52400535

http://www.jianshu.com/p/f347e00abb9c

http://starzero.hatenablog.com/entry/2014/02/24/182646

http://eppz.eu/blog/override-app-delegate-unity-ios-osx-1/

已测试过,导出之后除了配置开发证书开发小组以外不用在XCode里手动修改任何项目设置
一、定制UnityAppController
①修改好的UnityAppController.h(只要修改NSObject<UIApplicationDelegate,WXApiDelegate>),复制到Assets\Plugins\iOS\UnityAppController.h

这样虽然不会删除Unity自带的UnityAppController.h,但是会让它无效,可能和头文件搜索顺序有关,可以试试把原来Classes的UnityAppController.h里面乱改几个错误的文字,也能编译通过。

②MyUnityAppController.mm复制到Assets\Plugins\iOS\MyUnityAppController.mm

#import "UnityAppController.h"#import "SDKExport/WXApiObject.h"#import "SDKExport/WechatAuthSDK.h"#import "AlipaySDK/AlipaySDK.h"#import "UnityInterface.h"#include <wchar.h>#define ksendAuthRequestNotification @"ksendAuthRequestNotification"// 给Unity3d调用的方法extern "C" void IosWeixinAuthLogin(){    // 登录    [[NSNotificationCenter defaultCenter] postNotificationName:ksendAuthRequestNotification object:nil];}NSString* UnicodeToNsstr( const wchar_t *wszUnicode ){    size_t sizeUnicode = (wcslen(wszUnicode)+0)*sizeof(wchar_t);    NSString *nsstr = [[NSString alloc] initWithBytes:wszUnicode length:sizeUnicode encoding:NSUTF16LittleEndianStringEncoding];    return nsstr;}NSString* g_nssWeixinMchId = @"123456789";//微信商户号extern "C" void IosWeixinRecharge( const wchar_t * szPrepayId,  const wchar_t * szNonce , const wchar_t * szTimeStamp, const wchar_t * szSign){    //调起微信支付    PayReq* req             = [[PayReq alloc] init];//autorelease];    req.partnerId           = g_nssWeixinMchId;    req.prepayId            = UnicodeToNsstr(szPrepayId);    req.nonceStr            = UnicodeToNsstr(szNonce);    req.timeStamp           = [UnicodeToNsstr(szTimeStamp) longLongValue];    req.package             =  @"Sign=WXPay";    req.sign                = UnicodeToNsstr(szSign);    [WXApi sendReq:req];}extern "C" void IosAlipayRecharge(const wchar_t * szAlipayOrderInfo){    NSString *appScheme = @"Abcd";        // NOTE: 将签名成功字符串格式化为订单字符串,请严格按照该格式    NSString *orderString = UnicodeToNsstr(szAlipayOrderInfo);//[NSString stringWithFormat:@"%@&sign=%@",orderInfoEncoded, signedString];        // NOTE: 调用支付结果开始支付    [[AlipaySDK defaultService] payOrder:orderString fromScheme:appScheme callback:^(NSDictionary *resultDic) {        NSLog(@"reslut = %@",resultDic);    }];}@interface MyUnityAppController : UnityAppController@end@implementation MyUnityAppController- (BOOL)application:(UIApplication *)application handleOpenURL:(NSURL *)url {    return  [WXApi handleOpenURL:url delegate:self];}- (BOOL)application:(UIApplication *)application openURL:(NSURL *)url sourceApplication:(NSString *)sourceApplication annotation:(id)annotation {    return [WXApi handleOpenURL:url delegate:self];}- (void)onReq:(BaseReq *)req // 微信向第三方程序发起请求,要求第三方程序响应{}- (void)onResp:(BaseResp *)resp // 第三方程序向微信发送了sendReq的请求,那么onResp会被回调{    if([resp isKindOfClass:[SendAuthResp class]]) // 登录授权    {        SendAuthResp *temp = (SendAuthResp*)resp;        if(temp.code!=nil)            UnitySendMessage("Login", "WeixinAccessToken" , [temp.code cStringUsingEncoding:NSUTF8StringEncoding]);//        [self getAccessToken:temp.code];    }    else if([resp isKindOfClass:[SendMessageToWXResp class]])    {        // 分享        if(resp.errCode==0)        {            NSString *code = [NSString stringWithFormat:@"%d",resp.errCode]; // 0是成功 -2是取消            NSLog(@"SendMessageToWXResp:%@",code);            //UnitySendMessage(GameObjectName, ShareMethod, [code cStringUsingEncoding:NSUTF8StringEncoding]);        }    }}- (BOOL)application:(UIApplication*)application didFinishLaunchingWithOptions:(NSDictionary*)launchOptions{    [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(sendAuthRequest) name:ksendAuthRequestNotification object:nil]; // 微信//向微信注册      [WXApi registerApp:@"wxasd4f6s5df4s65d" ];    return [super application:application didFinishLaunchingWithOptions:launchOptions];}static NSString *kAuthOpenID = @"asdfsdfasdfasd5f55";- (void)sendAuthRequest{        SendAuthReq* req = [[SendAuthReq alloc] init];// autorelease];        req.scope = @"snsapi_userinfo";    req.state = @"only123";    req.openID = kAuthOpenID;        [WXApi sendAuthReq:req viewController:_rootController delegate:self];}@endIMPL_APP_CONTROLLER_SUBCLASS( MyUnityAppController )




二、AlipaySDK.bundle、AlipaySDK.framework、SDKExport三个文件夹,复制到Assets\Plugins\iOS\

不用任何设置AlipaySDK.framework和SDKExport\libWeChatSDK.a就会自动加入XCode链接库


三、Unity Editor编辑器脚本增加PostProcessBuild导出XCode的IOS脚本

//添加系统framework//PBX项目设置{string projPath = UnityEditor.iOS.Xcode.PBXProject.GetPBXProjectPath (path);var proj = new UnityEditor.iOS.Xcode.PBXProject ();proj.ReadFromString (System.IO.File.ReadAllText (projPath));var target = proj.TargetGuidByName (UnityEditor.iOS.Xcode.PBXProject.GetUnityTargetName ());// システムのフレームワークを追加proj.AddFrameworkToProject (target, "CoreTelephony.framework", false);proj.AddFrameworkToProject (target, "Security.framework", false);//微信登录proj.AddFrameworkToProject (target, "libstdc++.6.0.9.tbd", false);proj.AddFrameworkToProject (target, "libz.tbd", false);//微信登录proj.AddBuildProperty(target, "OTHER_LDFLAGS", "-ObjC");//微信登录// 書き出しSystem.IO.File.WriteAllText (projPath, proj.WriteToString ());}//Handle info.plist{var plistPath = path + "/Info.plist";var plist = new UnityEditor.iOS.Xcode.PlistDocument ();plist.ReadFromString (System.IO.File.ReadAllText (plistPath));var rootDict = plist.root;var arrAqs = rootDict.CreateArray ("LSApplicationQueriesSchemes");//微信登录arrAqs.AddString("weixin");//微信登录var arrUrlTypes = rootDict.CreateArray ("CFBundleURLTypes");{//微信通知回调var item0 = arrUrlTypes.AddDict ();item0.SetString ("CFBundleURLName", "weixin");item0.SetString ("CFBundleTypeRole", "Editor");var arrUrlSchemes = item0.CreateArray ("CFBundleURLSchemes");arrUrlSchemes.AddString ("wxasdfswsdf89sf");}{//支付宝通知回调var item1 = arrUrlTypes.AddDict ();item1.SetString ("CFBundleURLName", "Abcd");//这里对应支付宝 NSString *appScheme = @"Abcd";item1.SetString ("CFBundleTypeRole", "Editor");var arrUrlSchemes = item1.CreateArray ("CFBundleURLSchemes");arrUrlSchemes.AddString ("Abcd");}System.IO.File.WriteAllText (plistPath, plist.WriteToString ());}