相信很多朋友都有这样的需求,在一个现有的iOS app里嵌入cocos,cocos要能完全释放,重启。
以下是我研究源码后,实现的方案。已经上线验证使用
版本:Cocos Creator 2.4.2
其它版本大同小异,各位看官相应三步走就行
1.停止渲染
在退出viewcontroller之前,一定要保证把cocos runloop先停掉
// stop runloop
cocos2d::Application::getInstance()->onPause();
cocos2d::Application::getInstance()->stop();
2.释放cocos
- (void)cleanup {
// audio
cocos2d::AudioEngine::end();
// network
cocos2d::network::HttpClient::destroyInstance();
// application
if (s_sharedApplication != nullptr) {
delete s_sharedApplication;
s_sharedApplication = nullptr;
}
}
3.修改Application析构
CCApplication-ios.mm
Application::~Application()
{
_scheduler->removeAllFunctionsToBePerformedInCocosThread();
_scheduler->unscheduleAll();
#if USE_AUDIO
AudioEngine::end();
#endif
// spine
#if USE_SPINE
spine::SkeletonDataMgr::destroyInstance();
#endif
// dragonbones
#if USE_DRAGONBONES
dragonBones::ArmatureCacheMgr::destroyInstance();
#endif
// middleware
#if USE_MIDDLEWARE
middleware::MiddlewareManager::destroyInstance();
#endif
// font
TTFLabelAtlasCache::destroyInstance();
// stencil
renderer::StencilManager::destoryInstance();
EventDispatcher::destroy();
se::ScriptEngine::destroyInstance();
// stop main loop
[(MainLoop*)_delegate stopMainLoop];
[(MainLoop*)_delegate release];
_delegate = nullptr;
[(CCEAGLView*)_view release];
_view = nullptr;
delete _renderTexture;
_renderTexture = nullptr;
// director
renderer::DeviceGraphics::destoryInstance();
Configuration::destroyInstance();
FileUtils::destroyInstance();
// clean auto release pool
PoolManager::destroyInstance();
Application::_scheduler = nullptr;
Application::_instance = nullptr;
}