Cocos2d-x 3.2 自动更新 -- 使用AssetsManager更新游戏资源包

前端之家收集整理的这篇文章主要介绍了Cocos2d-x 3.2 自动更新 -- 使用AssetsManager更新游戏资源包前端之家小编觉得挺不错的,现在分享给大家,也给大家做个参考。

Android和win32已经测试通过,理论上IOS也可以。
1、AssetsManagerDelegateProtocol
AssetsManagerDelegateProtocol,用于与服务器校验版本号,更新下载资源包,并对成功、出错、下载进度等进行回调。
2、资源包名称
所下载资源包名称默认为:cocos2dx-update-temp-package.zip。
如果想要修改文件名,直接修改引擎下extensions\assets-manager\AsetsManager.ccp中的TEMP_PACKAGE_FILE_NAME

3、服务器
下以是我的资源包路径和本版号。
http://shezzer.sinaapp.com/downloadTest/cocos2dx-update-temp-package.zip
http://shezzer.sinaapp.com/downloadTest/version.PHP
4、实现
//Upgrade.h
//<imgsrc="http://img.blog.csdn.net/20140728093037654?watermark/2/text/aHR0cDovL2Jsb2cuY3Nkbi5uZXQvTGlhbmdzaGFvemU=/font/5a6L5L2T/fontsize/400/fill/I0JBQkFCMA==/dissolve/70/gravity/SouthEast"alt=""/>
//CreatedbySharezeron14-07-26.
//
#ifndef_UPGRADE_H_
#define_UPGRADE_H_
#include"cocos2d.h"
#include"extensions/cocos-ext.h"
classUpgrade:publiccocos2d::CCLayer,publiccocos2d::extension::AssetsManagerDelegateProtocol
{
public:
Upgrade();
virtual~Upgrade();
virtualboolinit();
voidupgrade(cocos2d::Ref*pSender); //检查版本更新
voidreset(cocos2d::Ref*pSender); //重置版本
virtualvoidonError(cocos2d::extension::AssetsManager::ErrorCodeerrorCode); //错误信息
virtualvoidonProgress(intpercent); //更新下载进度
virtualvoidonSuccess(); //下载成功
CREATE_FUNC(Upgrade);
private:
cocos2d::extension::AssetsManager*getAssetManager();
voidinitDownloadDir(); //创建下载目录
std::string_pathToSave;
cocos2d::Label*_showDownloadInfo;
};
#endif
//Upgrade.cpp
#include"Upgrade.h"
#include"HelloWorldScene.h"
#if(CC_TARGET_PLATFORM!=CC_PLATFORM_WIN32)
#include<dirent.h>
#include<sys/stat.h>
USING_NS_CC;
USING_NS_CC_EXT;
#defineDOWNLOAD_FIEL "download" //下载后保存的文件夹名
Upgrade::Upgrade():
_pathToSave(""),
_showDownloadInfo(NULL)
}
Upgrade::~Upgrade()
AssetsManager*assetManager=getAssetManager();
CC_SAFE_DELETE(assetManager);
boolUpgrade::init()
if(!CCLayer::init())
{
returnfalse;
}
SizewinSize=Director::getInstance()->getWinSize();
initDownloadDir();
_showDownloadInfo=Label::createWithSystemFont("","Arial",20);
this->addChild(_showDownloadInfo);
_showDownloadInfo->setPosition(Vec2(winSize.width/2,winSize.height/2-20));
autoitemLabel1=MenuItemLabel::create(
Label::createWithSystemFont("Reset","Arail",20),CC_CALLBACK_1(Upgrade::reset,this));
autoitemLabel2=MenuItemLabel::create(
Label::createWithSystemFont("Upgrad",CC_CALLBACK_1(Upgrade::upgrade,Simsun; font-size:14px; line-height:30px"> automenu=Menu::create(itemLabel1,itemLabel2,NULL);
this->addChild(menu);
itemLabel1->setPosition(Vec2(winSize.width/2,winSize.height/2+20));
itemLabel2->setPosition(Vec2(winSize.width/2,winSize.height/2));
menu->setPosition(Vec2::ZERO);
returntrue;
voidUpgrade::onError(AssetsManager::ErrorCodeerrorCode)
if(errorCode==AssetsManager::ErrorCode::NO_NEW_VERSION)
_showDownloadInfo->setString("nonewversion");
elseif(errorCode==AssetsManager::ErrorCode::NETWORK)
_showDownloadInfo->setString("networkerror");
elseif(errorCode==AssetsManager::ErrorCode::CREATE_FILE)
_showDownloadInfo->setString("createfileerror");
voidUpgrade::onProgress(intpercent)
if(percent<0)
return;
charprogress[20];
snprintf(progress,20,"download%d%%",percent);
_showDownloadInfo->setString(progress);
voidUpgrade::onSuccess()
CCLOG("downloadsuccess");
_showDownloadInfo->setString("downloadsuccess");
std::stringpath=FileUtils::getInstance()->getWritablePath()+DOWNLOAD_FIEL;
//FileUtils::getInstance()->addSearchPath(path,true);
autoscene=HelloWorld::scene();
Director::getInstance()->replaceScene(scene);
AssetsManager*Upgrade::getAssetManager()
staticAssetsManager*assetManager=NULL;
if(!assetManager)
assetManager=newAssetsManager("http://shezzer.sinaapp.com/downloadTest/cocos2dx-update-temp-package.zip",Simsun; font-size:14px; line-height:30px"> "http://shezzer.sinaapp.com/downloadTest/version.PHP",Simsun; font-size:14px; line-height:30px"> _pathToSave.c_str());
assetManager->setDelegate(this);
assetManager->setConnectionTimeout(3);
returnassetManager;
voidUpgrade::initDownloadDir()
CCLOG("initDownloadDir");
_pathToSave=CCFileUtils::getInstance()->getWritablePath();
_pathToSave+=DOWNLOAD_FIEL;
CCLOG("Path:%s",_pathToSave.c_str());
DIR*pDir=NULL;
pDir=opendir(_pathToSave.c_str());
if(!pDir)
mkdir(_pathToSave.c_str(),S_IRWXU|S_IRWXG|S_IRWXO);
#else
if((GetFileAttributesA(_pathToSave.c_str()))==INVALID_FILE_ATTRIBUTES)
CreateDirectoryA(_pathToSave.c_str(),0);
CCLOG("initDownloadDirend");
voidUpgrade::reset(Ref*pSender)
_showDownloadInfo->setString("");
//Removedownloadedfiles
stringcommand="rm-r";
//Pathmayincludespace.
command+="\""+_pathToSave+"\"";
system(command.c_str());
std::stringcommand="rd/s/q";
getAssetManager()->deleteVersion();
voidUpgrade::upgrade(Ref*pSender)
getAssetManager()->update();
运行:
Reset:重置版本号,办删除下载资源。
Upgrad:校验版本号,当有更新时下载新资源。
_pathToSave保存着文件的下载路径,压缩包下载下来后,将被自动解压到_pathToSave中。
以win32为,在cocos2d-x\build\Debug.win32\中,可以看到我们之前设置的下载目录download。
onSuccess中,当下载成功后,将跳转到HelloWorld。
这时可以在HelloWorld中直接使用已下载的资源。

猜你在找的Cocos2d-x相关文章