主要学习unity与ios进行交互,unity写功能,ios写界面。软件环境 Mac 10.10 ,unity 4.6.2, xcode 6.4,测试没有问题!
上面的步骤做完了,接下来就要写脚本了,这里我们就写一个让物体旋转的脚本就好了,测试而已,当你学会以后,复杂的同样也可以做到,不废话了,上代码,新建一个Move脚本(这里我用js脚本,用c#脚本也是一样用法,只是js和c#de函数命名方式不同):
当然不选择它,它默认是Device SDK,它是需要在真机下才可以测试的。到此步骤unity里的所有步骤都操作完了,接下来我们就需要在xcode里面对项目进行重新小改造下。
- (void)viewDidLoad {
[super viewDidLoad];
//创建label视图
UILabel *label = [[UILabel alloc] initWithFrame:CGRectMake(0, 0, 320, 40)];
//设置显示内容
label.text = @"程序员佳佳的字符串世界";
//设置背景颜色
label.backgroundColor = [UIColor grayColor];
//设置文字颜色
label.textColor = [UIColor whiteColor];
//设置显示位置居中
label.textAlignment = UITextAlignmentCenter;
//设置字体大小
label.font = [UIFont fontWithName:[[UIFont familyNames] objectAtIndex:10] size:20];
//创建按钮
UIButton *button0 = [UIButton buttonWithType:1];
//设置按钮范围
button0.frame = CGRectMake(0, 40, 100, 30);
//设置按钮显示内容
[button0 setTitle:@"矩形左旋转" forState:UIControlStateNormal];
button0.tintColor = [UIColor whiteColor];
//设置按钮改变后 绑定响应方法
[button0 addTarget:self action:@selector(LeftButtonPressed)forControlEvents:UIControlEventTouchUpInside];
//创建按钮
UIButton *button1 = [UIButton buttonWithType:1];
//设置按钮范围
button1.frame = CGRectMake(0, 100, 100, 30);
//设置按钮显示内容
[button1 setTitle:@"矩形右旋转" forState:UIControlStateNormal];
button1.tintColor = [UIColor whiteColor];
//设置按钮改变后 绑定响应方法
[button1 addTarget:self action:@selector(RightButtonPressed)forControlEvents:UIControlEventTouchUpInside];
//创建按钮
UIButton *button2 = [UIButton buttonWithType:1];
//设置按钮范围
button2.frame = CGRectMake(0, 160, 100, 30);
//设置按钮显示内容
[button2 setTitle:@"矩形上旋转" forState:UIControlStateNormal];
button2.tintColor = [UIColor whiteColor];
//设置按钮改变后 绑定响应方法
[button2 addTarget:self action:@selector(UpButtonPressed)forControlEvents:UIControlEventTouchUpInside];
//创建按钮
UIButton *button3 = [UIButton buttonWithType:1];
//设置按钮范围
button3.frame = CGRectMake(0, 220, 100, 30);
//设置按钮显示内容
[button3 setTitle:@"矩形下旋转" forState:UIControlStateNormal];
button3.tintColor = [UIColor whiteColor];
//设置按钮改变后 绑定响应方法
[button3 addTarget:self action:@selector(DownButtonPressed)forControlEvents:UIControlEventTouchUpInside];
//向view添加
[self.view addSubview:label];
[self.view addSubview:button0];
[self.view addSubview:button1];
[self.view addSubview:button2];
[self.view addSubview:button3];
}
//向左按钮
-(void)LeftButtonPressed{
UnitySendMessage("Cube","MoveLeft","");
}
//向右按钮
-(void)RightButtonPressed{
UnitySendMessage("Cube","MoveRight","");
}
//向上按钮
-(void)UpButtonPressed{
UnitySendMessage("Cube","MoveUp","");
}
//向下按钮
-(void)DownButtonPressed{
UnitySendMessage("Cube","MoveDown","");
}
- (void)didReceiveMemoryWarning {
[super didReceiveMemoryWarning];
// Dispose of any resources that can be recreated.
}
这里要注意的是“UnitySendMessage("Cube","MoveDown","");”这段代码,这段代码是最关键的,因为需要它来传递信息,unity才知道发生什么事了,这个方法是固定的,里面的第一个参数是脚本所在的物体,第二个参数是指调用的函数,第三个参数是传递信息到unity,这里我们不需要传递,所以我们可以为空。
接下来我们就需要unity去启动我们自己写的这个视图了,在 extern "C" int CreateContext_UnityCallback(UIWindow** window,int* screenWidth, int* screenHeight, int* openglesVersion)方法里添加如下代码:
LJViewController *ljView = [[LJViewController alloc]init];
[UnityGetGLViewController().view addSubview:ljView.view];
如图:![Unity与ios交互 Unity与ios交互](https://i-blog.csdnimg.cn/blog_migrate/c6062d1e0d39df036382044adf97a36c.jpeg)
到这里我们的所有操作都已经完成了!哈哈。接下来就可以运行测试看看了。运行程序如下图: