我正在开发一个能够使用UnityAppController在其中运行Unity 3d导航并在不使用导航时将其暂停的应用。主应用程序还具有另一个引擎,即cocos2d,其中我具有其他功能。
现在的问题是,仅当不使用cocos2d(使用OpenGL-es)功能时3d导航才起作用,一旦我使用了cocos2d功能并再次回到3d导航中,它似乎就被冻结了。
这是我的应用程序委托(继承自Unity AppController类),在这里我正在处理Unity暂停和播放
- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions{
[super application:application didFinishLaunchingWithOptions:launchOptions];
self.unityVC = [self getRootController];
UIButton *button =[UIButton buttonWithType:UIButtonTypeCustom];
[button setBackgroundColor:[UIColor colorWithRed:108.0/255 green:106.0/255 blue:55.0/255 alpha:1]];
[button setTitle:@"DONE" forState:UIControlStateNormal];
// [button setTitleColor:[UIColor blackColor] forState:UIControlStateNormal];
[button setFrame:frame];
[button addTarget:self action:@selector(Fn_RemoveUnity) forControlEvents:UIControlEventTouchUpInside];
[_rootView addSubview:button];
[_rootView bringSubviewToFront:button];
//Default ViewController Code
//--------------------------------------------------------
// self.window = [UIApplication sharedApplication].keyWindow;
self.window = [[UIWindow alloc] initWithFrame:[[UIScreen mainScreen] bounds]];
// Override point for customization after application launch.
[self Fn_AddMainScreen];
[self.window makeKeyAndVisible];
//--------------------------------------------------------
return YES;
}
- (void)playUnity
{
[self unityPause:NO];
}
- (void)pauseUnity
{
[self unityPause:YES];
}
-(void) Fn_AddUnity{
[window setHidden:YES];
[self playUnity];
}
-(void) Fn_RemoveUnity{
[self pauseUnity];
[window setHidden:NO];
[self Fn_AddMainScreen];
}
这是cocos2d代码
EAGLView *glView = [EAGLView viewWithFrame:CGRectMake(md.glview_X,md.glview_Y,md.glview_Width,md.glview_Height)
pixelFormat:kEAGLColorFormatRGB565 // kEAGLColorFormatRGBA8
depthFormat:0 // GL_DEPTH_COMPONENT16_OES
preserveBackbuffer:YES sharegroup:nil multiSampling:NO numberOfSamples:nil
];
glView.autoresizingMask = UIViewAutoresizingFlexibleWidth | UIViewAutoresizingFlexibleHeight;
[[CCDirector sharedDirector] setProjection:CCDirectorProjection2D];
[glView setContentScaleFactor:1.0];
[[CCDirector sharedDirector] enableRetinaDisplay:YES];
[[CCDirector sharedDirector] setOpenGLView:glView];
[self.view insertSubview:glView atIndex:0];
[[CCDirector sharedDirector] runWithScene: [smoothningEffect scene]];
最佳答案
任何时候都只能有一个OpenGL上下文处于活动状态,因此在它们之间切换时必须完全关闭两个引擎。即使到那时,两个引擎都没有被设计为与其他渲染引擎配合使用,因此也很可能被证明是不可能的。
例如,缓存机制可能是一个问题,特别是因为cocos2d在各处都使用Singleton来将事物缓存在内存中。在切换回Unity之前,我将尝试确保从内存中释放所有必需的cocos2d Singleton类。
这里的问题是,为什么您首先还要混合两个单独的渲染引擎?首先,Unity本身具有2D工具集。因此,通过研究如何使用Unity自己的2D工具集在2D中做任何事情,或者像过去许多其他游戏一样成功地在Unity 3D渲染器中模拟2D,可以更好地投资您的时间。