如何在ios的xcode工程里面利用一个按钮单独启动unity3d
展开全部
首先,我已经建好了我的u3d工程,包括模型,模型的移动、旋转等函数。。
然后,我导出一个xcode工程(版本:unity4.2.1、xcode5)。。。
如图:
这是unity导出来的xcode工程的目录结构。
接下来:
第一步:选择UnityAppController.mm这个文件打开。
并在int OpenEAGL_UnityCallback(UIWindow** window, int* screenWidth, int* screenHeight, int* openglesVersion)
这个方法里面的
return true;之前加入
//创建一个全屏幕的window
CGRect rect=CGRectMake(0, 0, *screenWidth, *screenHeight);
*window=[[UIWindow alloc] initWithFrame:rect];
[*window makeKeyAndVisible];
GLView * view=[[GLView alloc] initWithFrame:rect];
[*window addSubview:view];
[view release];
MyViewController *myView=[[MyViewController alloc] init];
[*window addSubview:myView.view];
[myView release];
其中:MyViewController是导出工程后,手动新建的类,目的是,在这个ViewController种控制u3d模型的旋转、移动等动作。
第二步:创建MyViewController并添加按钮,启动u3d
- (void)viewDidLoad {
[super viewDidLoad];
//创建label视图
UILabel *label = [[UILabel alloc] initWithFrame:CGRectMake(0, 0, 320, 40)];
//设置显示内容
label.text = @"雨松MOMO的程序世界";
//设置背景颜色
label.backgroundColor = [UIColor blueColor];
//设置文字颜色
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 addTarget:self action:@selector(LeftButtonPressed) forControlEvents:UIControlEventTouchUpInside];
//创建按钮
UIButton *button1 = [UIButton buttonWithType:1];
//设置按钮范围
button1.frame = CGRectMake(0, 100, 100, 30);
//设置按钮显示内容
[button1 setTitle:@"矩形右旋转" forState:UIControlStateNormal];
//设置按钮改变后 绑定响应方法
[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 addTarget:self action:@selector(UpButtonPressed) forControlEvents:UIControlEventTouchUpInside];
//创建按钮
UIButton *button3 = [UIButton buttonWithType:1];
//设置按钮范围
button3.frame = CGRectMake(0, 220, 100, 30);
//设置按钮显示内容
[button3 setTitle:@"矩形下旋转" forState:UIControlStateNormal];
//设置按钮改变后 绑定响应方法
[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 {
// Releases the view if it doesn't have a superview.
[super didReceiveMemoryWarning];
// Release any cached data, images, etc. that aren't in use.
}
- (void)viewDidUnload {
[super viewDidUnload];
}
- (void)dealloc {
[super dealloc];
}
第三步:修改Bundle Identifier,进行真机测试
到此,代码部分全部搞定。接下来是真机,等待运行...
艰难的运行中,大约两分钟...
u3d的开机画面出来了,刷一下,进入到了你添加的界面,里面有几个按钮,分别是控制模型旋转的。
此时,看上去一切都很顺路,轻轻的,点一下,其中一个按钮。。。。问题出了。。崩溃。。。
悲剧的崩溃。。。
控制台输出了一大堆错误。
错误如下:
2014-01-09 11:50:55.863 ProductName[3340:907] -> registered mono modules 0xfd4610
-> applicationDidFinishLaunching()
-> applicationDidBecomeActive()
Mono path[0] = '/var/mobile/Applications/AA64800A-19C5-4CC9-8CF9-B00ACD24A100/ProductName.app/Data/Managed'
Mono config path = '/var/mobile/Applications/AA64800A-19C5-4CC9-8CF9-B00ACD24A100/ProductName.app/Data/Managed'
2014-01-09 11:50:57.289 ProductName[3340:907] 2
Renderer: PowerVR SGX 535
Vendor: Imagination Technologies
Version: OpenGL ES 2.0 IMGSGX535-73.16.1
GL_OES_depth_texture GL_OES_depth24 GL_OES_element_index_uint GL_OES_fbo_render_mipmap GL_OES_mapbuffer GL_OES_packed_depth_stencil GL_OES_rgb8_rgba8 GL_OES_standard_derivatives GL_OES_texture_float GL_OES_texture_half_float GL_OES_vertex_array_object GL_EXT_blend_minmax GL_EXT_debug_label GL_EXT_debug_marker GL_EXT_discard_framebuffer GL_EXT_map_buffer_range GL_EXT_read_format_bgra GL_EXT_separate_shader_objects GL_EXT_shader_framebuffer_fetch GL_EXT_shader_texture_lod GL_EXT_texture_filter_anisotropic GL_EXT_texture_storage GL_APPLE_copy_texture_levels GL_APPLE_framebuffer_multisample GL_APPLE_rgb_422 GL_APPLE_sync GL_APPLE_texture_format_BGRA8888 GL_APPLE_texture_max_level GL_IMG_read_format GL_IMG_texture_compression_pvrtc
Creating OpenGLES2.0 graphics device
Initialize engine version: 4.2.1f4 (4d30acc925c2)
Begin MonoManager ReloadAssembly
Platform assembly: /var/mobile/Applications/AA64800A-19C5-4CC9-8CF9-B00ACD24A100/ProductName.app/Data/Managed/UnityEngine.dll (this message is harmless)
Loading /var/mobile/Applications/AA64800A-19C5-4CC9-8CF9-B00ACD24A100/ProductName.app/Data/Managed/UnityEngine.dll into Unity Child Domain
Non platform assembly: /private/var/mobile/Applications/AA64800A-19C5-4CC9-8CF9-B00ACD24A100/ProductName.app/Data/Managed/Mono.Security.dll (this message is harmless)
Non platform assembly: /private/var/mobile/Applications/AA64800A-19C5-4CC9-8CF9-B00ACD24A100/ProductName.app/Data/Managed/System.dll (this message is harmless)
Platform assembly: /var/mobile/Applications/AA64800A-19C5-4CC9-8CF9-B00ACD24A100/ProductName.app/Data/Managed/Assembly-UnityScript.dll (this message is harmless)
Loading /var/mobile/Applications/AA64800A-19C5-4CC9-8CF9-B00ACD24A100/ProductName.app/Data/Managed/Assembly-UnityScript.dll into Unity Child Domain
- Completed reload, in 0.182 seconds
The referenced script on this Behaviour is missing!
(Filename: Line: 1657)
The referenced script on this Behaviour is missing!
(Filename: Line: 1657)
然后,我导出一个xcode工程(版本:unity4.2.1、xcode5)。。。
如图:
这是unity导出来的xcode工程的目录结构。
接下来:
第一步:选择UnityAppController.mm这个文件打开。
并在int OpenEAGL_UnityCallback(UIWindow** window, int* screenWidth, int* screenHeight, int* openglesVersion)
这个方法里面的
return true;之前加入
//创建一个全屏幕的window
CGRect rect=CGRectMake(0, 0, *screenWidth, *screenHeight);
*window=[[UIWindow alloc] initWithFrame:rect];
[*window makeKeyAndVisible];
GLView * view=[[GLView alloc] initWithFrame:rect];
[*window addSubview:view];
[view release];
MyViewController *myView=[[MyViewController alloc] init];
[*window addSubview:myView.view];
[myView release];
其中:MyViewController是导出工程后,手动新建的类,目的是,在这个ViewController种控制u3d模型的旋转、移动等动作。
第二步:创建MyViewController并添加按钮,启动u3d
- (void)viewDidLoad {
[super viewDidLoad];
//创建label视图
UILabel *label = [[UILabel alloc] initWithFrame:CGRectMake(0, 0, 320, 40)];
//设置显示内容
label.text = @"雨松MOMO的程序世界";
//设置背景颜色
label.backgroundColor = [UIColor blueColor];
//设置文字颜色
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 addTarget:self action:@selector(LeftButtonPressed) forControlEvents:UIControlEventTouchUpInside];
//创建按钮
UIButton *button1 = [UIButton buttonWithType:1];
//设置按钮范围
button1.frame = CGRectMake(0, 100, 100, 30);
//设置按钮显示内容
[button1 setTitle:@"矩形右旋转" forState:UIControlStateNormal];
//设置按钮改变后 绑定响应方法
[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 addTarget:self action:@selector(UpButtonPressed) forControlEvents:UIControlEventTouchUpInside];
//创建按钮
UIButton *button3 = [UIButton buttonWithType:1];
//设置按钮范围
button3.frame = CGRectMake(0, 220, 100, 30);
//设置按钮显示内容
[button3 setTitle:@"矩形下旋转" forState:UIControlStateNormal];
//设置按钮改变后 绑定响应方法
[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 {
// Releases the view if it doesn't have a superview.
[super didReceiveMemoryWarning];
// Release any cached data, images, etc. that aren't in use.
}
- (void)viewDidUnload {
[super viewDidUnload];
}
- (void)dealloc {
[super dealloc];
}
第三步:修改Bundle Identifier,进行真机测试
到此,代码部分全部搞定。接下来是真机,等待运行...
艰难的运行中,大约两分钟...
u3d的开机画面出来了,刷一下,进入到了你添加的界面,里面有几个按钮,分别是控制模型旋转的。
此时,看上去一切都很顺路,轻轻的,点一下,其中一个按钮。。。。问题出了。。崩溃。。。
悲剧的崩溃。。。
控制台输出了一大堆错误。
错误如下:
2014-01-09 11:50:55.863 ProductName[3340:907] -> registered mono modules 0xfd4610
-> applicationDidFinishLaunching()
-> applicationDidBecomeActive()
Mono path[0] = '/var/mobile/Applications/AA64800A-19C5-4CC9-8CF9-B00ACD24A100/ProductName.app/Data/Managed'
Mono config path = '/var/mobile/Applications/AA64800A-19C5-4CC9-8CF9-B00ACD24A100/ProductName.app/Data/Managed'
2014-01-09 11:50:57.289 ProductName[3340:907] 2
Renderer: PowerVR SGX 535
Vendor: Imagination Technologies
Version: OpenGL ES 2.0 IMGSGX535-73.16.1
GL_OES_depth_texture GL_OES_depth24 GL_OES_element_index_uint GL_OES_fbo_render_mipmap GL_OES_mapbuffer GL_OES_packed_depth_stencil GL_OES_rgb8_rgba8 GL_OES_standard_derivatives GL_OES_texture_float GL_OES_texture_half_float GL_OES_vertex_array_object GL_EXT_blend_minmax GL_EXT_debug_label GL_EXT_debug_marker GL_EXT_discard_framebuffer GL_EXT_map_buffer_range GL_EXT_read_format_bgra GL_EXT_separate_shader_objects GL_EXT_shader_framebuffer_fetch GL_EXT_shader_texture_lod GL_EXT_texture_filter_anisotropic GL_EXT_texture_storage GL_APPLE_copy_texture_levels GL_APPLE_framebuffer_multisample GL_APPLE_rgb_422 GL_APPLE_sync GL_APPLE_texture_format_BGRA8888 GL_APPLE_texture_max_level GL_IMG_read_format GL_IMG_texture_compression_pvrtc
Creating OpenGLES2.0 graphics device
Initialize engine version: 4.2.1f4 (4d30acc925c2)
Begin MonoManager ReloadAssembly
Platform assembly: /var/mobile/Applications/AA64800A-19C5-4CC9-8CF9-B00ACD24A100/ProductName.app/Data/Managed/UnityEngine.dll (this message is harmless)
Loading /var/mobile/Applications/AA64800A-19C5-4CC9-8CF9-B00ACD24A100/ProductName.app/Data/Managed/UnityEngine.dll into Unity Child Domain
Non platform assembly: /private/var/mobile/Applications/AA64800A-19C5-4CC9-8CF9-B00ACD24A100/ProductName.app/Data/Managed/Mono.Security.dll (this message is harmless)
Non platform assembly: /private/var/mobile/Applications/AA64800A-19C5-4CC9-8CF9-B00ACD24A100/ProductName.app/Data/Managed/System.dll (this message is harmless)
Platform assembly: /var/mobile/Applications/AA64800A-19C5-4CC9-8CF9-B00ACD24A100/ProductName.app/Data/Managed/Assembly-UnityScript.dll (this message is harmless)
Loading /var/mobile/Applications/AA64800A-19C5-4CC9-8CF9-B00ACD24A100/ProductName.app/Data/Managed/Assembly-UnityScript.dll into Unity Child Domain
- Completed reload, in 0.182 seconds
The referenced script on this Behaviour is missing!
(Filename: Line: 1657)
The referenced script on this Behaviour is missing!
(Filename: Line: 1657)
2016-05-21 · 做真实的自己 用良心做教育
千锋教育
千锋教育专注HTML5大前端、JavaEE、Python、人工智能、UI&UE、云计算、全栈软件测试、大数据、物联网+嵌入式、Unity游戏开发、网络安全、互联网营销、Go语言等培训教育。
向TA提问
关注
展开全部
在按钮的点击事件里面添加启动unity3d的点击事件
已赞过
已踩过<
评论
收起
你对这个回答的评价是?
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询