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、实现

[cpp]  view plain copy
  1. //  Upgrade.h  
  2. //<img src="https://img-blog.csdn.net/20140728093037654?watermark/2/text/aHR0cDovL2Jsb2cuY3Nkbi5uZXQvTGlhbmdzaGFvemU=/font/5a6L5L2T/fontsize/400/fill/I0JBQkFCMA==/dissolve/70/gravity/SouthEast" alt="" />  
  3. //  Created by Sharezer on 14-07-26.  
  4. //  
  5. //  
  6.   
  7. #ifndef _UPGRADE_H_  
  8. #define _UPGRADE_H_  
  9. #include "cocos2d.h"  
  10. #include "extensions/cocos-ext.h"  
  11.   
  12. class Upgrade : public cocos2d::CCLayer, public cocos2d::extension::AssetsManagerDelegateProtocol  
  13. {  
  14. public:  
  15.     Upgrade();  
  16.     virtual ~Upgrade();  
  17.   
  18.     virtual bool init();  
  19.   
  20.     void upgrade(cocos2d::Ref* pSender);    //检查版本更新  
  21.     void reset(cocos2d::Ref* pSender);      //重置版本  
  22.   
  23.     virtual void onError(cocos2d::extension::AssetsManager::ErrorCode errorCode);       //错误信息  
  24.     virtual void onProgress(int percent);   //更新下载进度  
  25.     virtual void onSuccess();       //下载成功  
  26.     CREATE_FUNC(Upgrade);  
  27. private:  
  28.     cocos2d::extension::AssetsManager* getAssetManager();  
  29.     void initDownloadDir();     //创建下载目录  
  30.   
  31. private:  
  32.     std::string _pathToSave;  
  33.     cocos2d::Label *_showDownloadInfo;  
  34. };  
  35.   
  36.   
  37. #endif  

[cpp]  view plain copy
  1. //Upgrade.cpp  
[cpp]  view plain copy
  1. #include "Upgrade.h"  
  2. #include "HelloWorldScene.h"  
  3.   
  4. #if (CC_TARGET_PLATFORM != CC_PLATFORM_WIN32)  
  5. #include <dirent.h>  
  6. #include <sys/stat.h>  
  7. #endif  
  8.   
  9. USING_NS_CC;  
  10. USING_NS_CC_EXT;  
  11.   
  12. #define DOWNLOAD_FIEL       "download"  //下载后保存的文件夹名  
  13.   
  14. Upgrade::Upgrade():  
  15. _pathToSave(""),  
  16. _showDownloadInfo(NULL)  
  17. {  
  18.   
  19. }  
  20.   
  21. Upgrade::~Upgrade()  
  22. {  
  23.     AssetsManager* assetManager = getAssetManager();  
  24.     CC_SAFE_DELETE(assetManager);  
  25. }  
  26.   
  27. bool Upgrade::init()  
  28. {  
  29.     if (!CCLayer::init())  
  30.     {  
  31.         return false;  
  32.     }  
  33.     Size winSize = Director::getInstance()->getWinSize();  
  34.     initDownloadDir();  
  35.     _showDownloadInfo = Label::createWithSystemFont("""Arial", 20);  
  36.     this->addChild(_showDownloadInfo);  
  37.     _showDownloadInfo->setPosition(Vec2(winSize.width / 2, winSize.height / 2 - 20));  
  38.   
  39.   
  40.     auto itemLabel1 = MenuItemLabel::create(  
  41.         Label::createWithSystemFont("Reset""Arail", 20), CC_CALLBACK_1(Upgrade::reset, this));  
  42.     auto itemLabel2 = MenuItemLabel::create(  
  43.         Label::createWithSystemFont("Upgrad""Arail", 20), CC_CALLBACK_1(Upgrade::upgrade, this));  
  44.   
  45.     auto menu = Menu::create(itemLabel1, itemLabel2, NULL);  
  46.     this->addChild(menu);  
  47.   
  48.     itemLabel1->setPosition(Vec2(winSize.width / 2, winSize.height / 2 + 20));  
  49.     itemLabel2->setPosition(Vec2(winSize.width / 2, winSize.height / 2 ));  
  50.   
  51.     menu->setPosition(Vec2::ZERO);  
  52.   
  53.     return true;  
  54. }  
  55.   
  56. void Upgrade::onError(AssetsManager::ErrorCode errorCode)  
  57. {  
  58.     if (errorCode == AssetsManager::ErrorCode::NO_NEW_VERSION)  
  59.     {  
  60.         _showDownloadInfo->setString("no new version");  
  61.     }  
  62.     else if (errorCode == AssetsManager::ErrorCode::NETWORK)  
  63.     {  
  64.         _showDownloadInfo->setString("network error");  
  65.     }  
  66.     else if (errorCode == AssetsManager::ErrorCode::CREATE_FILE)  
  67.     {  
  68.         _showDownloadInfo->setString("create file error");  
  69.     }  
  70. }  
  71.   
  72. void Upgrade::onProgress(int percent)  
  73. {  
  74.     if (percent < 0)  
  75.         return;  
  76.     char progress[20];  
  77.     snprintf(progress, 20, "download %d%%", percent);  
  78.     _showDownloadInfo->setString(progress);  
  79. }  
  80.   
  81. void Upgrade::onSuccess()  
  82. {  
  83.     CCLOG("download success");  
  84.     _showDownloadInfo->setString("download success");  
  85.     std::string path = FileUtils::getInstance()->getWritablePath() + DOWNLOAD_FIEL;  
  86.     //FileUtils::getInstance()->addSearchPath(path, true);  
  87.     auto scene = HelloWorld::scene();  
  88.     Director::getInstance()->replaceScene(scene);  
  89. }  
  90.   
  91. AssetsManager* Upgrade::getAssetManager()  
  92. {  
  93.     static AssetsManager *assetManager = NULL;  
  94.     if (!assetManager)  
  95.     {  
  96.         assetManager = new AssetsManager("http://shezzer.sinaapp.com/downloadTest/cocos2dx-update-temp-package.zip",  
  97.             "http://shezzer.sinaapp.com/downloadTest/version.php",  
  98.             _pathToSave.c_str());  
  99.         assetManager->setDelegate(this);  
  100.         assetManager->setConnectionTimeout(3);  
  101.     }  
  102.     return assetManager;  
  103. }  
  104.   
  105. void Upgrade::initDownloadDir()  
  106. {  
  107.     CCLOG("initDownloadDir");  
  108.     _pathToSave = CCFileUtils::getInstance()->getWritablePath();  
  109.     _pathToSave += DOWNLOAD_FIEL;  
  110. CCLOG("Path: %s", _pathToSave.c_str());  
  111. #if (CC_TARGET_PLATFORM != CC_PLATFORM_WIN32)  
  112.     DIR *pDir = NULL;  
  113.     pDir = opendir(_pathToSave.c_str());  
  114.     if (!pDir)  
  115.     {  
  116.         mkdir(_pathToSave.c_str(), S_IRWXU | S_IRWXG | S_IRWXO);  
  117.     }  
  118. #else  
  119.     if ((GetFileAttributesA(_pathToSave.c_str())) == INVALID_FILE_ATTRIBUTES)  
  120.     {  
  121.         CreateDirectoryA(_pathToSave.c_str(), 0);  
  122.     }  
  123. #endif  
  124.     CCLOG("initDownloadDir end");  
  125. }  
  126.   
  127. void Upgrade::reset(Ref* pSender)  
  128. {  
  129.     _showDownloadInfo->setString("");  
  130.     // Remove downloaded files  
  131. #if (CC_TARGET_PLATFORM != CC_PLATFORM_WIN32)  
  132.     string command = "rm -r ";  
  133.     // Path may include space.  
  134.     command += "\"" + _pathToSave + "\"";  
  135.     system(command.c_str());  
  136. #else  
  137.     std::string command = "rd /s /q ";  
  138.     // Path may include space.  
  139.     command += "\"" + _pathToSave + "\"";  
  140.     system(command.c_str());  
  141. #endif  
  142.     getAssetManager()->deleteVersion();  
  143.     initDownloadDir();  
  144. }  
  145.   
  146. void Upgrade::upgrade(Ref* pSender)  
  147. {  
  148.     _showDownloadInfo->setString("");  
  149.     getAssetManager()->update();  
  150.   
  151. }  

运行:


Reset:重置版本号,办删除下载资源。

Upgrad:校验版本号,当有更新时下载新资源。


_pathToSave保存着文件的下载路径,压缩包下载下来后,将被自动解压到_pathToSave中。

以win32为,在cocos2d-x\build\Debug.win32\  中,可以看到我们之前设置的下载目录download。

onSuccess中,当下载成功后,将跳转到HelloWorld。

这时可以在HelloWorld中直接使用已下载的资源。


具体实现代码见下载地址:点击打开链接

转自:http://blog.csdn.net/liangshaoze/article/details/38224013

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值