cocos2dx 3.1.1 在线热更新 自动更新(使用AssetsManager更新游戏资源包)

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

为什么要在线更新资源和脚本文件

简单概括,如果你的游戏项目已经在google play 或Apple Store 等平台上架了,那么当你项目需要做一些活动或者修改前端的一些代码等那么你需要重新提交一个新版本给平台。但是平台审核和具体的上架时间是个不确定的。具体什么时候能上架,主要由具体的平台决定。

如果游戏项目是使用脚本语言进行编写的(如lua、js),那么一旦需要更新,则可以通过从服务器下载最新的脚本和资源,从而跳过平台直接实现在线更新。(有些平台是禁止在线更新资源方式的,但是你懂得)


闲话少说,本文主要是解决如何在项目中实现在线更新:

我们这里用的是cocos2dx的类AssertsMananger,它在引擎的extensions\assets-manager可以看到。

AssetsManager传三个参数,资源的zip包路径,version路径,写文件的路径。

然后调用AssetsManager的update函数进行下载更新。

设置资源包名称
这里继续沿用cocos2dx的AssetsManager类中默认的名称:cocos2dx-update-temp-package.zip。
如果想要修改文件名,可以直接修改引擎下 extensions\assets-manager\AsetsManager.ccp中的TEMP_PACKAGE_FILE_NAME


选定服务器地址和设置版本号 <喎�"/kf/ware/vc/" target="_blank" class="keylink">vc3Ryb25nPgogztLV4sDv08O1xMrHss6/vNfKwc/Su6OsTmVsc7XEuPbIy7/VvOSjqLKpv8212Na3aHR0cDovL3d3dy41OHBsYXllci5jb20vYmxvZy0yNTM3LTk1OTEzLmh0bWyjqcv7tcTXytS0sPzCt762us2xvrDmusWhoyAKIGh0dHA6Ly9zaGV6emVyLnNpbmFhcHAuY29tL2Rvd25sb2FkVGVzdC9jb2NvczJkeC11cGRhdGUtdGVtcC1wYWNrYWdlLnppcCA8YnI+CiBodHRwOi8vc2hlenplci5zaW5hYXBwLmNvbS9kb3dubG9hZFRlc3QvdmVyc2lvbi5waHAgIAo8YnI+Cgo8YnI+CjxzdHJvbmc+QyYjNDM7JiM0Mzu0+sLryrXP1jwvc3Ryb25nPjxicj4K0MK9qFVwZ3JhZGXA4KOsvMyz0NfUQ0NMYXllcgqx4LytVXBncmFkZS5ozsS8/sTayN3I58/Co7oKPGJyPgo8cHJlIGNsYXNzPQ=="brush:java;">// Upgrade.h // Created by Sharezer on 14-11-23. // #ifndef _UPGRADE_H_ #define _UPGRADE_H_ #include "cocos2d.h" #include "extensions/cocos-ext.h" class Upgrade : public cocos2d::CCLayer,public cocos2d::extension::AssetsManagerDelegateProtocol { public: Upgrade(); virtual ~Upgrade(); virtual bool init(); void upgrade(cocos2d::Ref* pSender); //检查版本更新 void reset(cocos2d::Ref* pSender); //重置版本 virtual void onError(cocos2d::extension::AssetsManager::ErrorCode errorCode); //错误信息 virtual void onProgress(int percent); //更新下载进度 virtual void onSuccess(); //下载成功 CREATE_FUNC(Upgrade); private: cocos2d::extension::AssetsManager* getAssetManager(); void initDownloadDir(); //创建下载目录 private: std::string _pathToSave; cocos2d::Label *_showDownloadInfo; }; #endif

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

其中 Upgrade::onSuccess()函数中,我这里调用的是main.lua文件
?
5
auto engine = LuaEngine::getInstance();
ScriptEngineManager::getInstance()->setScriptEngine(engine);
)) {
;
}
如果是c++项目,可以自己调用相应的C++文件。 如 #include "HelloWorldScene.h" auto scene = HelloWorld::scene();
Director::getInstance()->replaceScene(scene);

修改AppDelegate.cpp文件调用Upgrade类AppDelegate.h无需修改,cpp稍微修改一下就可以了
文件中加入#include "Upgrade.h"
主要是修改了AppDelegate::applicationDidFinishLaunching()函数调用Upgrade类
?
68
"AppDelegate.h"
"CCLuaEngine.h"
"SimpleAudioEngine.h"
"cocos2d.h"
"Upgrade.h"
using namespace CocosDenshion;
USING_NS_CC;
using namespace std;
AppDelegate::AppDelegate()
{
}
AppDelegate::~AppDelegate()
{
SimpleAudioEngine::end();
}
bool AppDelegate::applicationDidFinishLaunching()
{
// initialize director
auto director = Director::getInstance();
auto glview = director->getOpenGLView();
(!glview) {
glview = GLView::createWithRect( @H_161_404@"dragan" 900 640 ));
director->setOpenGLView(glview);
}
glview->setDesignResolutionSize( 480 320
// turn on display FPS
director->setDisplayStats( true );
// set FPS. the default value is 1.0/60 if you don't call this
director->setAnimationInterval( 1.0 / 60 );
//auto engine = LuaEngine::getInstance();
//ScriptEngineManager::getInstance()->setScriptEngine(engine);
//if (engine->executeScriptFile("src/main.lua")) {
// return false;
//}
auto scene = Scene::create();
auto layer = Upgrade::create();
Director::getInstance()->runWithScene(scene);
scene->addChild(layer);
;
}
// This function will be called when the app is inactive. When comes a phone call,it's be invoked too
AppDelegate::applicationDidEnterBackground()
{
Director::getInstance()->stopAnimation();
SimpleAudioEngine::getInstance()->pauseBackgroundMusic();
}
// this function will be called when the app is active again
AppDelegate::applicationWillEnterForeground()
Director::getInstance()->startAnimation();
SimpleAudioEngine::getInstance()->resumeBackgroundMusic();
}

修改cocos2dx的main.lua 我只是把其中一个资源文件名字和路径参考下载资源包中的路径修改了,以便看到资源更新的效果。 例如我把农场背景图片改为了下载资源包中的3D/CompleteMap.PNG
编译运行项目:


Reset:用于重置版本号,办删除下载资源。
Upgrad:校验版本号,当有更新时下载新资源。


_pathToSave保存着文件的下载路径,压缩包下载下来后,将被自动解压到_pathToSave中。html Nels的个人空间 cocos2dx 3.2引擎版本 lua热更新 自动更新 http://zengrong.net/post/2131.htm 这个关于lua热更新的文章也是写得甚好!十分容易明白,步骤清晰 http://blog.csdn.net/xiaominghimi/article/details/8825524 Himi关于热更新的解析贴 cocos2dx 2.x引擎版本 http://blog.csdn.net/cloud95/article/details/38065085 cocos2dx lua热更新 实例版本,语言貌似比较通俗 http://www.cocoachina.com/bbs/simple/?t183552.html cocos关于热更新的讨论帖(关于路径的讨论很经典) http://www.cocoachina.com/bbs/read.PHP?tid=213066 cocos2dx lua 游戏热更新 http://lcinx.blog.163.com/blog/static/43494267201210270345232/ http://my.oschina.net/u/1785418/blog/283043 基于Quick-cocos2dx 2.2.3 的动态更新实现完整篇。(打包,服务器接口,模块自更新) http://blog.csdn.net/q277055799/article/details/8463835 lua热更新原理 http://lcinx.blog.163.com/blog/static/43494267201210270345232/ lua 热更新原理

原文链接:/cocos2dx/339388.html

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