Unity-IOS交互整理

最近年前谈工作,顺便把IOS交互的内容整理一下随笔。

我这里平时用的跟IOS交互的方式,主要分为三部分

1、Asset/Plugins/IOS下面有一个继承自UnityAppController的AppController的类,文件名是AppController.mm

2、在Xcode里面跟Unity交互的类UnityViewController.h和UnityViewController.m

3、在Unity里面跟IOS交互的类

需要注意的是:

Unity调用IOS的方法,是不能直接调用objective-c的方法的,而是调用C语言的方法。

代码如下:

1、

AppController.mm

这个文件丢到Unity的Asset/Plugins/IOS下

#import "UnityAppController.h" //导入Unity的UnityAppController.h
#import "UnityViewController.h"//导入我们自己写接口的类的.h文件

@interface AppController : UnityAppController
@property(nonatomic,strong) UINavigationController *naVC;
-(void) createUI;
@end



@implementation AppController

-(void) createUI
{
    _rootController = [[UIViewController alloc]init];
    _rootView = [[UIView alloc]initWithFrame:[UIScreen mainScreen].bounds];
    _rootController.view = _rootView;
    
    UnityViewController *vc = [[UnityViewController alloc]init];
    self.naVC =[[UINavigationController alloc]initWithRootViewController:vc];
    
    [_rootView addSubview:self.naVC.view];
    
    _window.rootViewController = _rootController;
    [_window bringSubviewToFront:_rootView];
    [_window makeKeyAndVisible];
}

@end


IMPL_APP_CONTROLLER_SUBCLASS(AppController); //这是一个固定写法,因为这个代码,所以启动界面是从这里启动

2、

UnityViewController.h

这个文件是放在Xcode工程目录下的

#import "UnityAppController.h"

@interface UnityViewController : UIViewController
@property (nonatomic,strong) NSString *str;
@end

UnityViewController.m 

这个文件是放在Xcode工程目录下的

#import "UnityViewController.h"
#import "UnityAppController+ViewHandling.h"
#import <UI/UnityView.h>
@interface UnityViewController ()

@end

@implementation UnityViewController

static id object;
- (void)viewDidLoad {
    
    [super viewDidLoad];
    // self.view.backgroundColor = [UIColor blueColor];
    object = self.navigationController;
    self.str = @"我是威少,我成功调用了ios的接口";
    [self.view addSubview:GetAppController().unityView];
    GetAppController().unityView.frame = self.view.frame;
    
    // Do any additional setup after loading the view.
}

//视图已经完全过渡到屏幕上时调用的方法
-(void)viewDidAppear:(BOOL)animated{
    
    UnitySendMessage("ReceiveiOSMessage", "ReceiveiOSInputMessage", [self.str UTF8String]);
    //1.参数1接受消息所挂的脚本的物体名称
    //2.参数2接受消息的方法名
    //3.参数3发送的字符串
    
}

//返回字符串类型,注意字符串类型在返回的时候一定要加上strdup,
//Unity可以调用这个方法
const char * _BackStringParm(const char *parm) { const char *parm1 = strcat(strdup(parm),"dddd"); return strdup(parm1); } //C语言返回int类型,
//Unity可以调用这个方法
int _BackIntParm(int parm) { return parm; } //c语言返回void,
//Unity可以调用这个方法
void _BackBtnPress() { UnitySendMessage("ReceiveiOSMessage", "ReceiveiOSInputMessage", "点击我返回"); //[object popViewControllerAnimated:YES]; } -(void)viewWillAppear:(BOOL)animated{ self.navigationController.navigationBar.hidden = YES; } -(void)viewDidDisappear:(BOOL)animated { self.navigationController.navigationBar.hidden = NO; } - (void)didReceiveMemoryWarning { [super didReceiveMemoryWarning]; // Dispose of any resources that can be recreated. } /* #pragma mark - Navigation // In a storyboard-based application, you will often want to do a little preparation before navigation - (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender { // Get the new view controller using [segue destinationViewController]. // Pass the selected object to the new view controller. } */ @end

3、

unityscript.cs

[DllImport ("__Internal")]

public static extern void _BackBtnPress();

[DllImport ("__Internal")]

public static extern int _BackIntParm(int x);

[DllImport ("__Internal")]

public static extern string _BackStringParm(string str);

以上三个方法就是从ios那边映射调用,有没有发现这个和C#程序调用C语言的dll库的方式类似?

噢不,不是类似,就是一样。因为我们从C#这边调用的就是C语言写的方法,而不是OC写的方法。

public void ReceiveiOSInputMessage(string str)

{

  Debug.Log(str);

}

以上的这个方法,是IOS那边使用UnitySendMessage方法调用的,

例如 UnitySendMessage("ReceiveiOSMessage", "ReceiveiOSInputMessage", "点击我返回");

参数1是GameObject的名字,参数2是GameObject下的脚本里的方法名,参数3是返回的字符串。

最后,有没有发现这一套下来其实和Unity-Android其实相似?

Unity-IOS:Unity使用的是UnityAppController,实质就是UIViewController之间的交互。

Unity-Android:Unity使用的是UnityPlayerActivity,实质就是Activity之间的交互。

原文地址:https://www.cnblogs.com/vsirWaiter/p/8383408.html