以原生为主的启动、原生与Unity3D视图Push/Pop切换
参考文章:在iOS APP中反复打开和关闭Unity页面 ,不吝赐教hertz@hertzwang.com
使用 Unity Version 5.3.5f1 Personal
和Xcode 7.3.1
1、打开Unity
New 一个新项目;
2、File
–>Build Settings...
打开Build Setting对话框,Platform
选择iOS
,点击Build
按钮生成Xcode工程
;
3、从Finder
打开生成的Xcode项目
;
4、在Project/Classes/
中新建原生视图HomepageViewController
,使用XIB:
4.1、HomepageViewController.h
1 |
|
4.2、HomepageViewController.m
1 | /** |
5、在UnityAppController.h
中声名HomepageViewController
和UINavigationController
:
1 | HomepageViewController *homepageViewController; //原生主视图 |
6、 UnityAppController.mm
修改,调整启动顺序和界面切效果:
6.1、添加一个成员变量
homepageEnable
,用于防止多次创建原生视图BOOL homepageEnable = YES; //只启动一次
6.2、修改方法
applicationDidBecomeActive:
,注释Unity的启动、添加调用新启动:1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34- (void)applicationDidBecomeActive:(UIApplication*)application
{
::printf("-> applicationDidBecomeActive()\n");
//add new code begin
if (homepageEnable) {
homepageEnable = NO;
[self performSelector:@selector(startHomepage:) withObject:application afterDelay:0];
}
//add new code end
if(_snapshotView)
{
[_snapshotView removeFromSuperview];
_snapshotView = nil;
}
if(_unityAppReady)
{
if(UnityIsPaused() && _wasPausedExternal == false)
{
UnityPause(0);
UnityWillResume();
}
UnitySetPlayerFocus(1);
}
else if(!_startUnityScheduled)
{
_startUnityScheduled = true;
//[self performSelector:@selector(startUnity:) withObject:application afterDelay:0]; //注释Unity启动
}
_didResignActive = false;
}6.3、实现
startHomepage:
方法:1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24/**
* 启动原生视图
*
* @param application 应用
*/
- (void)startHomepage:(UIApplication*)application {
//初始化原生视图
homepageViewController = [[HomepageViewController alloc] initWithNibName:@"HomepageViewController" bundle:[NSBundle mainBundle]];
__weak UnityAppController *weakSelf = self;
homepageViewController.homepageViewControllerPushUnityBlock = ^(BOOL isFirst) {
if (isFirst) {
[weakSelf startUnity:application]; //首次点击调用启动Unity
} else {
[weakSelf restartUnity];
}
};
//初始化导航控制器
homepageNavigationController = [[UINavigationController alloc] initWithRootViewController:homepageViewController];
[homepageNavigationController setNavigationBarHidden:YES]; //隐藏导航栏
//设置为主视图
[_window setRootViewController:homepageNavigationController];
[UnityGetMainWindow() makeKeyAndVisible];
}6.4、实现
restartUnity
方法:1
2
3
4
5
6
7
8
9
10
11
12
13/**
* 重启Unity
*/
- (void)restartUnity {
[homepageNavigationController pushViewController:_rootController animated:YES]; //Push出Unity
if (_didResignActive) {
UnityPause(false); //恢复Unity
}
_didResignActive = NO;
}
7、修改UnityAppController+ViewHandling.mm
的showGameUI
方法实现Push效果,并添加Pop按钮:
1 | - (void)showGameUI |
8、修改UnityAppController+ViewHandling.mm
的transitionToViewController:
方法解决横屏下Push/Pop问题
1 | - (void)transitionToViewController:(UIViewController*)vc |
总结
- 实现原生为主启动通过改变启动顺序实现
- 实现Push/Pop效果添加导航栏,并在相应的地方Push/Pop即可
目前存在的问题(已解决,参照8)
- 横屏状态Push,Unity界面黑屏
- 竖屏Push到Unity界面,Unity界面切换到横屏时点击Pop界面卡死
补充
- 去掉Unity启动界面需要使用非个版的Unity,参考官方说明-订阅详情-启动画面