集成方式:
两种引入自行搜索,教程很多
1、工程引用:
优点:iOS原生工程和unity打包出来的iOS成功由workspace管理
缺点:unity频繁更新包,每次都要重新集成
2、framework方式引入
打包UnityFramework.framework,引入iOS原生工程
交互-unity调用iOS原生方法
unity开发提供的包为:
// NativeCallProxy.mm
extern "C" {
void __UnityToNativeCallFun(const char* funName,const char* args) {}
}
1、Protocol写法
- 创建NativeCallProxy.h
- 创建协议方法
// NativeCallsProtocol defines protocol with methods you want to be called from managed
@protocol NativeCallsProtocol
@required
- (void) __UnityToNativeCallFun:(NSString*)funName args:(NSString *)args;
// other methods
@end
__attribute__ ((visibility("default")))
@interface FrameworkLibAPI : NSObject
// call it any time after UnityFrameworkLoad to set object implementing NativeCallsProtocol methods
+(void) registerAPIforNativeCalls:(id<NativeCallsProtocol>) aApi;
@end
- 修改NativeCallProxy.mm
#import <Foundation/Foundation.h>
#import "NativeCallProxy.h"
@implementation FrameworkLibAPI
id<NativeCallsProtocol> api = NULL;
+(void) registerAPIforNativeCalls:(id<NativeCallsProtocol>) aApi
{
api = aApi;
}
@end
extern "C" {
void __UnityToNativeCallFun(const char* funName,const char* args) {
NSLog(@"funName = %@",[NSString stringWithUTF8String:funName]);
NSString *argsStr = nil;
if(args){
argsStr = [NSString stringWithUTF8String:args];
NSLog(@"args = %@",argsStr);
}
return [api __UnityToNativeCallFun:[NSString stringWithUTF8String:funName] args:argsStr];
}
}
-
点击NativeCallProxy.h 设置为public
iOS原生工程中使用:
//实现协议方法
@interface AAAA()<NativeCallsProtocol>
- (void) __UnityToNativeCallFun:(NSString*)funName args:(NSString *)args;
{
//iOS原生工程中业务逻辑
}
- (void)initUnity{
///
[NSClassFromString(@"FrameworkLibAPI") registerAPIforNativeCalls:self];
///
}
2、NSNotification写法
- 直接在NativeCallProxy.mm中将参数通知出去
extern "C" {
void __UnityToNativeCallFun(const char* funName,const char* args) {
[[NSNotificationCenter defaultCenter] postNotificationName:@"bbbbbbbbb" object:[NSString stringWithUTF8String:funName]];
- iOS原生工程中使用:页面直接监听通知即可