一直一来都是用c++来写游戏,最近学lua主要是因为lua更新比较方便,cocos提供了AssetsManager类可以进行一些资源包的更新,但是不够灵活,AssetsManagerEx 是AssetsManager的增强版本,提供了比AssetsManager更加灵活的更新方式使用AssetsManagerEx的方法:添加必要的头文件
#include "extensions/cocos-ext.h" USING_NS_CC_EXT;
AssetsManagerEx* HelloWorld::getAssetsManagerEx()
{
static AssetsManagerEx *amEx = nullptr;
if (!amEx)
{
amEx = AssetsManagerEx::create("project.manifest",m_pathToSaveEx);
amEx->retain();
if (amEx->getLocalManifest()->isLoaded())
{
log("Fail to update assets, step skipped.");
}
auto listener = EventListenerAssetsManagerEx::create(amEx,CC_CALLBACK_1(HelloWorld::onAssetsEvent,this));
Director::getInstance()->getEventDispatcher()->addEventListenerWithFixedPriority(listener, 1);
}
return amEx;
}
清单文件project.manifest
下面一个清单文件的一些具体内容{
"packageUrl" : "http://tools.itharbors.com/assets_manager/AMTestScene1/",
"remoteManifestUrl" : "http://tools.itharbors.com/assets_manager/AMTestScene1/project_dev.manifest",
"remoteVersionUrl" : "http://tools.itharbors.com/assets_manager/AMTestScene1/version_dev.manifest",
"version" : "1.2.0",
"engineVersion" : "3.x dev", "assets" : {
"Images/assetMgrBackground1.jpg" : {
"md5" : "....."
},
"Images/ball.png" : {
"md5" : "..."
},
"Images/blocks.png" : {
"md5" : "..."
},
"compressed.zip" : {
"md5" : "...",
"compressed" : true
}
},
"searchPaths" : [
]
}
packageUrl是资源下载的地址assets 里面包含的则是文件的清单
另一个是资源的更新目录,下载好的资源就在该目录下
单独创建一个函数来获取更新目录
void HelloWorld::InitDownLoadDirEx()
{
m_pathToSaveEx = FileUtils::getInstance()->getWritablePath();
#if (CC_TARGET_PLATFORM != CC_PLATFORM_WIN32)
DIR *pDir = NULL;
pDir = opendir(m_pathToSaveEx.c_str());
if (!pDir)
{
mkdir(m_pathToSaveEx.c_str(), S_IRWXU | S_IRWXG | S_IRWXO);
}
#else
if ((GetFileAttributesA(m_pathToSaveEx.c_str())) == INVALID_FILE_ATTRIBUTES)
{
CreateDirectoryA(m_pathToSaveEx.c_str(), 0);
}
#endif
CCLOG("InitDownLoadDirEx end");
}
不同平台的目录创建方式有点不同
void HelloWorld::onAssetsEvent(EventAssetsManagerEx* pEvent)
{
switch (pEvent->getEventCode())
{
case EventAssetsManagerEx::EventCode::ERROR_NO_LOCAL_MANIFEST:
{
log("没有本地清单文件");
}
break;
case EventAssetsManagerEx::EventCode::ERROR_DOWNLOAD_MANIFEST:
{
log("下载远程清单文件失败");
}
break;
case EventAssetsManagerEx::EventCode::ERROR_PARSE_MANIFEST:
{
log("解析清单文件失败");
}
break;
case EventAssetsManagerEx::EventCode::NEW_VERSION_FOUND:
{
log("找到新版本");
}
break;
case EventAssetsManagerEx::EventCode::ALREADY_UP_TO_DATE:
{
log("更新完成");
}
break;
case EventAssetsManagerEx::EventCode::UPDATE_PROGRESSION:
{
log("更新中");
char szBuf[256] = "";
sprintf(szBuf,"%s,%0.2f,%0.2f",pEvent->getAssetId().c_str(),pEvent->getPercentByFile(),pEvent->getPercent());
m_pProcessLabel->setString(szBuf);
}
break;
case EventAssetsManagerEx::EventCode::ASSET_UPDATED:
{
log("检测更新");
}
break;
case EventAssetsManagerEx::EventCode::ERROR_UPDATING:
{
log("更新出错");
}
break;
case EventAssetsManagerEx::EventCode::UPDATE_FINISHED:
{
log("更新完成");
}
break;
case EventAssetsManagerEx::EventCode::UPDATE_FAILED:
{
log("更新失败");
}
break;
case EventAssetsManagerEx::EventCode::ERROR_DECOMPRESS:
{
log("解压出错");
}
break;
default:
break;
}
}
注意:pEvent->getPercent()是获取的当前文件的下载进度
InitDownLoadDirEx();
getAssetsManagerEx()->update();