说明
iOS中实现unity的方法,是指unity中申明了一个方法,在iOS端实现其方法
例如,在unity中申明了一个方法
[DllImport("__Internal")]
private static extern void _PressButton1();
那么我们如何在iOS端实现_PressButton1()呢?
实现
在我们已经将unity集成到我们项目之后,我们需要创建两个UIViewController,分别是LinkViewController、zzMyViewViewController
将zzMyViewViewController.m修改为zzMyViewViewController.mm,并实现_PressButton1()方法,
#import "zzMyViewViewController.h"
#import "LinkViewController.h"
@interface zzMyViewViewController ()
@end
@implementation zzMyViewViewController
- (void)viewDidLoad {
[super viewDidLoad];
// Do any additional setup after loading the view.
}
- (void)didReceiveMemoryWarning {
[super didReceiveMemoryWarning];
// Dispose of any resources that can be recreated.
}
-(void)test
{
LinkViewController *LinkVC=[LinkViewController shareInstance];//LinkViewController必须是单例
[LinkVC log];//调用LinkViewController的log方法
}
@end
extern "C"
{
void _PressButton1 ()
{
zzMyViewViewController *instance=[[zzMyViewViewController alloc]init];
UIViewController *vc=UnityGetGLViewController();
[vc.view addSubview:instance.view];
[instance test];//调用test方法
}
}
- 在LinkViewController中申明log方法,并创建单例
#import "LinkViewController.h"
@interface LinkViewController ()
@end
@implementation LinkViewController
#pragma mark--创建单例--
+(instancetype) shareInstance
{
static LinkViewController *_instance = nil;
static dispatch_once_t onceToken ;
dispatch_once(&onceToken, ^{
_instance = [[super allocWithZone:NULL] init] ;
}) ;
return _instance ;
}
-(void)log
{
NSLog(@"unity调用了iOS的方法");
}
@end
- ok,当unity中调用了_PressButton1()方法的时候,控制台就会打印unity调用了iOS的方法。