unity3d接入ANDROID相关SDK请参考:http://blog.youkuaiyun.com/yang8456211/article/details/51331358
unity3d接入IOS相关SDK
下面以微信分享sdk为例介绍:
首先就是在c#这边定义接口
using UnityEngine;
using LitJson;
using System.Runtime.InteropServices;
public class ShareMgr : MonoBehaviour {
//声明unity--to--ios的函数
#if UNITY_IOS
[DllImport
("__Internal")]
private static extern void RegisterWeChat(string
wxAppID);
[DllImport ("__Internal")]
private static extern void ShareWithWeChat(string
content);
#endif
///
/// 注册的微信id
///
string wxAppID =
"test6668888";
///
/// 分享之后的lua回调
///
System.Action
luaResponseCall;
///
/// 默认分享内容
///
string defaultContent="666888";
public void
ShareWeChat(string content)
{
if (string.IsNullOrEmpty(content))
{
content =
defaultContent;
}
#if UNITY_IOS
ShareWithWeChat(content);
#endif
}
///
/// 注册分享之后的回调
///
///
public void
RegLuaResponse(System.Action luaResponseCall)
{
this.luaResponseCall = luaResponseCall;
}
///
/// 删除注册的回调
///
public void
UnRegLuaResponse()
{
luaResponseCall = null;
}
void Awake()
{
this.name = "ShareMgr";
DontDestroyOnLoad(gameObject);
Init();
}
void Init()
{
#if UNITY_IOS
RegisterWeChat(wxAppID);
#elif UNITY_ANDROID
AndroidJavaClass jc = new
Android;
Android;
jo.Call("RegisterWeChat", wxAppID);
#endif
}
void
ShareCallResponse(int state)
{
if (luaResponseCall != null)
{
luaResponseCall(state);
}
}
}
再者就是调用oc的方法:
//
// ShareMgr.h
//
// Copyright . All rights reserved.
//
#import
#import "WXApi.h"
#import "AppDelegateListener.h"
typedef NS_ENUM(int,ErrorCode) {
ERROR_CODE_NO_INSTALL=-101,//没有安装微信客户端,分享失败!
ERROR_CODE_SHARE_SUCCESS=0,//分享成功
ERROR_CODE_SHARE_CANCEL=-2,//分享失败
ERROR_CODE_SHARE_FAIL=-3,//分享失败
};
@interface ShareMgr : NSObject
{
}
@property (nonatomic, strong) UIActivityIndicatorView
*indicatorView;
@property (nonatomic) BOOL waitingResponse;
@property (nonatomic) const char*targetMgr;
@property (nonatomic) const char*targetMethod;
-(void)ShareWithWeChat:(NSString*)content;
+ (instancetype)Instance;
@end
//
// ShareMgr.mm
//
// Copyright . All rights reserved.
//
#import
#import "ShareMgr.h"
#include "UnityAppController.h"
#if defined (__cplusplus)
extern "C" {
#endif
extern void
ShareWithWeChat(const char* content);
extern void
RegisterWeChat(const char* wxAppID);
#if defined (__cplusplus)
}
#endif
@implementation ShareMgr
static ShareMgr*_instance=NULL;
-(id)init
{
if ((self = [super
init]))
{
self.targetMgr = [@"ShareMgr" UTF8String
];
self.targetMethod = [@"ShareCallResponse"
UTF8String];
}
return self;
}
+ (instancetype)Instance
{
static dispatch_once_t
onceToken;
dispatch_once(&onceToken, ^{
_instance = [[ShareMgr alloc] init];
UnityRegisterAppDelegateListener(_instance);
});
return _instance;
}
-(void)ShareWithWeChat:(NSString*)content
{
if
(self.waitingResponse){return;}
if([WXApi
isWXAppInstalled])
{
WXMediaMessage *message = [WXMediaMessage
message];
[message setThumbImage:[UIImage
imageNamed:@"Icon@2x.png"]];
WXImageObject *ext = [WXImageObject
object];
NSArray *paths =
NSSearchPathForDirectoriesInDomains(NSDocumentDirectory,
NSUserDomainMask, YES);
NSString *documentsDirectory = [paths
objectAtIndex:0];
NSMutableString *StringFullPath =
[[NSMutableString alloc] initWithString:@""];
[StringFullPath
appendString:documentsDirectory];
[StringFullPath
appendString:@"/temp_capture_screen.png"];
ext.imageData = [NSData
dataWithContentsOfFile:StringFullPath];
UIImage* image = [UIImage
imageWithData:ext.imageData];
ext.imageData =
UIImagePNGRepresentation(image);
message.mediaObject = ext;
message.description = content;
SendMessageToWXReq* req = [[SendMessageToWXReq
alloc] init];
req.bText = NO;
req.message = message;
req.scene = WXSceneTimeline;
[WXApi sendReq:req];
if (self.indicatorView)
{
[self.indicatorView startAnimating];
[self.indicatorView setHidesWhenStopped:YES];
}
else
{
dispatch_async(dispatch_get_main_queue(), ^{
if (!self.indicatorView)
{
self.indicatorView = [[UIActivityIndicatorView
alloc]
initWithActivityIndicatorStyle:UIActivityIndicatorViewStyleWhiteLarge];
self.indicatorView.center =
GetAppController().rootView.center;
[GetAppController().rootView
addSubview:self.indicatorView];
self.indicatorView.color = [UIColor
whiteColor];
[self.indicatorView startAnimating];
[self.indicatorView
setHidesWhenStopped:YES];
}
});
}
self.waitingResponse = YES;
}
else
{
UnitySendMessage([self targetMgr], [self
targetMethod] ,[[NSString stringWithFormat:@"%i"
,(int)ERROR_CODE_NO_INSTALL] UTF8String]);
self.waitingResponse = NO;
}
}
#pragma mark - WXApiDelegate
-(void) onResp:(BaseResp*)resp
{
[self.indicatorView
stopAnimating];
int errorCode ;
if (resp.errCode ==
ERROR_CODE_SHARE_SUCCESS)
{
errorCode = ERROR_CODE_SHARE_SUCCESS;
}
else if(resp.errCode ==
ERROR_CODE_SHARE_CANCEL)
{
errorCode=ERROR_CODE_SHARE_CANCEL;
}
else
{
errorCode=ERROR_CODE_SHARE_FAIL;
}
if([resp
isKindOfClass:[SendMessageToWXResp class]])
{
UnitySendMessage([self target], [self
targetMethod], [[NSString stringWithFormat:@"%i" ,errorCode]
UTF8String]);
self.waitingResponse = NO;
}
}
#pragma mark - AppDelegateListener
-(void) willEnterForeground:(NSNotification
*)notification
{
}
-(void) didFinishLaunching:(NSNotification
*)notification
{
}
-(void) didEnterBackground:(NSNotification
*)notification
{
}
-(void) didReceiveRemoteNotification:(NSNotification
*)notification
{
}
-(void) didReceiveLocalNotification:(NSNotification
*)notification
{
}
-(void)
didRegisterForRemoteNotificationsWithDeviceToken:(NSNotification
*)notification
{
}
-(void) onOpenURL:(NSNotification *)notification
{
}
@end
#if defined(_cplusplus)
extern "C"{
#endif
void
ShareWithWeChat(const char* content)
{
NSString *s = [[NSString alloc]
initWithUTF8String:content];
[[ShareMgr Instance] ShareWithWeChat:s];
}
void
RegisterWeChat(const char* wxAppID)
{
[ShareMgr Instance];
NSString *appId = [[NSString alloc]
initWithUTF8String:wxAppID];
// 微信注册
[WXApi registerApp:appId];
}
#if defined(_cplusplus)
}
#endif