说明:
使用平台:mac电脑
2.下载微信的开发工具包sdk导入untiy的 Assest/Plugins/iOS/WechatSDK 目录下
3.在Assest/Plugins/iOS下新建.h文件 CustomAppController.mm,为了Xcode打包的时候用自己定义的AppController,微信官方也说过要重写一些方法
// CustomAppController.mm
#import "WXApiManager.h"
#import "UnityAppController.h"
@interface CustomAppController : UnityAppController
@end
IMPL_APP_CONTROLLER_SUBCLASS (CustomAppController)
@implementation CustomAppController
- (BOOL)application:(UIApplication*)application didFinishLaunchingWithOptions:(NSDictionary*)launchOptions
{
[super application:application didFinishLaunchingWithOptions:launchOptions];
// TODO
return YES;
}
- (BOOL)application:(UIApplication *)application handleOpenURL:(NSURL *)url {
return [WXApi handleOpenURL:url delegate:[WXApiManager sharedManager]];
}
- (BOOL)application:(UIApplication *)application openURL:(NSURL *)url sourceApplication:(NSString *)sourceApplication annotation:(id)annotation {
return [WXApi handleOpenURL:url delegate:[WXApiManager sharedManager]];
}
- (BOOL)application:(UIApplication *)application continueUserActivity:(NSUserActivity *)userActivity restorationHandler:(void(^)(NSArray<id<UIUserActivityRestoring>> * __nullable restorableObjects))restorationHandler
{
return [WXApi handleOpenUniversalLink:userActivity delegate:[WXApiManager sharedManager]];
}
@end
4.在Assest/Plugins/iOS下新建WeChatUnity文件夹,处理unity和微信sdk的通信,并在此文件夹下新建.h文件:WXApiManager.h
// WXApiManager.h
#import <Foundation/Foundation.h>
#import "WXApi.h"
@interface WXApiManager : UIResponder<UIApplicationDelegate, WXApiDelegate>
+ (instancetype)sharedManager;
@end
5.在Assest/Plugins/iOS/WeChatUnity下新建WeChatUnity.mm文件,负责微信sdk的初始化和分享功能的封装
// WeChatUnity.mm
#import <Foundation/Foundation.h>
#import "WXApiManager.h"
#define UNITY_CS_API extern "C"
static NSString *mWXAppid = nil;
// 将c字符串const char* 转为 oc字符串NSString
static inline NSString * str_c2ns(const char*s)
{
if (s)
return [NSString stringWithUTF8String: s];
else
return [NSString stringWithUTF8String: ""];
}
// 初始化
UNITY_CS_API void UnityWeChatInit(const char* appId, const char* universalLink)
{
mWXAppid = str_c2ns(appId);
[WXApi registerApp:mWXAppid universalLink:str_c2ns(universalLink)];
}
// 封装分享接口
UNITY_CS_API void UnityWeChatLogin(const char* state)
{
NSLog(@"UnityWeChatLogin");
// 此时会拉起微信,授权后会回调WXApiManager的onResp方法
SendMessageToWXReq *req = [[SendMessageToWXReq alloc] init];
req.bText = YES;
req.text = @"分享的内容";
req.scene = WXSceneSession;
[WXApi sendReq:req completion:^(BOOL success){NSLog(@"微信分享:%@", success ? @"成功" : @"失败");}];
}
6.注册回调,在Assest/Plugins/iOS/WeChatUnity下新建WXApiManager.mm,编写分享的回调
#import "WXApiManager.h"
@implementation WXApiManager
// 单例
+(instancetype)sharedManager {
static dispatch_once_t onceToken;
static WXApiManager *instance;
dispatch_once(&onceToken, ^{
instance = [[WXApiManager alloc] init];
});
return instance;
}
- (void)onReq:(BaseReq *)req {
// TODO 微信回调,从微信端主动发送过来的请求
}
// WXApiManager.mm
- (void)onResp:(BaseResp *)resp {
if ([resp isKindOfClass:[SendMessageToWXReq class]]) {
NSLog(@"微信授权回调");
if (resp.errCode == 0) {
UnitySendMessage("SDKCallBack", "WeChatLoginCallback", "234");//象征性传一个234
}
else
{
// 失败,回调给Unity
UnitySendMessage("SDKCallBack", "WeChatLoginCallback", "");
}
}
}
@end
7.看到上面的回调会调用unity方法了吧,我们需要建一个SDKCallBack类,里面写个WeChatLoginCallback方法。
// SDKCallBack.cs
using UnityEngine;
public class SDKCallBack : MonoBehaviour
{
///
/// 分享回调
///
///
public void WechatLoginCallback(string callBackInfo)
{
if (string.IsNullOrEmpty(callBackInfo))
{
// TODO 分享失败,请重试
Debug.LogWarning("分享失败");
}
else
{
Debug.LogWarning("分享成功");
}
}
}
8.建立c#脚本WechatSDK.cs进行sdk的初始化,方法是UnityWeChatLogin代表分享意思
// WeChatSDK.cs
using System.Runtime.InteropServices;
using UnityEngine;
public class WeChatSDK
{
public static void Init()
{
#if UNITY_IOS
UnityWeChatInit(@"你的AppId", "你的Universal Links");
#elif UNITY_ANDROID
// TODO Android的调用
#endif
}
#if UNITY_IOS
[DllImport("__Internal")]
static extern void UnityWeChatInit(string appId, string universalLink);
#endif
// 登录
public static void Login()
{
#if UNITY_IOS
// "app_wechat"为传给分享的数据
UnityWeChatLogin("app_wechat");
#elif UNITY_ANDROID
// TODO Android的调用
#endif
}
#if UNITY_IOS
[DllImport("__Internal")]
static extern void UnityWeChatLogin(string state);
#endif
}
9.untiy这边的处理就完了,建一个Test脚本做sdk的初始化,UI按钮点击调用分享的功能吧
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.UI;
public class Test : MonoBehaviour
{
public Button loginBtn;
// Start is called before the first frame update
void Start()
{
WeChatSDK.Init();//初始化微信SDK
loginBtn.onClick.AddListener(
()=> {
Debug.Log("开始分享");
WeChatSDK.Login(); });
}
}
10.最后打包Xcode项目
1.Xcode项目的根目录打开控制台,输入命令:touch podfile,会看到这个名字的文件,打开它并编写
# Uncomment the next line to define a global platform for your project
platform :ios, '11.0'
target 'Unity-iPhone' do//'引号内为你的Xcode项目名字,我的默认叫Unity-iPhone'
# Uncomment the next line if you're using Swift or would like to use dynamic frameworks
# use_frameworks!
# Pods for 项目文件名,目前为止最新的已经到1.8.4
pod 'WechatOpenSDK'
end
2.继续在根目录控制台输入:pod install,等待完成安装微信的WechatOpenSDK
3.打开.xcworkspace工程,在info里输入微信的3个白名单,info的改变代码:
<key>LSApplicationQueriesSchemes</key>
<array>
<!-- 微信 URL Scheme 白名单-->
<string>weixinULAPI</string>
<string>weixin</string>
<string>weixinURLParamsAPI</string>
</array>
重启项目后就可以在info看到这个key
4.Xcode打包ios的签名证书啥的看其他博客,这里跳过。
5.开始打包,我打包中遇到了这些问题:
解决:这里只需要在Xcode中将BitCode改为No
还有这个错,是缺少WebKit
解决:添加webkit
最后打包测试
video
点击按钮跳转到微信分享,由于App还在审核中,我随便加的Appid和Universal Links,所以有这个错,不过我们已经可以调用打开微信进行分享了,应用审核完就填入正确的Appid和Universal Links,就不会有这个窗口了!