【Cocos2d-x】实现资源热更新
2015-01-07 23:44
281 查看
转载地址:/article/1371597.html
什么是热更新?
游戏客户端启动时,主动请求服务端检查版本号,并更新资源到本地。
应用场景:
情况一:游戏客户端已经发布了,但突然发现有个比较严重的bug需要修复。这时需要更新游戏的代码(Lua代码)。
情况二:情人节到了,需要搞个活动,在游戏中营造一个节日氛围。这时,需要更新游戏资源或增加一些功能。
好处:不需要重新打包和提交应用到市场等待审核。
![](http://img.blog.csdn.net/20150101181716090?watermark/2/text/aHR0cDovL2Jsb2cuY3Nkbi5uZXQvbGluY2hhb2xvbmc=/font/5a6L5L2T/fontsize/400/fill/I0JBQkFCMA==/dissolve/70/gravity/Center)
在Cocos2d-x中已经封装了用于实现热更新功能的类,就是AssetsManager。
api说明:
// 检测是否有版本更新
virtual bool checkUpdate();
// 下载更新的资源包并解压到下载路径
virtual void update();
// 获取当前客户端版本号
std::string getVersion();
// 删除客户端版本号
void deleteVersion();
// 设置下载回调(AssetsManagerDelegateProtocol)
void setDelegate(AssetsManagerDelegateProtocol *delegate);
// 设置连接超时时间(单位:秒)
void setConnectionTimeout(unsigned int timeout);
// 设置从服务端下载资源包的url
void setPackageUrl(const char* packageUrl);
// 设置服务端获取版本号的url
void setVersionFileUrl(const char* versionFileUrl);
// 设置资源保存路径
void setStoragePath(const char* storagePath);
因为AssetsManager使用了pthread库,所以需要在win32工程中需要包含pthread库所在目录。
VS在工程属性——C/C++——常规——附加包含目录中添加:$(ProjectDir)..\..\..\cocos2dx\platform\third_party\win32\pthread
该工程使用Cocos2d-x2.1.6和VS2012。【点击下载源码】
把工程放到引擎projects目录下即可。
来自CODE的代码片
UpdateLayer.cpp
来自CODE的代码片
UpdateLayer.h
项目地址:https://coding.net/u/linchaolong/p/Cocos2d-x_HotUpdate/git
什么是热更新?
游戏客户端启动时,主动请求服务端检查版本号,并更新资源到本地。
应用场景:
情况一:游戏客户端已经发布了,但突然发现有个比较严重的bug需要修复。这时需要更新游戏的代码(Lua代码)。
情况二:情人节到了,需要搞个活动,在游戏中营造一个节日氛围。这时,需要更新游戏资源或增加一些功能。
好处:不需要重新打包和提交应用到市场等待审核。
![](http://img.blog.csdn.net/20150101181716090?watermark/2/text/aHR0cDovL2Jsb2cuY3Nkbi5uZXQvbGluY2hhb2xvbmc=/font/5a6L5L2T/fontsize/400/fill/I0JBQkFCMA==/dissolve/70/gravity/Center)
在Cocos2d-x中已经封装了用于实现热更新功能的类,就是AssetsManager。
api说明:
// 检测是否有版本更新
virtual bool checkUpdate();
// 下载更新的资源包并解压到下载路径
virtual void update();
// 获取当前客户端版本号
std::string getVersion();
// 删除客户端版本号
void deleteVersion();
// 设置下载回调(AssetsManagerDelegateProtocol)
void setDelegate(AssetsManagerDelegateProtocol *delegate);
// 设置连接超时时间(单位:秒)
void setConnectionTimeout(unsigned int timeout);
// 设置从服务端下载资源包的url
void setPackageUrl(const char* packageUrl);
// 设置服务端获取版本号的url
void setVersionFileUrl(const char* versionFileUrl);
// 设置资源保存路径
void setStoragePath(const char* storagePath);
因为AssetsManager使用了pthread库,所以需要在win32工程中需要包含pthread库所在目录。
VS在工程属性——C/C++——常规——附加包含目录中添加:$(ProjectDir)..\..\..\cocos2dx\platform\third_party\win32\pthread
该工程使用Cocos2d-x2.1.6和VS2012。【点击下载源码】
把工程放到引擎projects目录下即可。
来自CODE的代码片
UpdateLayer.cpp
来自CODE的代码片
UpdateLayer.h
项目地址:https://coding.net/u/linchaolong/p/Cocos2d-x_HotUpdate/git
热更新介绍
什么是热更新?游戏客户端启动时,主动请求服务端检查版本号,并更新资源到本地。
应用场景:
情况一:游戏客户端已经发布了,但突然发现有个比较严重的bug需要修复。这时需要更新游戏的代码(Lua代码)。
情况二:情人节到了,需要搞个活动,在游戏中营造一个节日氛围。这时,需要更新游戏资源或增加一些功能。
好处:不需要重新打包和提交应用到市场等待审核。
热更新流程
AssetsManager
在Cocos2d-x中已经封装了用于实现热更新功能的类,就是AssetsManager。api说明:
// 检测是否有版本更新
virtual bool checkUpdate();
// 下载更新的资源包并解压到下载路径
virtual void update();
// 获取当前客户端版本号
std::string getVersion();
// 删除客户端版本号
void deleteVersion();
// 设置下载回调(AssetsManagerDelegateProtocol)
void setDelegate(AssetsManagerDelegateProtocol *delegate);
// 设置连接超时时间(单位:秒)
void setConnectionTimeout(unsigned int timeout);
// 设置从服务端下载资源包的url
void setPackageUrl(const char* packageUrl);
// 设置服务端获取版本号的url
void setVersionFileUrl(const char* versionFileUrl);
// 设置资源保存路径
void setStoragePath(const char* storagePath);
因为AssetsManager使用了pthread库,所以需要在win32工程中需要包含pthread库所在目录。
VS在工程属性——C/C++——常规——附加包含目录中添加:$(ProjectDir)..\..\..\cocos2dx\platform\third_party\win32\pthread
示例工程
该工程使用Cocos2d-x2.1.6和VS2012。【点击下载源码】把工程放到引擎projects目录下即可。
<a target=_blank id="L1" href="/article/1371597.html#L1" rel="#L1" style="text-decoration: none; color: rgb(12, 137, 207);"> 1</a> <a target=_blank id="L2" href="/article/1371597.html#L2" rel="#L2" style="text-decoration: none; color: rgb(12, 137, 207);"> 2</a> <a target=_blank id="L3" href="/article/1371597.html#L3" rel="#L3" style="text-decoration: none; color: rgb(12, 137, 207);"> 3</a> <a target=_blank id="L4" href="/article/1371597.html#L4" rel="#L4" style="text-decoration: none; color: rgb(12, 137, 207);"> 4</a> <a target=_blank id="L5" href="/article/1371597.html#L5" rel="#L5" style="text-decoration: none; color: rgb(12, 137, 207);"> 5</a> <a target=_blank id="L6" href="/article/1371597.html#L6" rel="#L6" style="text-decoration: none; color: rgb(12, 137, 207);"> 6</a> <a target=_blank id="L7" href="/article/1371597.html#L7" rel="#L7" style="text-decoration: none; color: rgb(12, 137, 207);"> 7</a> <a target=_blank id="L8" href="/article/1371597.html#L8" rel="#L8" style="text-decoration: none; color: rgb(12, 137, 207);"> 8</a> <a target=_blank id="L9" href="/article/1371597.html#L9" rel="#L9" style="text-decoration: none; color: rgb(12, 137, 207);"> 9</a> <a target=_blank id="L10" href="/article/1371597.html#L10" rel="#L10" style="text-decoration: none; color: rgb(12, 137, 207);"> 10</a> <a target=_blank id="L11" href="/article/1371597.html#L11" rel="#L11" style="text-decoration: none; color: rgb(12, 137, 207);"> 11</a> <a target=_blank id="L12" href="/article/1371597.html#L12" rel="#L12" style="text-decoration: none; color: rgb(12, 137, 207);"> 12</a> <a target=_blank id="L13" href="/article/1371597.html#L13" rel="#L13" style="text-decoration: none; color: rgb(12, 137, 207);"> 13</a> <a target=_blank id="L14" href="/article/1371597.html#L14" rel="#L14" style="text-decoration: none; color: rgb(12, 137, 207);"> 14</a> <a target=_blank id="L15" href="/article/1371597.html#L15" rel="#L15" style="text-decoration: none; color: rgb(12, 137, 207);"> 15</a> <a target=_blank id="L16" href="/article/1371597.html#L16" rel="#L16" style="text-decoration: none; color: rgb(12, 137, 207);"> 16</a> <a target=_blank id="L17" href="/article/1371597.html#L17" rel="#L17" style="text-decoration: none; color: rgb(12, 137, 207);"> 17</a> <a target=_blank id="L18" href="/article/1371597.html#L18" rel="#L18" style="text-decoration: none; color: rgb(12, 137, 207);"> 18</a> <a target=_blank id="L19" href="/article/1371597.html#L19" rel="#L19" style="text-decoration: none; color: rgb(12, 137, 207);"> 19</a> <a target=_blank id="L20" href="/article/1371597.html#L20" rel="#L20" style="text-decoration: none; color: rgb(12, 137, 207);"> 20</a> <a target=_blank id="L21" href="/article/1371597.html#L21" rel="#L21" style="text-decoration: none; color: rgb(12, 137, 207);"> 21</a> <a target=_blank id="L22" href="/article/1371597.html#L22" rel="#L22" style="text-decoration: none; color: rgb(12, 137, 207);"> 22</a> <a target=_blank id="L23" href="/article/1371597.html#L23" rel="#L23" style="text-decoration: none; color: rgb(12, 137, 207);"> 23</a> <a target=_blank id="L24" href="/article/1371597.html#L24" rel="#L24" style="text-decoration: none; color: rgb(12, 137, 207);"> 24</a> <a target=_blank id="L25" href="/article/1371597.html#L25" rel="#L25" style="text-decoration: none; color: rgb(12, 137, 207);"> 25</a> <a target=_blank id="L26" href="/article/1371597.html#L26" rel="#L26" style="text-decoration: none; color: rgb(12, 137, 207);"> 26</a> <a target=_blank id="L27" href="/article/1371597.html#L27" rel="#L27" style="text-decoration: none; color: rgb(12, 137, 207);"> 27</a> <a target=_blank id="L28" href="/article/1371597.html#L28" rel="#L28" style="text-decoration: none; color: rgb(12, 137, 207);"> 28</a> <a target=_blank id="L29" href="/article/1371597.html#L29" rel="#L29" style="text-decoration: none; color: rgb(12, 137, 207);"> 29</a> <a target=_blank id="L30" href="/article/1371597.html#L30" rel="#L30" style="text-decoration: none; color: rgb(12, 137, 207);"> 30</a> <a target=_blank id="L31" href="/article/1371597.html#L31" rel="#L31" style="text-decoration: none; color: rgb(12, 137, 207);"> 31</a> <a target=_blank id="L32" href="/article/1371597.html#L32" rel="#L32" style="text-decoration: none; color: rgb(12, 137, 207);"> 32</a> <a target=_blank id="L33" href="/article/1371597.html#L33" rel="#L33" style="text-decoration: none; color: rgb(12, 137, 207);"> 33</a> <a target=_blank id="L34" href="/article/1371597.html#L34" rel="#L34" style="text-decoration: none; color: rgb(12, 137, 207);"> 34</a> <a target=_blank id="L35" href="/article/1371597.html#L35" rel="#L35" style="text-decoration: none; color: rgb(12, 137, 207);"> 35</a> <a target=_blank id="L36" href="/article/1371597.html#L36" rel="#L36" style="text-decoration: none; color: rgb(12, 137, 207);"> 36</a> <a target=_blank id="L37" href="/article/1371597.html#L37" rel="#L37" style="text-decoration: none; color: rgb(12, 137, 207);"> 37</a> <a target=_blank id="L38" href="/article/1371597.html#L38" rel="#L38" style="text-decoration: none; color: rgb(12, 137, 207);"> 38</a> <a target=_blank id="L39" href="/article/1371597.html#L39" rel="#L39" style="text-decoration: none; color: rgb(12, 137, 207);"> 39</a> <a target=_blank id="L40" href="/article/1371597.html#L40" rel="#L40" style="text-decoration: none; color: rgb(12, 137, 207);"> 40</a> <a target=_blank id="L41" href="/article/1371597.html#L41" rel="#L41" style="text-decoration: none; color: rgb(12, 137, 207);"> 41</a> <a target=_blank id="L42" href="/article/1371597.html#L42" rel="#L42" style="text-decoration: none; color: rgb(12, 137, 207);"> 42</a> <a target=_blank id="L43" href="/article/1371597.html#L43" rel="#L43" style="text-decoration: none; color: rgb(12, 137, 207);"> 43</a> <a target=_blank id="L44" href="/article/1371597.html#L44" rel="#L44" style="text-decoration: none; color: rgb(12, 137, 207);"> 44</a> <a target=_blank id="L45" href="/article/1371597.html#L45" rel="#L45" style="text-decoration: none; color: rgb(12, 137, 207);"> 45</a> <a target=_blank id="L46" href="/article/1371597.html#L46" rel="#L46" style="text-decoration: none; color: rgb(12, 137, 207);"> 46</a> <a target=_blank id="L47" href="/article/1371597.html#L47" rel="#L47" style="text-decoration: none; color: rgb(12, 137, 207);"> 47</a> <a target=_blank id="L48" href="/article/1371597.html#L48" rel="#L48" style="text-decoration: none; color: rgb(12, 137, 207);"> 48</a> <a target=_blank id="L49" href="/article/1371597.html#L49" rel="#L49" style="text-decoration: none; color: rgb(12, 137, 207);"> 49</a> <a target=_blank id="L50" href="/article/1371597.html#L50" rel="#L50" style="text-decoration: none; color: rgb(12, 137, 207);"> 50</a> <a target=_blank id="L51" href="/article/1371597.html#L51" rel="#L51" style="text-decoration: none; color: rgb(12, 137, 207);"> 51</a> <a target=_blank id="L52" href="/article/1371597.html#L52" rel="#L52" style="text-decoration: none; color: rgb(12, 137, 207);"> 52</a> <a target=_blank id="L53" href="/article/1371597.html#L53" rel="#L53" style="text-decoration: none; color: rgb(12, 137, 207);"> 53</a> <a target=_blank id="L54" href="/article/1371597.html#L54" rel="#L54" style="text-decoration: none; color: rgb(12, 137, 207);"> 54</a> <a target=_blank id="L55" href="/article/1371597.html#L55" rel="#L55" style="text-decoration: none; color: rgb(12, 137, 207);"> 55</a> <a target=_blank id="L56" href="/article/1371597.html#L56" rel="#L56" style="text-decoration: none; color: rgb(12, 137, 207);"> 56</a> <a target=_blank id="L57" href="/article/1371597.html#L57" rel="#L57" style="text-decoration: none; color: rgb(12, 137, 207);"> 57</a> <a target=_blank id="L58" href="/article/1371597.html#L58" rel="#L58" style="text-decoration: none; color: rgb(12, 137, 207);"> 58</a> <a target=_blank id="L59" href="/article/1371597.html#L59" rel="#L59" style="text-decoration: none; color: rgb(12, 137, 207);"> 59</a> <a target=_blank id="L60" href="/article/1371597.html#L60" rel="#L60" style="text-decoration: none; color: rgb(12, 137, 207);"> 60</a> <a target=_blank id="L61" href="/article/1371597.html#L61" rel="#L61" style="text-decoration: none; color: rgb(12, 137, 207);"> 61</a> <a target=_blank id="L62" href="/article/1371597.html#L62" rel="#L62" style="text-decoration: none; color: rgb(12, 137, 207);"> 62</a> <a target=_blank id="L63" href="/article/1371597.html#L63" rel="#L63" style="text-decoration: none; color: rgb(12, 137, 207);"> 63</a> <a target=_blank id="L64" href="/article/1371597.html#L64" rel="#L64" style="text-decoration: none; color: rgb(12, 137, 207);"> 64</a> <a target=_blank id="L65" href="/article/1371597.html#L65" rel="#L65" style="text-decoration: none; color: rgb(12, 137, 207);"> 65</a> <a target=_blank id="L66" href="/article/1371597.html#L66" rel="#L66" style="text-decoration: none; color: rgb(12, 137, 207);"> 66</a> <a target=_blank id="L67" href="/article/1371597.html#L67" rel="#L67" style="text-decoration: none; color: rgb(12, 137, 207);"> 67</a> <a target=_blank id="L68" href="/article/1371597.html#L68" rel="#L68" style="text-decoration: none; color: rgb(12, 137, 207);"> 68</a> <a target=_blank id="L69" href="/article/1371597.html#L69" rel="#L69" style="text-decoration: none; color: rgb(12, 137, 207);"> 69</a> <a target=_blank id="L70" href="/article/1371597.html#L70" rel="#L70" style="text-decoration: none; color: rgb(12, 137, 207);"> 70</a> <a target=_blank id="L71" href="/article/1371597.html#L71" rel="#L71" style="text-decoration: none; color: rgb(12, 137, 207);"> 71</a> <a target=_blank id="L72" href="/article/1371597.html#L72" rel="#L72" style="text-decoration: none; color: rgb(12, 137, 207);"> 72</a> <a target=_blank id="L73" href="/article/1371597.html#L73" rel="#L73" style="text-decoration: none; color: rgb(12, 137, 207);"> 73</a> <a target=_blank id="L74" href="/article/1371597.html#L74" rel="#L74" style="text-decoration: none; color: rgb(12, 137, 207);"> 74</a> <a target=_blank id="L75" href="/article/1371597.html#L75" rel="#L75" style="text-decoration: none; color: rgb(12, 137, 207);"> 75</a> <a target=_blank id="L76" href="/article/1371597.html#L76" rel="#L76" style="text-decoration: none; color: rgb(12, 137, 207);"> 76</a> <a target=_blank id="L77" href="/article/1371597.html#L77" rel="#L77" style="text-decoration: none; color: rgb(12, 137, 207);"> 77</a> <a target=_blank id="L78" href="/article/1371597.html#L78" rel="#L78" style="text-decoration: none; color: rgb(12, 137, 207);"> 78</a> <a target=_blank id="L79" href="/article/1371597.html#L79" rel="#L79" style="text-decoration: none; color: rgb(12, 137, 207);"> 79</a> <a target=_blank id="L80" href="/article/1371597.html#L80" rel="#L80" style="text-decoration: none; color: rgb(12, 137, 207);"> 80</a> <a target=_blank id="L81" href="/article/1371597.html#L81" rel="#L81" style="text-decoration: none; color: rgb(12, 137, 207);"> 81</a> <a target=_blank id="L82" href="/article/1371597.html#L82" rel="#L82" style="text-decoration: none; color: rgb(12, 137, 207);"> 82</a> <a target=_blank id="L83" href="/article/1371597.html#L83" rel="#L83" style="text-decoration: none; color: rgb(12, 137, 207);"> 83</a> <a target=_blank id="L84" href="/article/1371597.html#L84" rel="#L84" style="text-decoration: none; color: rgb(12, 137, 207);"> 84</a> <a target=_blank id="L85" href="/article/1371597.html#L85" rel="#L85" style="text-decoration: none; color: rgb(12, 137, 207);"> 85</a> <a target=_blank id="L86" href="/article/1371597.html#L86" rel="#L86" style="text-decoration: none; color: rgb(12, 137, 207);"> 86</a> <a target=_blank id="L87" href="/article/1371597.html#L87" rel="#L87" style="text-decoration: none; color: rgb(12, 137, 207);"> 87</a> <a target=_blank id="L88" href="/article/1371597.html#L88" rel="#L88" style="text-decoration: none; color: rgb(12, 137, 207);"> 88</a> <a target=_blank id="L89" href="/article/1371597.html#L89" rel="#L89" style="text-decoration: none; color: rgb(12, 137, 207);"> 89</a> <a target=_blank id="L90" href="/article/1371597.html#L90" rel="#L90" style="text-decoration: none; color: rgb(12, 137, 207);"> 90</a> <a target=_blank id="L91" href="/article/1371597.html#L91" rel="#L91" style="text-decoration: none; color: rgb(12, 137, 207);"> 91</a> <a target=_blank id="L92" href="/article/1371597.html#L92" rel="#L92" style="text-decoration: none; color: rgb(12, 137, 207);"> 92</a> <a target=_blank id="L93" href="/article/1371597.html#L93" rel="#L93" style="text-decoration: none; color: rgb(12, 137, 207);"> 93</a> <a target=_blank id="L94" href="/article/1371597.html#L94" rel="#L94" style="text-decoration: none; color: rgb(12, 137, 207);"> 94</a> <a target=_blank id="L95" href="/article/1371597.html#L95" rel="#L95" style="text-decoration: none; color: rgb(12, 137, 207);"> 95</a> <a target=_blank id="L96" href="/article/1371597.html#L96" rel="#L96" style="text-decoration: none; color: rgb(12, 137, 207);"> 96</a> <a target=_blank id="L97" href="/article/1371597.html#L97" rel="#L97" style="text-decoration: none; color: rgb(12, 137, 207);"> 97</a> <a target=_blank id="L98" href="/article/1371597.html#L98" rel="#L98" style="text-decoration: none; color: rgb(12, 137, 207);"> 98</a> <a target=_blank id="L99" href="/article/1371597.html#L99" rel="#L99" style="text-decoration: none; color: rgb(12, 137, 207);"> 99</a> <a target=_blank id="L100" href="/article/1371597.html#L100" rel="#L100" style="text-decoration: none; color: rgb(12, 137, 207);"> 100</a> <a target=_blank id="L101" href="/article/1371597.html#L101" rel="#L101" style="text-decoration: none; color: rgb(12, 137, 207);"> 101</a> <a target=_blank id="L102" href="/article/1371597.html#L102" rel="#L102" style="text-decoration: none; color: rgb(12, 137, 207);"> 102</a> <a target=_blank id="L103" href="/article/1371597.html#L103" rel="#L103" style="text-decoration: none; color: rgb(12, 137, 207);"> 103</a> <a target=_blank id="L104" href="/article/1371597.html#L104" rel="#L104" style="text-decoration: none; color: rgb(12, 137, 207);"> 104</a> <a target=_blank id="L105" href="/article/1371597.html#L105" rel="#L105" style="text-decoration: none; color: rgb(12, 137, 207);"> 105</a> <a target=_blank id="L106" href="/article/1371597.html#L106" rel="#L106" style="text-decoration: none; color: rgb(12, 137, 207);"> 106</a> <a target=_blank id="L107" href="/article/1371597.html#L107" rel="#L107" style="text-decoration: none; color: rgb(12, 137, 207);"> 107</a> <a target=_blank id="L108" href="/article/1371597.html#L108" rel="#L108" style="text-decoration: none; color: rgb(12, 137, 207);"> 108</a> <a target=_blank id="L109" href="/article/1371597.html#L109" rel="#L109" style="text-decoration: none; color: rgb(12, 137, 207);"> 109</a> <a target=_blank id="L110" href="/article/1371597.html#L110" rel="#L110" style="text-decoration: none; color: rgb(12, 137, 207);"> 110</a> <a target=_blank id="L111" href="/article/1371597.html#L111" rel="#L111" style="text-decoration: none; color: rgb(12, 137, 207);"> 111</a> <a target=_blank id="L112" href="/article/1371597.html#L112" rel="#L112" style="text-decoration: none; color: rgb(12, 137, 207);"> 112</a> <a target=_blank id="L113" href="/article/1371597.html#L113" rel="#L113" style="text-decoration: none; color: rgb(12, 137, 207);"> 113</a> <a target=_blank id="L114" href="/article/1371597.html#L114" rel="#L114" style="text-decoration: none; color: rgb(12, 137, 207);"> 114</a> <a target=_blank id="L115" href="/article/1371597.html#L115" rel="#L115" style="text-decoration: none; color: rgb(12, 137, 207);"> 115</a> <a target=_blank id="L116" href="/article/1371597.html#L116" rel="#L116" style="text-decoration: none; color: rgb(12, 137, 207);"> 116</a> <a target=_blank id="L117" href="/article/1371597.html#L117" rel="#L117" style="text-decoration: none; color: rgb(12, 137, 207);"> 117</a> <a target=_blank id="L118" href="/article/1371597.html#L118" rel="#L118" style="text-decoration: none; color: rgb(12, 137, 207);"> 118</a> <a target=_blank id="L119" href="/article/1371597.html#L119" rel="#L119" style="text-decoration: none; color: rgb(12, 137, 207);"> 119</a> <a target=_blank id="L120" href="/article/1371597.html#L120" rel="#L120" style="text-decoration: none; color: rgb(12, 137, 207);"> 120</a> <a target=_blank id="L121" href="/article/1371597.html#L121" rel="#L121" style="text-decoration: none; color: rgb(12, 137, 207);"> 121</a> <a target=_blank id="L122" href="/article/1371597.html#L122" rel="#L122" style="text-decoration: none; color: rgb(12, 137, 207);"> 122</a> <a target=_blank id="L123" href="/article/1371597.html#L123" rel="#L123" style="text-decoration: none; color: rgb(12, 137, 207);"> 123</a> <a target=_blank id="L124" href="/article/1371597.html#L124" rel="#L124" style="text-decoration: none; color: rgb(12, 137, 207);"> 124</a> <a target=_blank id="L125" href="/article/1371597.html#L125" rel="#L125" style="text-decoration: none; color: rgb(12, 137, 207);"> 125</a> <a target=_blank id="L126" href="/article/1371597.html#L126" rel="#L126" style="text-decoration: none; color: rgb(12, 137, 207);"> 126</a> <a target=_blank id="L127" href="/article/1371597.html#L127" rel="#L127" style="text-decoration: none; color: rgb(12, 137, 207);"> 127</a> <a target=_blank id="L128" href="/article/1371597.html#L128" rel="#L128" style="text-decoration: none; color: rgb(12, 137, 207);"> 128</a> <a target=_blank id="L129" href="/article/1371597.html#L129" rel="#L129" style="text-decoration: none; color: rgb(12, 137, 207);"> 129</a> <a target=_blank id="L130" href="/article/1371597.html#L130" rel="#L130" style="text-decoration: none; color: rgb(12, 137, 207);"> 130</a> <a target=_blank id="L131" href="/article/1371597.html#L131" rel="#L131" style="text-decoration: none; color: rgb(12, 137, 207);"> 131</a> <a target=_blank id="L132" href="/article/1371597.html#L132" rel="#L132" style="text-decoration: none; color: rgb(12, 137, 207);"> 132</a> <a target=_blank id="L133" href="/article/1371597.html#L133" rel="#L133" style="text-decoration: none; color: rgb(12, 137, 207);"> 133</a> <a target=_blank id="L134" href="/article/1371597.html#L134" rel="#L134" style="text-decoration: none; color: rgb(12, 137, 207);"> 134</a> <a target=_blank id="L135" href="/article/1371597.html#L135" rel="#L135" style="text-decoration: none; color: rgb(12, 137, 207);"> 135</a> <a target=_blank id="L136" href="/article/1371597.html#L136" rel="#L136" style="text-decoration: none; color: rgb(12, 137, 207);"> 136</a> <a target=_blank id="L137" href="/article/1371597.html#L137" rel="#L137" style="text-decoration: none; color: rgb(12, 137, 207);"> 137</a> <a target=_blank id="L138" href="/article/1371597.html#L138" rel="#L138" style="text-decoration: none; color: rgb(12, 137, 207);"> 138</a> <a target=_blank id="L139" href="/article/1371597.html#L139" rel="#L139" style="text-decoration: none; color: rgb(12, 137, 207);"> 139</a> <a target=_blank id="L140" href="/article/1371597.html#L140" rel="#L140" style="text-decoration: none; color: rgb(12, 137, 207);"> 140</a> <a target=_blank id="L141" href="/article/1371597.html#L141" rel="#L141" style="text-decoration: none; color: rgb(12, 137, 207);"> 141</a> <a target=_blank id="L142" href="/article/1371597.html#L142" rel="#L142" style="text-decoration: none; color: rgb(12, 137, 207);"> 142</a> <a target=_blank id="L143" href="/article/1371597.html#L143" rel="#L143" style="text-decoration: none; color: rgb(12, 137, 207);"> 143</a> <a target=_blank id="L144" href="/article/1371597.html#L144" rel="#L144" style="text-decoration: none; color: rgb(12, 137, 207);"> 144</a> <a target=_blank id="L145" href="/article/1371597.html#L145" rel="#L145" style="text-decoration: none; color: rgb(12, 137, 207);"> 145</a> <a target=_blank id="L146" href="/article/1371597.html#L146" rel="#L146" style="text-decoration: none; color: rgb(12, 137, 207);"> 146</a> <a target=_blank id="L147" href="/article/1371597.html#L147" rel="#L147" style="text-decoration: none; color: rgb(12, 137, 207);"> 147</a> <a target=_blank id="L148" href="/article/1371597.html#L148" rel="#L148" style="text-decoration: none; color: rgb(12, 137, 207);"> 148</a> <a target=_blank id="L149" href="/article/1371597.html#L149" rel="#L149" style="text-decoration: none; color: rgb(12, 137, 207);"> 149</a> <a target=_blank id="L150" href="/article/1371597.html#L150" rel="#L150" style="text-decoration: none; color: rgb(12, 137, 207);"> 150</a> <a target=_blank id="L151" href="/article/1371597.html#L151" rel="#L151" style="text-decoration: none; color: rgb(12, 137, 207);"> 151</a> <a target=_blank id="L152" href="/article/1371597.html#L152" rel="#L152" style="text-decoration: none; color: rgb(12, 137, 207);"> 152</a> <a target=_blank id="L153" href="/article/1371597.html#L153" rel="#L153" style="text-decoration: none; color: rgb(12, 137, 207);"> 153</a> <a target=_blank id="L154" href="/article/1371597.html#L154" rel="#L154" style="text-decoration: none; color: rgb(12, 137, 207);"> 154</a> <a target=_blank id="L155" href="/article/1371597.html#L155" rel="#L155" style="text-decoration: none; color: rgb(12, 137, 207);"> 155</a> <a target=_blank id="L156" href="/article/1371597.html#L156" rel="#L156" style="text-decoration: none; color: rgb(12, 137, 207);"> 156</a> <a target=_blank id="L157" href="/article/1371597.html#L157" rel="#L157" style="text-decoration: none; color: rgb(12, 137, 207);"> 157</a> <a target=_blank id="L158" href="/article/1371597.html#L158" rel="#L158" style="text-decoration: none; color: rgb(12, 137, 207);"> 158</a> <a target=_blank id="L159" href="/article/1371597.html#L159" rel="#L159" style="text-decoration: none; color: rgb(12, 137, 207);"> 159</a> <a target=_blank id="L160" href="/article/1371597.html#L160" rel="#L160" style="text-decoration: none; color: rgb(12, 137, 207);"> 160</a> <a target=_blank id="L161" href="/article/1371597.html#L161" rel="#L161" style="text-decoration: none; color: rgb(12, 137, 207);"> 161</a> <a target=_blank id="L162" href="/article/1371597.html#L162" rel="#L162" style="text-decoration: none; color: rgb(12, 137, 207);"> 162</a> <a target=_blank id="L163" href="/article/1371597.html#L163" rel="#L163" style="text-decoration: none; color: rgb(12, 137, 207);"> 163</a> <a target=_blank id="L164" href="/article/1371597.html#L164" rel="#L164" style="text-decoration: none; color: rgb(12, 137, 207);"> 164</a> <a target=_blank id="L165" href="/article/1371597.html#L165" rel="#L165" style="text-decoration: none; color: rgb(12, 137, 207);"> 165</a> <a target=_blank id="L166" href="/article/1371597.html#L166" rel="#L166" style="text-decoration: none; color: rgb(12, 137, 207);"> 166</a> <a target=_blank id="L167" href="/article/1371597.html#L167" rel="#L167" style="text-decoration: none; color: rgb(12, 137, 207);"> 167</a> <a target=_blank id="L168" href="/article/1371597.html#L168" rel="#L168" style="text-decoration: none; color: rgb(12, 137, 207);"> 168</a> <a target=_blank id="L169" href="/article/1371597.html#L169" rel="#L169" style="text-decoration: none; color: rgb(12, 137, 207);"> 169</a> <a target=_blank id="L170" href="/article/1371597.html#L170" rel="#L170" style="text-decoration: none; color: rgb(12, 137, 207);"> 170</a> <a target=_blank id="L171" href="/article/1371597.html#L171" rel="#L171" style="text-decoration: none; color: rgb(12, 137, 207);"> 171</a> <a target=_blank id="L172" href="/article/1371597.html#L172" rel="#L172" style="text-decoration: none; color: rgb(12, 137, 207);"> 172</a> <a target=_blank id="L173" href="/article/1371597.html#L173" rel="#L173" style="text-decoration: none; color: rgb(12, 137, 207);"> 173</a> <a target=_blank id="L174" href="/article/1371597.html#L174" rel="#L174" style="text-decoration: none; color: rgb(12, 137, 207);"> 174</a> <a target=_blank id="L175" href="/article/1371597.html#L175" rel="#L175" style="text-decoration: none; color: rgb(12, 137, 207);"> 175</a> <a target=_blank id="L176" href="/article/1371597.html#L176" rel="#L176" style="text-decoration: none; color: rgb(12, 137, 207);"> 176</a> <a target=_blank id="L177" href="/article/1371597.html#L177" rel="#L177" style="text-decoration: none; color: rgb(12, 137, 207);"> 177</a> <a target=_blank id="L178" href="/article/1371597.html#L178" rel="#L178" style="text-decoration: none; color: rgb(12, 137, 207);"> 178</a> <a target=_blank id="L179" href="/article/1371597.html#L179" rel="#L179" style="text-decoration: none; color: rgb(12, 137, 207);"> 179</a> | #include "UpdateLayer.h"#include "HelloWorldScene.h" #if (CC_TARGET_PLATFORM != CC_PLATFORM_WIN32)#include <dirent.h>#include <sys/stat.h>#endif bool UpdateLayer::init(){ if (CCLayer::init()) { // 设置资源包下载目录 m_downloadDir = CCFileUtils::sharedFileUtils()->getWritablePath(); m_downloadDir += "download"; // 添加资源包下载路径到搜索路径,优先搜索更新的资源 std::vector<std::string> searchPaths = CCFileUtils::sharedFileUtils()->getSearchPaths(); searchPaths.insert(searchPaths.begin(), m_downloadDir); CCFileUtils::sharedFileUtils()->setSearchPaths(searchPaths); // 提示 m_label = CCLabelTTF::create("", "Arial", 18); m_label->setAnchorPoint(ccp(1,0.5)); m_label->setPosition(ccp(465,20)); addChild(m_label); // 菜单 CCMenu* menu = CCMenu::create(); menu->setPosition(CCPointZero); addChild(menu); CCSize visibleSize = CCDirector::sharedDirector()->getVisibleSize(); // 重置 CCMenuItemFont* itemReset = CCMenuItemFont::create("reset",this,menu_selector(UpdateLayer::reset)); itemReset->setPosition(ccp(visibleSize.width/2, 50)); menu->addChild(itemReset); // 获取当前版本号 CCMenuItemFont* itemGetClientVersion = CCMenuItemFont::create("getClientVersion",this,menu_selector(UpdateLayer::getClientVersion)); itemGetClientVersion->setPosition(ccp(visibleSize.width/2, 100)); menu->addChild(itemGetClientVersion); // 获取服务器最新版本 CCMenuItemFont* itemGetServerVersion = CCMenuItemFont::create("checkUpdate",this,menu_selector(UpdateLayer::checkUpdate)); itemGetServerVersion->setPosition(ccp(visibleSize.width/2, 150)); menu->addChild(itemGetServerVersion); // 更新版本 CCMenuItemFont* itemUpdateVersion = CCMenuItemFont::create("updateVersion",this,menu_selector(UpdateLayer::update)); itemUpdateVersion->setPosition(ccp(visibleSize.width/2, 200)); menu->addChild(itemUpdateVersion); // 进入场景 CCMenuItemFont* itemEnterScene = CCMenuItemFont::create("enterScene",this,menu_selector(UpdateLayer::enterScene)); itemEnterScene->setPosition(ccp(visibleSize.width/2, 250)); menu->addChild(itemEnterScene); return true; } return false;} AssetsManager* UpdateLayer::getAssetsManager(){ static AssetsManager* s_assetsManager = NULL; if (s_assetsManager ==NULL) { s_assetsManager = new AssetsManager("http://is.muni.cz/do/rect/el/ikony/sady/ikonky-128x128-png.zip", "https://coding.net/u/linchaolong/p/Test/git/raw/master/README.md", m_downloadDir.c_str()); s_assetsManager->setDelegate(this); s_assetsManager->setConnectionTimeout(3); } return s_assetsManager;} void UpdateLayer::initDownloadDir(){ // 如果下载目录不存在,则创建下载目录#if (CC_TARGET_PLATFORM != CC_PLATFORM_WIN32) DIR *pDir = NULL; pDir = opendir (m_downloadDir.c_str()); if (! pDir) { mkdir(m_downloadDir.c_str(), S_IRWXU | S_IRWXG | S_IRWXO); }#else if ((GetFileAttributesA(m_downloadDir.c_str())) == INVALID_FILE_ATTRIBUTES) { CreateDirectoryA(m_downloadDir.c_str(), 0); }#endif} void UpdateLayer::deleteDir(std::string dir){ // Remove downloaded files#if (CC_TARGET_PLATFORM != CC_PLATFORM_WIN32) std::string command = "rm -r "; // Path may include space. command += "\"" + dir + "\""; system(command.c_str());#else std::string command = "rd /s /q "; // Path may include space. command += "\"" + dir + "\""; system(command.c_str());#endif} void UpdateLayer::onError(cocos2d::extension::AssetsManager::ErrorCode errorCode){ switch (errorCode) { case cocos2d::extension::AssetsManager::kCreateFile: CCLOG("error : create file failure"); m_label->setString("error : create file failure"); break; case cocos2d::extension::AssetsManager::kNetwork: CCLOG("error : no network"); m_label->setString("error : no network"); break; case cocos2d::extension::AssetsManager::kNoNewVersion: CCLOG("error : no new version"); m_label->setString("error : no new version"); break; case cocos2d::extension::AssetsManager::kUncompress: CCLOG("error : uncompress file error"); m_label->setString("error : uncompress file error"); break; default: break; }} void UpdateLayer::onProgress(int percent){ char progress[20]; snprintf(progress, 20, "downloading %d%%", percent); CCLOG("%s",progress); m_label->setString(progress);} void UpdateLayer::onSuccess(){ CCLOG("download success."); m_label->setString("download success.");} void UpdateLayer::update(CCObject* pSender){ // 初始化下载目录 initDownloadDir(); // 下载更新包 getAssetsManager()->update();} void UpdateLayer::reset(CCObject* pSender){ if ("" != m_downloadDir) { // 删除下载目录 deleteDir(m_downloadDir); } // 删除版本号 getAssetsManager()->deleteVersion();} void UpdateLayer::getClientVersion(CCObject* pSender){ CCString* msg = CCString::createWithFormat("current client version : %s", getAssetsManager()->getVersion().c_str()); CCLOG("%s",msg->getCString()); m_label->setString(msg->getCString());} void UpdateLayer::checkUpdate(CCObject* pSender){ if (getAssetsManager()->checkUpdate()) { CCLOG("has new version"); m_label->setString("has new version"); }else{ CCLOG("has not new version"); m_label->setString("has not new version"); }} void UpdateLayer::enterScene(CCObject* pSender){ CCDirector::sharedDirector()->replaceScene(HelloWorld::scene());} |
来自CODE的代码片
UpdateLayer.cpp
<a target=_blank id="L1" href="/article/1371597.html#L1" rel="#L1" style="text-decoration: none; color: rgb(12, 137, 207);"> 1</a> <a target=_blank id="L2" href="/article/1371597.html#L2" rel="#L2" style="text-decoration: none; color: rgb(12, 137, 207);"> 2</a> <a target=_blank id="L3" href="/article/1371597.html#L3" rel="#L3" style="text-decoration: none; color: rgb(12, 137, 207);"> 3</a> <a target=_blank id="L4" href="/article/1371597.html#L4" rel="#L4" style="text-decoration: none; color: rgb(12, 137, 207);"> 4</a> <a target=_blank id="L5" href="/article/1371597.html#L5" rel="#L5" style="text-decoration: none; color: rgb(12, 137, 207);"> 5</a> <a target=_blank id="L6" href="/article/1371597.html#L6" rel="#L6" style="text-decoration: none; color: rgb(12, 137, 207);"> 6</a> <a target=_blank id="L7" href="/article/1371597.html#L7" rel="#L7" style="text-decoration: none; color: rgb(12, 137, 207);"> 7</a> <a target=_blank id="L8" href="/article/1371597.html#L8" rel="#L8" style="text-decoration: none; color: rgb(12, 137, 207);"> 8</a> <a target=_blank id="L9" href="/article/1371597.html#L9" rel="#L9" style="text-decoration: none; color: rgb(12, 137, 207);"> 9</a> <a target=_blank id="L10" href="/article/1371597.html#L10" rel="#L10" style="text-decoration: none; color: rgb(12, 137, 207);"> 10</a> <a target=_blank id="L11" href="/article/1371597.html#L11" rel="#L11" style="text-decoration: none; color: rgb(12, 137, 207);"> 11</a> <a target=_blank id="L12" href="/article/1371597.html#L12" rel="#L12" style="text-decoration: none; color: rgb(12, 137, 207);"> 12</a> <a target=_blank id="L13" href="/article/1371597.html#L13" rel="#L13" style="text-decoration: none; color: rgb(12, 137, 207);"> 13</a> <a target=_blank id="L14" href="/article/1371597.html#L14" rel="#L14" style="text-decoration: none; color: rgb(12, 137, 207);"> 14</a> <a target=_blank id="L15" href="/article/1371597.html#L15" rel="#L15" style="text-decoration: none; color: rgb(12, 137, 207);"> 15</a> <a target=_blank id="L16" href="/article/1371597.html#L16" rel="#L16" style="text-decoration: none; color: rgb(12, 137, 207);"> 16</a> <a target=_blank id="L17" href="/article/1371597.html#L17" rel="#L17" style="text-decoration: none; color: rgb(12, 137, 207);"> 17</a> <a target=_blank id="L18" href="/article/1371597.html#L18" rel="#L18" style="text-decoration: none; color: rgb(12, 137, 207);"> 18</a> <a target=_blank id="L19" href="/article/1371597.html#L19" rel="#L19" style="text-decoration: none; color: rgb(12, 137, 207);"> 19</a> <a target=_blank id="L20" href="/article/1371597.html#L20" rel="#L20" style="text-decoration: none; color: rgb(12, 137, 207);"> 20</a> <a target=_blank id="L21" href="/article/1371597.html#L21" rel="#L21" style="text-decoration: none; color: rgb(12, 137, 207);"> 21</a> <a target=_blank id="L22" href="/article/1371597.html#L22" rel="#L22" style="text-decoration: none; color: rgb(12, 137, 207);"> 22</a> <a target=_blank id="L23" href="/article/1371597.html#L23" rel="#L23" style="text-decoration: none; color: rgb(12, 137, 207);"> 23</a> <a target=_blank id="L24" href="/article/1371597.html#L24" rel="#L24" style="text-decoration: none; color: rgb(12, 137, 207);"> 24</a> <a target=_blank id="L25" href="/article/1371597.html#L25" rel="#L25" style="text-decoration: none; color: rgb(12, 137, 207);"> 25</a> <a target=_blank id="L26" href="/article/1371597.html#L26" rel="#L26" style="text-decoration: none; color: rgb(12, 137, 207);"> 26</a> <a target=_blank id="L27" href="/article/1371597.html#L27" rel="#L27" style="text-decoration: none; color: rgb(12, 137, 207);"> 27</a> <a target=_blank id="L28" href="/article/1371597.html#L28" rel="#L28" style="text-decoration: none; color: rgb(12, 137, 207);"> 28</a> <a target=_blank id="L29" href="/article/1371597.html#L29" rel="#L29" style="text-decoration: none; color: rgb(12, 137, 207);"> 29</a> <a target=_blank id="L30" href="/article/1371597.html#L30" rel="#L30" style="text-decoration: none; color: rgb(12, 137, 207);"> 30</a> <a target=_blank id="L31" href="/article/1371597.html#L31" rel="#L31" style="text-decoration: none; color: rgb(12, 137, 207);"> 31</a> <a target=_blank id="L32" href="/article/1371597.html#L32" rel="#L32" style="text-decoration: none; color: rgb(12, 137, 207);"> 32</a> <a target=_blank id="L33" href="/article/1371597.html#L33" rel="#L33" style="text-decoration: none; color: rgb(12, 137, 207);"> 33</a> <a target=_blank id="L34" href="/article/1371597.html#L34" rel="#L34" style="text-decoration: none; color: rgb(12, 137, 207);"> 34</a> <a target=_blank id="L35" href="/article/1371597.html#L35" rel="#L35" style="text-decoration: none; color: rgb(12, 137, 207);"> 35</a> <a target=_blank id="L36" href="/article/1371597.html#L36" rel="#L36" style="text-decoration: none; color: rgb(12, 137, 207);"> 36</a> <a target=_blank id="L37" href="/article/1371597.html#L37" rel="#L37" style="text-decoration: none; color: rgb(12, 137, 207);"> 37</a> <a target=_blank id="L38" href="/article/1371597.html#L38" rel="#L38" style="text-decoration: none; color: rgb(12, 137, 207);"> 38</a> <a target=_blank id="L39" href="/article/1371597.html#L39" rel="#L39" style="text-decoration: none; color: rgb(12, 137, 207);"> 39</a> <a target=_blank id="L40" href="/article/1371597.html#L40" rel="#L40" style="text-decoration: none; color: rgb(12, 137, 207);"> 40</a> <a target=_blank id="L41" href="/article/1371597.html#L41" rel="#L41" style="text-decoration: none; color: rgb(12, 137, 207);"> 41</a> <a target=_blank id="L42" href="/article/1371597.html#L42" rel="#L42" style="text-decoration: none; color: rgb(12, 137, 207);"> 42</a> <a target=_blank id="L43" href="/article/1371597.html#L43" rel="#L43" style="text-decoration: none; color: rgb(12, 137, 207);"> 43</a> <a target=_blank id="L44" href="/article/1371597.html#L44" rel="#L44" style="text-decoration: none; color: rgb(12, 137, 207);"> 44</a> <a target=_blank id="L45" href="/article/1371597.html#L45" rel="#L45" style="text-decoration: none; color: rgb(12, 137, 207);"> 45</a> <a target=_blank id="L46" href="/article/1371597.html#L46" rel="#L46" style="text-decoration: none; color: rgb(12, 137, 207);"> 46</a> <a target=_blank id="L47" href="/article/1371597.html#L47" rel="#L47" style="text-decoration: none; color: rgb(12, 137, 207);"> 47</a> <a target=_blank id="L48" href="/article/1371597.html#L48" rel="#L48" style="text-decoration: none; color: rgb(12, 137, 207);"> 48</a> <a target=_blank id="L49" href="/article/1371597.html#L49" rel="#L49" style="text-decoration: none; color: rgb(12, 137, 207);"> 49</a> <a target=_blank id="L50" href="/article/1371597.html#L50" rel="#L50" style="text-decoration: none; color: rgb(12, 137, 207);"> 50</a> <a target=_blank id="L51" href="/article/1371597.html#L51" rel="#L51" style="text-decoration: none; color: rgb(12, 137, 207);"> 51</a> <a target=_blank id="L52" href="/article/1371597.html#L52" rel="#L52" style="text-decoration: none; color: rgb(12, 137, 207);"> 52</a> <a target=_blank id="L53" href="/article/1371597.html#L53" rel="#L53" style="text-decoration: none; color: rgb(12, 137, 207);"> 53</a> <a target=_blank id="L54" href="/article/1371597.html#L54" rel="#L54" style="text-decoration: none; color: rgb(12, 137, 207);"> 54</a> | #ifndef __HOTUPDATER_H__#define __HOTUPDATER_H__ #include "cocos2d.h"USING_NS_CC;#include "cocos-ext.h"USING_NS_CC_EXT;#include "AssetsManager/AssetsManager.h" // 热更新实现示例class UpdateLayer:public CCLayer, public AssetsManagerDelegateProtocol{public: static CCScene* scene(){ CCScene* scene = CCScene::create(); scene->addChild(UpdateLayer::create()); return scene; }; static UpdateLayer* create(){ UpdateLayer* pLayer = new UpdateLayer; if (pLayer && pLayer->init()) { pLayer->autorelease(); return pLayer; } delete pLayer; return NULL; }; // 初始化 bool init(); // 下载回调函数 virtual void onError(cocos2d::extension::AssetsManager::ErrorCode errorCode); virtual void onProgress(int percent); virtual void onSuccess(); // 菜单回调函数 void reset(CCObject* pSender); // 重置版本 void getClientVersion(CCObject* pSender); // 获取当前客户端版本号 void checkUpdate(CCObject* pSender); // 检查是否有版本更新 void update(CCObject* pSender); // 更新版本 void enterScene(CCObject* pSender); // 进入场景,如果未更新屏幕中间会显示叹号的图片,更新后会显示另一张图片protected: // 初始化下载目录 void initDownloadDir(); // 删除目录 void deleteDir(std::string dir);private: CCLabelTTF* m_label; std::string m_downloadDir; AssetsManager* getAssetsManager(); }; #endif |
来自CODE的代码片
UpdateLayer.h
项目地址:https://coding.net/u/linchaolong/p/Cocos2d-x_HotUpdate/git
热更新介绍
什么是热更新?游戏客户端启动时,主动请求服务端检查版本号,并更新资源到本地。
应用场景:
情况一:游戏客户端已经发布了,但突然发现有个比较严重的bug需要修复。这时需要更新游戏的代码(Lua代码)。
情况二:情人节到了,需要搞个活动,在游戏中营造一个节日氛围。这时,需要更新游戏资源或增加一些功能。
好处:不需要重新打包和提交应用到市场等待审核。
热更新流程
AssetsManager
在Cocos2d-x中已经封装了用于实现热更新功能的类,就是AssetsManager。api说明:
// 检测是否有版本更新
virtual bool checkUpdate();
// 下载更新的资源包并解压到下载路径
virtual void update();
// 获取当前客户端版本号
std::string getVersion();
// 删除客户端版本号
void deleteVersion();
// 设置下载回调(AssetsManagerDelegateProtocol)
void setDelegate(AssetsManagerDelegateProtocol *delegate);
// 设置连接超时时间(单位:秒)
void setConnectionTimeout(unsigned int timeout);
// 设置从服务端下载资源包的url
void setPackageUrl(const char* packageUrl);
// 设置服务端获取版本号的url
void setVersionFileUrl(const char* versionFileUrl);
// 设置资源保存路径
void setStoragePath(const char* storagePath);
因为AssetsManager使用了pthread库,所以需要在win32工程中需要包含pthread库所在目录。
VS在工程属性——C/C++——常规——附加包含目录中添加:$(ProjectDir)..\..\..\cocos2dx\platform\third_party\win32\pthread
示例工程
该工程使用Cocos2d-x2.1.6和VS2012。【点击下载源码】把工程放到引擎projects目录下即可。
<a target=_blank id="L1" href="/article/1371597.html#L1" rel="#L1" style="text-decoration: none; color: rgb(12, 137, 207);"> 1</a> <a target=_blank id="L2" href="/article/1371597.html#L2" rel="#L2" style="text-decoration: none; color: rgb(12, 137, 207);"> 2</a> <a target=_blank id="L3" href="/article/1371597.html#L3" rel="#L3" style="text-decoration: none; color: rgb(12, 137, 207);"> 3</a> <a target=_blank id="L4" href="/article/1371597.html#L4" rel="#L4" style="text-decoration: none; color: rgb(12, 137, 207);"> 4</a> <a target=_blank id="L5" href="/article/1371597.html#L5" rel="#L5" style="text-decoration: none; color: rgb(12, 137, 207);"> 5</a> <a target=_blank id="L6" href="/article/1371597.html#L6" rel="#L6" style="text-decoration: none; color: rgb(12, 137, 207);"> 6</a> <a target=_blank id="L7" href="/article/1371597.html#L7" rel="#L7" style="text-decoration: none; color: rgb(12, 137, 207);"> 7</a> <a target=_blank id="L8" href="/article/1371597.html#L8" rel="#L8" style="text-decoration: none; color: rgb(12, 137, 207);"> 8</a> <a target=_blank id="L9" href="/article/1371597.html#L9" rel="#L9" style="text-decoration: none; color: rgb(12, 137, 207);"> 9</a> <a target=_blank id="L10" href="/article/1371597.html#L10" rel="#L10" style="text-decoration: none; color: rgb(12, 137, 207);"> 10</a> <a target=_blank id="L11" href="/article/1371597.html#L11" rel="#L11" style="text-decoration: none; color: rgb(12, 137, 207);"> 11</a> <a target=_blank id="L12" href="/article/1371597.html#L12" rel="#L12" style="text-decoration: none; color: rgb(12, 137, 207);"> 12</a> <a target=_blank id="L13" href="/article/1371597.html#L13" rel="#L13" style="text-decoration: none; color: rgb(12, 137, 207);"> 13</a> <a target=_blank id="L14" href="/article/1371597.html#L14" rel="#L14" style="text-decoration: none; color: rgb(12, 137, 207);"> 14</a> <a target=_blank id="L15" href="/article/1371597.html#L15" rel="#L15" style="text-decoration: none; color: rgb(12, 137, 207);"> 15</a> <a target=_blank id="L16" href="/article/1371597.html#L16" rel="#L16" style="text-decoration: none; color: rgb(12, 137, 207);"> 16</a> <a target=_blank id="L17" href="/article/1371597.html#L17" rel="#L17" style="text-decoration: none; color: rgb(12, 137, 207);"> 17</a> <a target=_blank id="L18" href="/article/1371597.html#L18" rel="#L18" style="text-decoration: none; color: rgb(12, 137, 207);"> 18</a> <a target=_blank id="L19" href="/article/1371597.html#L19" rel="#L19" style="text-decoration: none; color: rgb(12, 137, 207);"> 19</a> <a target=_blank id="L20" href="/article/1371597.html#L20" rel="#L20" style="text-decoration: none; color: rgb(12, 137, 207);"> 20</a> <a target=_blank id="L21" href="/article/1371597.html#L21" rel="#L21" style="text-decoration: none; color: rgb(12, 137, 207);"> 21</a> <a target=_blank id="L22" href="/article/1371597.html#L22" rel="#L22" style="text-decoration: none; color: rgb(12, 137, 207);"> 22</a> <a target=_blank id="L23" href="/article/1371597.html#L23" rel="#L23" style="text-decoration: none; color: rgb(12, 137, 207);"> 23</a> <a target=_blank id="L24" href="/article/1371597.html#L24" rel="#L24" style="text-decoration: none; color: rgb(12, 137, 207);"> 24</a> <a target=_blank id="L25" href="/article/1371597.html#L25" rel="#L25" style="text-decoration: none; color: rgb(12, 137, 207);"> 25</a> <a target=_blank id="L26" href="/article/1371597.html#L26" rel="#L26" style="text-decoration: none; color: rgb(12, 137, 207);"> 26</a> <a target=_blank id="L27" href="/article/1371597.html#L27" rel="#L27" style="text-decoration: none; color: rgb(12, 137, 207);"> 27</a> <a target=_blank id="L28" href="/article/1371597.html#L28" rel="#L28" style="text-decoration: none; color: rgb(12, 137, 207);"> 28</a> <a target=_blank id="L29" href="/article/1371597.html#L29" rel="#L29" style="text-decoration: none; color: rgb(12, 137, 207);"> 29</a> <a target=_blank id="L30" href="/article/1371597.html#L30" rel="#L30" style="text-decoration: none; color: rgb(12, 137, 207);"> 30</a> <a target=_blank id="L31" href="/article/1371597.html#L31" rel="#L31" style="text-decoration: none; color: rgb(12, 137, 207);"> 31</a> <a target=_blank id="L32" href="/article/1371597.html#L32" rel="#L32" style="text-decoration: none; color: rgb(12, 137, 207);"> 32</a> <a target=_blank id="L33" href="/article/1371597.html#L33" rel="#L33" style="text-decoration: none; color: rgb(12, 137, 207);"> 33</a> <a target=_blank id="L34" href="/article/1371597.html#L34" rel="#L34" style="text-decoration: none; color: rgb(12, 137, 207);"> 34</a> <a target=_blank id="L35" href="/article/1371597.html#L35" rel="#L35" style="text-decoration: none; color: rgb(12, 137, 207);"> 35</a> <a target=_blank id="L36" href="/article/1371597.html#L36" rel="#L36" style="text-decoration: none; color: rgb(12, 137, 207);"> 36</a> <a target=_blank id="L37" href="/article/1371597.html#L37" rel="#L37" style="text-decoration: none; color: rgb(12, 137, 207);"> 37</a> <a target=_blank id="L38" href="/article/1371597.html#L38" rel="#L38" style="text-decoration: none; color: rgb(12, 137, 207);"> 38</a> <a target=_blank id="L39" href="/article/1371597.html#L39" rel="#L39" style="text-decoration: none; color: rgb(12, 137, 207);"> 39</a> <a target=_blank id="L40" href="/article/1371597.html#L40" rel="#L40" style="text-decoration: none; color: rgb(12, 137, 207);"> 40</a> <a target=_blank id="L41" href="/article/1371597.html#L41" rel="#L41" style="text-decoration: none; color: rgb(12, 137, 207);"> 41</a> <a target=_blank id="L42" href="/article/1371597.html#L42" rel="#L42" style="text-decoration: none; color: rgb(12, 137, 207);"> 42</a> <a target=_blank id="L43" href="/article/1371597.html#L43" rel="#L43" style="text-decoration: none; color: rgb(12, 137, 207);"> 43</a> <a target=_blank id="L44" href="/article/1371597.html#L44" rel="#L44" style="text-decoration: none; color: rgb(12, 137, 207);"> 44</a> <a target=_blank id="L45" href="/article/1371597.html#L45" rel="#L45" style="text-decoration: none; color: rgb(12, 137, 207);"> 45</a> <a target=_blank id="L46" href="/article/1371597.html#L46" rel="#L46" style="text-decoration: none; color: rgb(12, 137, 207);"> 46</a> <a target=_blank id="L47" href="/article/1371597.html#L47" rel="#L47" style="text-decoration: none; color: rgb(12, 137, 207);"> 47</a> <a target=_blank id="L48" href="/article/1371597.html#L48" rel="#L48" style="text-decoration: none; color: rgb(12, 137, 207);"> 48</a> <a target=_blank id="L49" href="/article/1371597.html#L49" rel="#L49" style="text-decoration: none; color: rgb(12, 137, 207);"> 49</a> <a target=_blank id="L50" href="/article/1371597.html#L50" rel="#L50" style="text-decoration: none; color: rgb(12, 137, 207);"> 50</a> <a target=_blank id="L51" href="/article/1371597.html#L51" rel="#L51" style="text-decoration: none; color: rgb(12, 137, 207);"> 51</a> <a target=_blank id="L52" href="/article/1371597.html#L52" rel="#L52" style="text-decoration: none; color: rgb(12, 137, 207);"> 52</a> <a target=_blank id="L53" href="/article/1371597.html#L53" rel="#L53" style="text-decoration: none; color: rgb(12, 137, 207);"> 53</a> <a target=_blank id="L54" href="/article/1371597.html#L54" rel="#L54" style="text-decoration: none; color: rgb(12, 137, 207);"> 54</a> <a target=_blank id="L55" href="/article/1371597.html#L55" rel="#L55" style="text-decoration: none; color: rgb(12, 137, 207);"> 55</a> <a target=_blank id="L56" href="/article/1371597.html#L56" rel="#L56" style="text-decoration: none; color: rgb(12, 137, 207);"> 56</a> <a target=_blank id="L57" href="/article/1371597.html#L57" rel="#L57" style="text-decoration: none; color: rgb(12, 137, 207);"> 57</a> <a target=_blank id="L58" href="/article/1371597.html#L58" rel="#L58" style="text-decoration: none; color: rgb(12, 137, 207);"> 58</a> <a target=_blank id="L59" href="/article/1371597.html#L59" rel="#L59" style="text-decoration: none; color: rgb(12, 137, 207);"> 59</a> <a target=_blank id="L60" href="/article/1371597.html#L60" rel="#L60" style="text-decoration: none; color: rgb(12, 137, 207);"> 60</a> <a target=_blank id="L61" href="/article/1371597.html#L61" rel="#L61" style="text-decoration: none; color: rgb(12, 137, 207);"> 61</a> <a target=_blank id="L62" href="/article/1371597.html#L62" rel="#L62" style="text-decoration: none; color: rgb(12, 137, 207);"> 62</a> <a target=_blank id="L63" href="/article/1371597.html#L63" rel="#L63" style="text-decoration: none; color: rgb(12, 137, 207);"> 63</a> <a target=_blank id="L64" href="/article/1371597.html#L64" rel="#L64" style="text-decoration: none; color: rgb(12, 137, 207);"> 64</a> <a target=_blank id="L65" href="/article/1371597.html#L65" rel="#L65" style="text-decoration: none; color: rgb(12, 137, 207);"> 65</a> <a target=_blank id="L66" href="/article/1371597.html#L66" rel="#L66" style="text-decoration: none; color: rgb(12, 137, 207);"> 66</a> <a target=_blank id="L67" href="/article/1371597.html#L67" rel="#L67" style="text-decoration: none; color: rgb(12, 137, 207);"> 67</a> <a target=_blank id="L68" href="/article/1371597.html#L68" rel="#L68" style="text-decoration: none; color: rgb(12, 137, 207);"> 68</a> <a target=_blank id="L69" href="/article/1371597.html#L69" rel="#L69" style="text-decoration: none; color: rgb(12, 137, 207);"> 69</a> <a target=_blank id="L70" href="/article/1371597.html#L70" rel="#L70" style="text-decoration: none; color: rgb(12, 137, 207);"> 70</a> <a target=_blank id="L71" href="/article/1371597.html#L71" rel="#L71" style="text-decoration: none; color: rgb(12, 137, 207);"> 71</a> <a target=_blank id="L72" href="/article/1371597.html#L72" rel="#L72" style="text-decoration: none; color: rgb(12, 137, 207);"> 72</a> <a target=_blank id="L73" href="/article/1371597.html#L73" rel="#L73" style="text-decoration: none; color: rgb(12, 137, 207);"> 73</a> <a target=_blank id="L74" href="/article/1371597.html#L74" rel="#L74" style="text-decoration: none; color: rgb(12, 137, 207);"> 74</a> <a target=_blank id="L75" href="/article/1371597.html#L75" rel="#L75" style="text-decoration: none; color: rgb(12, 137, 207);"> 75</a> <a target=_blank id="L76" href="/article/1371597.html#L76" rel="#L76" style="text-decoration: none; color: rgb(12, 137, 207);"> 76</a> <a target=_blank id="L77" href="/article/1371597.html#L77" rel="#L77" style="text-decoration: none; color: rgb(12, 137, 207);"> 77</a> <a target=_blank id="L78" href="/article/1371597.html#L78" rel="#L78" style="text-decoration: none; color: rgb(12, 137, 207);"> 78</a> <a target=_blank id="L79" href="/article/1371597.html#L79" rel="#L79" style="text-decoration: none; color: rgb(12, 137, 207);"> 79</a> <a target=_blank id="L80" href="/article/1371597.html#L80" rel="#L80" style="text-decoration: none; color: rgb(12, 137, 207);"> 80</a> <a target=_blank id="L81" href="/article/1371597.html#L81" rel="#L81" style="text-decoration: none; color: rgb(12, 137, 207);"> 81</a> <a target=_blank id="L82" href="/article/1371597.html#L82" rel="#L82" style="text-decoration: none; color: rgb(12, 137, 207);"> 82</a> <a target=_blank id="L83" href="/article/1371597.html#L83" rel="#L83" style="text-decoration: none; color: rgb(12, 137, 207);"> 83</a> <a target=_blank id="L84" href="/article/1371597.html#L84" rel="#L84" style="text-decoration: none; color: rgb(12, 137, 207);"> 84</a> <a target=_blank id="L85" href="/article/1371597.html#L85" rel="#L85" style="text-decoration: none; color: rgb(12, 137, 207);"> 85</a> <a target=_blank id="L86" href="/article/1371597.html#L86" rel="#L86" style="text-decoration: none; color: rgb(12, 137, 207);"> 86</a> <a target=_blank id="L87" href="/article/1371597.html#L87" rel="#L87" style="text-decoration: none; color: rgb(12, 137, 207);"> 87</a> <a target=_blank id="L88" href="/article/1371597.html#L88" rel="#L88" style="text-decoration: none; color: rgb(12, 137, 207);"> 88</a> <a target=_blank id="L89" href="/article/1371597.html#L89" rel="#L89" style="text-decoration: none; color: rgb(12, 137, 207);"> 89</a> <a target=_blank id="L90" href="/article/1371597.html#L90" rel="#L90" style="text-decoration: none; color: rgb(12, 137, 207);"> 90</a> <a target=_blank id="L91" href="/article/1371597.html#L91" rel="#L91" style="text-decoration: none; color: rgb(12, 137, 207);"> 91</a> <a target=_blank id="L92" href="/article/1371597.html#L92" rel="#L92" style="text-decoration: none; color: rgb(12, 137, 207);"> 92</a> <a target=_blank id="L93" href="/article/1371597.html#L93" rel="#L93" style="text-decoration: none; color: rgb(12, 137, 207);"> 93</a> <a target=_blank id="L94" href="/article/1371597.html#L94" rel="#L94" style="text-decoration: none; color: rgb(12, 137, 207);"> 94</a> <a target=_blank id="L95" href="/article/1371597.html#L95" rel="#L95" style="text-decoration: none; color: rgb(12, 137, 207);"> 95</a> <a target=_blank id="L96" href="/article/1371597.html#L96" rel="#L96" style="text-decoration: none; color: rgb(12, 137, 207);"> 96</a> <a target=_blank id="L97" href="/article/1371597.html#L97" rel="#L97" style="text-decoration: none; color: rgb(12, 137, 207);"> 97</a> <a target=_blank id="L98" href="/article/1371597.html#L98" rel="#L98" style="text-decoration: none; color: rgb(12, 137, 207);"> 98</a> <a target=_blank id="L99" href="/article/1371597.html#L99" rel="#L99" style="text-decoration: none; color: rgb(12, 137, 207);"> 99</a> <a target=_blank id="L100" href="/article/1371597.html#L100" rel="#L100" style="text-decoration: none; color: rgb(12, 137, 207);"> 100</a> <a target=_blank id="L101" href="/article/1371597.html#L101" rel="#L101" style="text-decoration: none; color: rgb(12, 137, 207);"> 101</a> <a target=_blank id="L102" href="/article/1371597.html#L102" rel="#L102" style="text-decoration: none; color: rgb(12, 137, 207);"> 102</a> <a target=_blank id="L103" href="/article/1371597.html#L103" rel="#L103" style="text-decoration: none; color: rgb(12, 137, 207);"> 103</a> <a target=_blank id="L104" href="/article/1371597.html#L104" rel="#L104" style="text-decoration: none; color: rgb(12, 137, 207);"> 104</a> <a target=_blank id="L105" href="/article/1371597.html#L105" rel="#L105" style="text-decoration: none; color: rgb(12, 137, 207);"> 105</a> <a target=_blank id="L106" href="/article/1371597.html#L106" rel="#L106" style="text-decoration: none; color: rgb(12, 137, 207);"> 106</a> <a target=_blank id="L107" href="/article/1371597.html#L107" rel="#L107" style="text-decoration: none; color: rgb(12, 137, 207);"> 107</a> <a target=_blank id="L108" href="/article/1371597.html#L108" rel="#L108" style="text-decoration: none; color: rgb(12, 137, 207);"> 108</a> <a target=_blank id="L109" href="/article/1371597.html#L109" rel="#L109" style="text-decoration: none; color: rgb(12, 137, 207);"> 109</a> <a target=_blank id="L110" href="/article/1371597.html#L110" rel="#L110" style="text-decoration: none; color: rgb(12, 137, 207);"> 110</a> <a target=_blank id="L111" href="/article/1371597.html#L111" rel="#L111" style="text-decoration: none; color: rgb(12, 137, 207);"> 111</a> <a target=_blank id="L112" href="/article/1371597.html#L112" rel="#L112" style="text-decoration: none; color: rgb(12, 137, 207);"> 112</a> <a target=_blank id="L113" href="/article/1371597.html#L113" rel="#L113" style="text-decoration: none; color: rgb(12, 137, 207);"> 113</a> <a target=_blank id="L114" href="/article/1371597.html#L114" rel="#L114" style="text-decoration: none; color: rgb(12, 137, 207);"> 114</a> <a target=_blank id="L115" href="/article/1371597.html#L115" rel="#L115" style="text-decoration: none; color: rgb(12, 137, 207);"> 115</a> <a target=_blank id="L116" href="/article/1371597.html#L116" rel="#L116" style="text-decoration: none; color: rgb(12, 137, 207);"> 116</a> <a target=_blank id="L117" href="/article/1371597.html#L117" rel="#L117" style="text-decoration: none; color: rgb(12, 137, 207);"> 117</a> <a target=_blank id="L118" href="/article/1371597.html#L118" rel="#L118" style="text-decoration: none; color: rgb(12, 137, 207);"> 118</a> <a target=_blank id="L119" href="/article/1371597.html#L119" rel="#L119" style="text-decoration: none; color: rgb(12, 137, 207);"> 119</a> <a target=_blank id="L120" href="/article/1371597.html#L120" rel="#L120" style="text-decoration: none; color: rgb(12, 137, 207);"> 120</a> <a target=_blank id="L121" href="/article/1371597.html#L121" rel="#L121" style="text-decoration: none; color: rgb(12, 137, 207);"> 121</a> <a target=_blank id="L122" href="/article/1371597.html#L122" rel="#L122" style="text-decoration: none; color: rgb(12, 137, 207);"> 122</a> <a target=_blank id="L123" href="/article/1371597.html#L123" rel="#L123" style="text-decoration: none; color: rgb(12, 137, 207);"> 123</a> <a target=_blank id="L124" href="/article/1371597.html#L124" rel="#L124" style="text-decoration: none; color: rgb(12, 137, 207);"> 124</a> <a target=_blank id="L125" href="/article/1371597.html#L125" rel="#L125" style="text-decoration: none; color: rgb(12, 137, 207);"> 125</a> <a target=_blank id="L126" href="/article/1371597.html#L126" rel="#L126" style="text-decoration: none; color: rgb(12, 137, 207);"> 126</a> <a target=_blank id="L127" href="/article/1371597.html#L127" rel="#L127" style="text-decoration: none; color: rgb(12, 137, 207);"> 127</a> <a target=_blank id="L128" href="/article/1371597.html#L128" rel="#L128" style="text-decoration: none; color: rgb(12, 137, 207);"> 128</a> <a target=_blank id="L129" href="/article/1371597.html#L129" rel="#L129" style="text-decoration: none; color: rgb(12, 137, 207);"> 129</a> <a target=_blank id="L130" href="/article/1371597.html#L130" rel="#L130" style="text-decoration: none; color: rgb(12, 137, 207);"> 130</a> <a target=_blank id="L131" href="/article/1371597.html#L131" rel="#L131" style="text-decoration: none; color: rgb(12, 137, 207);"> 131</a> <a target=_blank id="L132" href="/article/1371597.html#L132" rel="#L132" style="text-decoration: none; color: rgb(12, 137, 207);"> 132</a> <a target=_blank id="L133" href="/article/1371597.html#L133" rel="#L133" style="text-decoration: none; color: rgb(12, 137, 207);"> 133</a> <a target=_blank id="L134" href="/article/1371597.html#L134" rel="#L134" style="text-decoration: none; color: rgb(12, 137, 207);"> 134</a> <a target=_blank id="L135" href="/article/1371597.html#L135" rel="#L135" style="text-decoration: none; color: rgb(12, 137, 207);"> 135</a> <a target=_blank id="L136" href="/article/1371597.html#L136" rel="#L136" style="text-decoration: none; color: rgb(12, 137, 207);"> 136</a> <a target=_blank id="L137" href="/article/1371597.html#L137" rel="#L137" style="text-decoration: none; color: rgb(12, 137, 207);"> 137</a> <a target=_blank id="L138" href="/article/1371597.html#L138" rel="#L138" style="text-decoration: none; color: rgb(12, 137, 207);"> 138</a> <a target=_blank id="L139" href="/article/1371597.html#L139" rel="#L139" style="text-decoration: none; color: rgb(12, 137, 207);"> 139</a> <a target=_blank id="L140" href="/article/1371597.html#L140" rel="#L140" style="text-decoration: none; color: rgb(12, 137, 207);"> 140</a> <a target=_blank id="L141" href="/article/1371597.html#L141" rel="#L141" style="text-decoration: none; color: rgb(12, 137, 207);"> 141</a> <a target=_blank id="L142" href="/article/1371597.html#L142" rel="#L142" style="text-decoration: none; color: rgb(12, 137, 207);"> 142</a> <a target=_blank id="L143" href="/article/1371597.html#L143" rel="#L143" style="text-decoration: none; color: rgb(12, 137, 207);"> 143</a> <a target=_blank id="L144" href="/article/1371597.html#L144" rel="#L144" style="text-decoration: none; color: rgb(12, 137, 207);"> 144</a> <a target=_blank id="L145" href="/article/1371597.html#L145" rel="#L145" style="text-decoration: none; color: rgb(12, 137, 207);"> 145</a> <a target=_blank id="L146" href="/article/1371597.html#L146" rel="#L146" style="text-decoration: none; color: rgb(12, 137, 207);"> 146</a> <a target=_blank id="L147" href="/article/1371597.html#L147" rel="#L147" style="text-decoration: none; color: rgb(12, 137, 207);"> 147</a> <a target=_blank id="L148" href="/article/1371597.html#L148" rel="#L148" style="text-decoration: none; color: rgb(12, 137, 207);"> 148</a> <a target=_blank id="L149" href="/article/1371597.html#L149" rel="#L149" style="text-decoration: none; color: rgb(12, 137, 207);"> 149</a> <a target=_blank id="L150" href="/article/1371597.html#L150" rel="#L150" style="text-decoration: none; color: rgb(12, 137, 207);"> 150</a> <a target=_blank id="L151" href="/article/1371597.html#L151" rel="#L151" style="text-decoration: none; color: rgb(12, 137, 207);"> 151</a> <a target=_blank id="L152" href="/article/1371597.html#L152" rel="#L152" style="text-decoration: none; color: rgb(12, 137, 207);"> 152</a> <a target=_blank id="L153" href="/article/1371597.html#L153" rel="#L153" style="text-decoration: none; color: rgb(12, 137, 207);"> 153</a> <a target=_blank id="L154" href="/article/1371597.html#L154" rel="#L154" style="text-decoration: none; color: rgb(12, 137, 207);"> 154</a> <a target=_blank id="L155" href="/article/1371597.html#L155" rel="#L155" style="text-decoration: none; color: rgb(12, 137, 207);"> 155</a> <a target=_blank id="L156" href="/article/1371597.html#L156" rel="#L156" style="text-decoration: none; color: rgb(12, 137, 207);"> 156</a> <a target=_blank id="L157" href="/article/1371597.html#L157" rel="#L157" style="text-decoration: none; color: rgb(12, 137, 207);"> 157</a> <a target=_blank id="L158" href="/article/1371597.html#L158" rel="#L158" style="text-decoration: none; color: rgb(12, 137, 207);"> 158</a> <a target=_blank id="L159" href="/article/1371597.html#L159" rel="#L159" style="text-decoration: none; color: rgb(12, 137, 207);"> 159</a> <a target=_blank id="L160" href="/article/1371597.html#L160" rel="#L160" style="text-decoration: none; color: rgb(12, 137, 207);"> 160</a> <a target=_blank id="L161" href="/article/1371597.html#L161" rel="#L161" style="text-decoration: none; color: rgb(12, 137, 207);"> 161</a> <a target=_blank id="L162" href="/article/1371597.html#L162" rel="#L162" style="text-decoration: none; color: rgb(12, 137, 207);"> 162</a> <a target=_blank id="L163" href="/article/1371597.html#L163" rel="#L163" style="text-decoration: none; color: rgb(12, 137, 207);"> 163</a> <a target=_blank id="L164" href="/article/1371597.html#L164" rel="#L164" style="text-decoration: none; color: rgb(12, 137, 207);"> 164</a> <a target=_blank id="L165" href="/article/1371597.html#L165" rel="#L165" style="text-decoration: none; color: rgb(12, 137, 207);"> 165</a> <a target=_blank id="L166" href="/article/1371597.html#L166" rel="#L166" style="text-decoration: none; color: rgb(12, 137, 207);"> 166</a> <a target=_blank id="L167" href="/article/1371597.html#L167" rel="#L167" style="text-decoration: none; color: rgb(12, 137, 207);"> 167</a> <a target=_blank id="L168" href="/article/1371597.html#L168" rel="#L168" style="text-decoration: none; color: rgb(12, 137, 207);"> 168</a> <a target=_blank id="L169" href="/article/1371597.html#L169" rel="#L169" style="text-decoration: none; color: rgb(12, 137, 207);"> 169</a> <a target=_blank id="L170" href="/article/1371597.html#L170" rel="#L170" style="text-decoration: none; color: rgb(12, 137, 207);"> 170</a> <a target=_blank id="L171" href="/article/1371597.html#L171" rel="#L171" style="text-decoration: none; color: rgb(12, 137, 207);"> 171</a> <a target=_blank id="L172" href="/article/1371597.html#L172" rel="#L172" style="text-decoration: none; color: rgb(12, 137, 207);"> 172</a> <a target=_blank id="L173" href="/article/1371597.html#L173" rel="#L173" style="text-decoration: none; color: rgb(12, 137, 207);"> 173</a> <a target=_blank id="L174" href="/article/1371597.html#L174" rel="#L174" style="text-decoration: none; color: rgb(12, 137, 207);"> 174</a> <a target=_blank id="L175" href="/article/1371597.html#L175" rel="#L175" style="text-decoration: none; color: rgb(12, 137, 207);"> 175</a> <a target=_blank id="L176" href="/article/1371597.html#L176" rel="#L176" style="text-decoration: none; color: rgb(12, 137, 207);"> 176</a> <a target=_blank id="L177" href="/article/1371597.html#L177" rel="#L177" style="text-decoration: none; color: rgb(12, 137, 207);"> 177</a> <a target=_blank id="L178" href="/article/1371597.html#L178" rel="#L178" style="text-decoration: none; color: rgb(12, 137, 207);"> 178</a> <a target=_blank id="L179" href="/article/1371597.html#L179" rel="#L179" style="text-decoration: none; color: rgb(12, 137, 207);"> 179</a> | #include "UpdateLayer.h"#include "HelloWorldScene.h" #if (CC_TARGET_PLATFORM != CC_PLATFORM_WIN32)#include <dirent.h>#include <sys/stat.h>#endif bool UpdateLayer::init(){ if (CCLayer::init()) { // 设置资源包下载目录 m_downloadDir = CCFileUtils::sharedFileUtils()->getWritablePath(); m_downloadDir += "download"; // 添加资源包下载路径到搜索路径,优先搜索更新的资源 std::vector<std::string> searchPaths = CCFileUtils::sharedFileUtils()->getSearchPaths(); searchPaths.insert(searchPaths.begin(), m_downloadDir); CCFileUtils::sharedFileUtils()->setSearchPaths(searchPaths); // 提示 m_label = CCLabelTTF::create("", "Arial", 18); m_label->setAnchorPoint(ccp(1,0.5)); m_label->setPosition(ccp(465,20)); addChild(m_label); // 菜单 CCMenu* menu = CCMenu::create(); menu->setPosition(CCPointZero); addChild(menu); CCSize visibleSize = CCDirector::sharedDirector()->getVisibleSize(); // 重置 CCMenuItemFont* itemReset = CCMenuItemFont::create("reset",this,menu_selector(UpdateLayer::reset)); itemReset->setPosition(ccp(visibleSize.width/2, 50)); menu->addChild(itemReset); // 获取当前版本号 CCMenuItemFont* itemGetClientVersion = CCMenuItemFont::create("getClientVersion",this,menu_selector(UpdateLayer::getClientVersion)); itemGetClientVersion->setPosition(ccp(visibleSize.width/2, 100)); menu->addChild(itemGetClientVersion); // 获取服务器最新版本 CCMenuItemFont* itemGetServerVersion = CCMenuItemFont::create("checkUpdate",this,menu_selector(UpdateLayer::checkUpdate)); itemGetServerVersion->setPosition(ccp(visibleSize.width/2, 150)); menu->addChild(itemGetServerVersion); // 更新版本 CCMenuItemFont* itemUpdateVersion = CCMenuItemFont::create("updateVersion",this,menu_selector(UpdateLayer::update)); itemUpdateVersion->setPosition(ccp(visibleSize.width/2, 200)); menu->addChild(itemUpdateVersion); // 进入场景 CCMenuItemFont* itemEnterScene = CCMenuItemFont::create("enterScene",this,menu_selector(UpdateLayer::enterScene)); itemEnterScene->setPosition(ccp(visibleSize.width/2, 250)); menu->addChild(itemEnterScene); return true; } return false;} AssetsManager* UpdateLayer::getAssetsManager(){ static AssetsManager* s_assetsManager = NULL; if (s_assetsManager ==NULL) { s_assetsManager = new AssetsManager("http://is.muni.cz/do/rect/el/ikony/sady/ikonky-128x128-png.zip", "https://coding.net/u/linchaolong/p/Test/git/raw/master/README.md", m_downloadDir.c_str()); s_assetsManager->setDelegate(this); s_assetsManager->setConnectionTimeout(3); } return s_assetsManager;} void UpdateLayer::initDownloadDir(){ // 如果下载目录不存在,则创建下载目录#if (CC_TARGET_PLATFORM != CC_PLATFORM_WIN32) DIR *pDir = NULL; pDir = opendir (m_downloadDir.c_str()); if (! pDir) { mkdir(m_downloadDir.c_str(), S_IRWXU | S_IRWXG | S_IRWXO); }#else if ((GetFileAttributesA(m_downloadDir.c_str())) == INVALID_FILE_ATTRIBUTES) { CreateDirectoryA(m_downloadDir.c_str(), 0); }#endif} void UpdateLayer::deleteDir(std::string dir){ // Remove downloaded files#if (CC_TARGET_PLATFORM != CC_PLATFORM_WIN32) std::string command = "rm -r "; // Path may include space. command += "\"" + dir + "\""; system(command.c_str());#else std::string command = "rd /s /q "; // Path may include space. command += "\"" + dir + "\""; system(command.c_str());#endif} void UpdateLayer::onError(cocos2d::extension::AssetsManager::ErrorCode errorCode){ switch (errorCode) { case cocos2d::extension::AssetsManager::kCreateFile: CCLOG("error : create file failure"); m_label->setString("error : create file failure"); break; case cocos2d::extension::AssetsManager::kNetwork: CCLOG("error : no network"); m_label->setString("error : no network"); break; case cocos2d::extension::AssetsManager::kNoNewVersion: CCLOG("error : no new version"); m_label->setString("error : no new version"); break; case cocos2d::extension::AssetsManager::kUncompress: CCLOG("error : uncompress file error"); m_label->setString("error : uncompress file error"); break; default: break; }} void UpdateLayer::onProgress(int percent){ char progress[20]; snprintf(progress, 20, "downloading %d%%", percent); CCLOG("%s",progress); m_label->setString(progress);} void UpdateLayer::onSuccess(){ CCLOG("download success."); m_label->setString("download success.");} void UpdateLayer::update(CCObject* pSender){ // 初始化下载目录 initDownloadDir(); // 下载更新包 getAssetsManager()->update();} void UpdateLayer::reset(CCObject* pSender){ if ("" != m_downloadDir) { // 删除下载目录 deleteDir(m_downloadDir); } // 删除版本号 getAssetsManager()->deleteVersion();} void UpdateLayer::getClientVersion(CCObject* pSender){ CCString* msg = CCString::createWithFormat("current client version : %s", getAssetsManager()->getVersion().c_str()); CCLOG("%s",msg->getCString()); m_label->setString(msg->getCString());} void UpdateLayer::checkUpdate(CCObject* pSender){ if (getAssetsManager()->checkUpdate()) { CCLOG("has new version"); m_label->setString("has new version"); }else{ CCLOG("has not new version"); m_label->setString("has not new version"); }} void UpdateLayer::enterScene(CCObject* pSender){ CCDirector::sharedDirector()->replaceScene(HelloWorld::scene());} |
来自CODE的代码片
UpdateLayer.cpp
<a target=_blank id="L1" href="/article/1371597.html#L1" rel="#L1" style="text-decoration: none; color: rgb(12, 137, 207);"> 1</a> <a target=_blank id="L2" href="/article/1371597.html#L2" rel="#L2" style="text-decoration: none; color: rgb(12, 137, 207);"> 2</a> <a target=_blank id="L3" href="/article/1371597.html#L3" rel="#L3" style="text-decoration: none; color: rgb(12, 137, 207);"> 3</a> <a target=_blank id="L4" href="/article/1371597.html#L4" rel="#L4" style="text-decoration: none; color: rgb(12, 137, 207);"> 4</a> <a target=_blank id="L5" href="/article/1371597.html#L5" rel="#L5" style="text-decoration: none; color: rgb(12, 137, 207);"> 5</a> <a target=_blank id="L6" href="/article/1371597.html#L6" rel="#L6" style="text-decoration: none; color: rgb(12, 137, 207);"> 6</a> <a target=_blank id="L7" href="/article/1371597.html#L7" rel="#L7" style="text-decoration: none; color: rgb(12, 137, 207);"> 7</a> <a target=_blank id="L8" href="/article/1371597.html#L8" rel="#L8" style="text-decoration: none; color: rgb(12, 137, 207);"> 8</a> <a target=_blank id="L9" href="/article/1371597.html#L9" rel="#L9" style="text-decoration: none; color: rgb(12, 137, 207);"> 9</a> <a target=_blank id="L10" href="/article/1371597.html#L10" rel="#L10" style="text-decoration: none; color: rgb(12, 137, 207);"> 10</a> <a target=_blank id="L11" href="/article/1371597.html#L11" rel="#L11" style="text-decoration: none; color: rgb(12, 137, 207);"> 11</a> <a target=_blank id="L12" href="/article/1371597.html#L12" rel="#L12" style="text-decoration: none; color: rgb(12, 137, 207);"> 12</a> <a target=_blank id="L13" href="/article/1371597.html#L13" rel="#L13" style="text-decoration: none; color: rgb(12, 137, 207);"> 13</a> <a target=_blank id="L14" href="/article/1371597.html#L14" rel="#L14" style="text-decoration: none; color: rgb(12, 137, 207);"> 14</a> <a target=_blank id="L15" href="/article/1371597.html#L15" rel="#L15" style="text-decoration: none; color: rgb(12, 137, 207);"> 15</a> <a target=_blank id="L16" href="/article/1371597.html#L16" rel="#L16" style="text-decoration: none; color: rgb(12, 137, 207);"> 16</a> <a target=_blank id="L17" href="/article/1371597.html#L17" rel="#L17" style="text-decoration: none; color: rgb(12, 137, 207);"> 17</a> <a target=_blank id="L18" href="/article/1371597.html#L18" rel="#L18" style="text-decoration: none; color: rgb(12, 137, 207);"> 18</a> <a target=_blank id="L19" href="/article/1371597.html#L19" rel="#L19" style="text-decoration: none; color: rgb(12, 137, 207);"> 19</a> <a target=_blank id="L20" href="/article/1371597.html#L20" rel="#L20" style="text-decoration: none; color: rgb(12, 137, 207);"> 20</a> <a target=_blank id="L21" href="/article/1371597.html#L21" rel="#L21" style="text-decoration: none; color: rgb(12, 137, 207);"> 21</a> <a target=_blank id="L22" href="/article/1371597.html#L22" rel="#L22" style="text-decoration: none; color: rgb(12, 137, 207);"> 22</a> <a target=_blank id="L23" href="/article/1371597.html#L23" rel="#L23" style="text-decoration: none; color: rgb(12, 137, 207);"> 23</a> <a target=_blank id="L24" href="/article/1371597.html#L24" rel="#L24" style="text-decoration: none; color: rgb(12, 137, 207);"> 24</a> <a target=_blank id="L25" href="/article/1371597.html#L25" rel="#L25" style="text-decoration: none; color: rgb(12, 137, 207);"> 25</a> <a target=_blank id="L26" href="/article/1371597.html#L26" rel="#L26" style="text-decoration: none; color: rgb(12, 137, 207);"> 26</a> <a target=_blank id="L27" href="/article/1371597.html#L27" rel="#L27" style="text-decoration: none; color: rgb(12, 137, 207);"> 27</a> <a target=_blank id="L28" href="/article/1371597.html#L28" rel="#L28" style="text-decoration: none; color: rgb(12, 137, 207);"> 28</a> <a target=_blank id="L29" href="/article/1371597.html#L29" rel="#L29" style="text-decoration: none; color: rgb(12, 137, 207);"> 29</a> <a target=_blank id="L30" href="/article/1371597.html#L30" rel="#L30" style="text-decoration: none; color: rgb(12, 137, 207);"> 30</a> <a target=_blank id="L31" href="/article/1371597.html#L31" rel="#L31" style="text-decoration: none; color: rgb(12, 137, 207);"> 31</a> <a target=_blank id="L32" href="/article/1371597.html#L32" rel="#L32" style="text-decoration: none; color: rgb(12, 137, 207);"> 32</a> <a target=_blank id="L33" href="/article/1371597.html#L33" rel="#L33" style="text-decoration: none; color: rgb(12, 137, 207);"> 33</a> <a target=_blank id="L34" href="/article/1371597.html#L34" rel="#L34" style="text-decoration: none; color: rgb(12, 137, 207);"> 34</a> <a target=_blank id="L35" href="/article/1371597.html#L35" rel="#L35" style="text-decoration: none; color: rgb(12, 137, 207);"> 35</a> <a target=_blank id="L36" href="/article/1371597.html#L36" rel="#L36" style="text-decoration: none; color: rgb(12, 137, 207);"> 36</a> <a target=_blank id="L37" href="/article/1371597.html#L37" rel="#L37" style="text-decoration: none; color: rgb(12, 137, 207);"> 37</a> <a target=_blank id="L38" href="/article/1371597.html#L38" rel="#L38" style="text-decoration: none; color: rgb(12, 137, 207);"> 38</a> <a target=_blank id="L39" href="/article/1371597.html#L39" rel="#L39" style="text-decoration: none; color: rgb(12, 137, 207);"> 39</a> <a target=_blank id="L40" href="/article/1371597.html#L40" rel="#L40" style="text-decoration: none; color: rgb(12, 137, 207);"> 40</a> <a target=_blank id="L41" href="/article/1371597.html#L41" rel="#L41" style="text-decoration: none; color: rgb(12, 137, 207);"> 41</a> <a target=_blank id="L42" href="/article/1371597.html#L42" rel="#L42" style="text-decoration: none; color: rgb(12, 137, 207);"> 42</a> <a target=_blank id="L43" href="/article/1371597.html#L43" rel="#L43" style="text-decoration: none; color: rgb(12, 137, 207);"> 43</a> <a target=_blank id="L44" href="/article/1371597.html#L44" rel="#L44" style="text-decoration: none; color: rgb(12, 137, 207);"> 44</a> <a target=_blank id="L45" href="/article/1371597.html#L45" rel="#L45" style="text-decoration: none; color: rgb(12, 137, 207);"> 45</a> <a target=_blank id="L46" href="/article/1371597.html#L46" rel="#L46" style="text-decoration: none; color: rgb(12, 137, 207);"> 46</a> <a target=_blank id="L47" href="/article/1371597.html#L47" rel="#L47" style="text-decoration: none; color: rgb(12, 137, 207);"> 47</a> <a target=_blank id="L48" href="/article/1371597.html#L48" rel="#L48" style="text-decoration: none; color: rgb(12, 137, 207);"> 48</a> <a target=_blank id="L49" href="/article/1371597.html#L49" rel="#L49" style="text-decoration: none; color: rgb(12, 137, 207);"> 49</a> <a target=_blank id="L50" href="/article/1371597.html#L50" rel="#L50" style="text-decoration: none; color: rgb(12, 137, 207);"> 50</a> <a target=_blank id="L51" href="/article/1371597.html#L51" rel="#L51" style="text-decoration: none; color: rgb(12, 137, 207);"> 51</a> <a target=_blank id="L52" href="/article/1371597.html#L52" rel="#L52" style="text-decoration: none; color: rgb(12, 137, 207);"> 52</a> <a target=_blank id="L53" href="/article/1371597.html#L53" rel="#L53" style="text-decoration: none; color: rgb(12, 137, 207);"> 53</a> <a target=_blank id="L54" href="/article/1371597.html#L54" rel="#L54" style="text-decoration: none; color: rgb(12, 137, 207);"> 54</a> | #ifndef __HOTUPDATER_H__#define __HOTUPDATER_H__ #include "cocos2d.h"USING_NS_CC;#include "cocos-ext.h"USING_NS_CC_EXT;#include "AssetsManager/AssetsManager.h" // 热更新实现示例class UpdateLayer:public CCLayer, public AssetsManagerDelegateProtocol{public: static CCScene* scene(){ CCScene* scene = CCScene::create(); scene->addChild(UpdateLayer::create()); return scene; }; static UpdateLayer* create(){ UpdateLayer* pLayer = new UpdateLayer; if (pLayer && pLayer->init()) { pLayer->autorelease(); return pLayer; } delete pLayer; return NULL; }; // 初始化 bool init(); // 下载回调函数 virtual void onError(cocos2d::extension::AssetsManager::ErrorCode errorCode); virtual void onProgress(int percent); virtual void onSuccess(); // 菜单回调函数 void reset(CCObject* pSender); // 重置版本 void getClientVersion(CCObject* pSender); // 获取当前客户端版本号 void checkUpdate(CCObject* pSender); // 检查是否有版本更新 void update(CCObject* pSender); // 更新版本 void enterScene(CCObject* pSender); // 进入场景,如果未更新屏幕中间会显示叹号的图片,更新后会显示另一张图片protected: // 初始化下载目录 void initDownloadDir(); // 删除目录 void deleteDir(std::string dir);private: CCLabelTTF* m_label; std::string m_downloadDir; AssetsManager* getAssetsManager(); }; #endif |
来自CODE的代码片
UpdateLayer.h
项目地址:https://coding.net/u/linchaolong/p/Cocos2d-x_HotUpdate/git
相关文章推荐
- cocos2d-x使用AssetsManager类实现资源的在线更新
- Cocos2d-x实现资源热更新详解
- 【Cocos2d-x】实现资源热更新
- cocos2d-x使用AssetsManager类实现资源的在线更新
- 【Cocos2d-x】实现资源热更新
- 【Cocos2d-x】实现资源热更新
- 【Cocos2d-x】实现资源热更新
- cocos2d-x使用AssetsManager类实现资源的在线更新
- rsync+inotify 实现资源服务器的同步目录下的文件变化时,备份服务器的同步目录更新,以资源服务器为准,去同步其他客户端
- 函数项目【COCOS2DX-LUA 脚本开发之十二】Hybrid模式-利用AssetsManager实现在线更新脚本文件lua、js、图片等资源(免去平台审核周期)函数项目
- cocos2d-x学习资源汇总(持续更新。。。)
- 使用lua更新脚本后,xcode下游戏资源没有更新的问题[quick-cocos2d-x]
- rsync+inotify 实现资源服务器的同步目录下的文件变化时,备份服务器的同步目录更新,以资源服务器为准,去同步其他客户端
- cocos2d-x jsbinding 资源下载实现
- 【COCOS2DX-LUA 脚本开发之十二】Hybrid模式-利用AssetsManager实现在线更新脚本文件lua、js、图片等资源(免去平台审核周期)
- 【COCOS2DX-LUA 脚本开发之十二】Hybrid模式-利用AssetsManager实现在线更新脚本文件lua、js、图片等资源(免去平台审核周期)
- 【COCOS2DX-LUA 脚本开发之十二】Hybrid模式-利用AssetsManager实现在线更新脚本文件lua、js、图片等资源(免去平台审核周期)
- cocos2d-x Loading界面实现资源加载
- 转载子龙山人 cocos2d-x学习资源汇总(持续更新。。。)
- cocos2d-x学习资源汇总(持续更新。。。)