转载地址:http://www.jb51.cc/article/p-uquxodan-bea.html
什么是热更新?
游戏客户端启动时,主动请求服务端检查版本号,并更新资源到本地。
应用场景:
情况一:游戏客户端已经发布了,但突然发现有个比较严重的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="http://blog.csdn.net/linchaolong/article/details/42321767#L1" rel="#L1" > 1</a><a target=_blank ID="L2" href="http://blog.csdn.net/linchaolong/article/details/42321767#L2" rel="#L2" > 2</a><a target=_blank ID="L3" href="http://blog.csdn.net/linchaolong/article/details/42321767#L3" rel="#L3" > 3</a><a target=_blank ID="L4" href="http://blog.csdn.net/linchaolong/article/details/42321767#L4" rel="#L4" > 4</a><a target=_blank ID="L5" href="http://blog.csdn.net/linchaolong/article/details/42321767#L5" rel="#L5" > 5</a><a target=_blank ID="L6" href="http://blog.csdn.net/linchaolong/article/details/42321767#L6" rel="#L6" > 6</a><a target=_blank ID="L7" href="http://blog.csdn.net/linchaolong/article/details/42321767#L7" rel="#L7" > 7</a><a target=_blank ID="L8" href="http://blog.csdn.net/linchaolong/article/details/42321767#L8" rel="#L8" > 8</a><a target=_blank ID="L9" href="http://blog.csdn.net/linchaolong/article/details/42321767#L9" rel="#L9" > 9</a><a target=_blank ID="L10" href="http://blog.csdn.net/linchaolong/article/details/42321767#L10" rel="#L10" > 10</a><a target=_blank ID="L11" href="http://blog.csdn.net/linchaolong/article/details/42321767#L11" rel="#L11" > 11</a><a target=_blank ID="L12" href="http://blog.csdn.net/linchaolong/article/details/42321767#L12" rel="#L12" > 12</a><a target=_blank ID="L13" href="http://blog.csdn.net/linchaolong/article/details/42321767#L13" rel="#L13" > 13</a><a target=_blank ID="L14" href="http://blog.csdn.net/linchaolong/article/details/42321767#L14" rel="#L14" > 14</a><a target=_blank ID="L15" href="http://blog.csdn.net/linchaolong/article/details/42321767#L15" rel="#L15" > 15</a><a target=_blank ID="L16" href="http://blog.csdn.net/linchaolong/article/details/42321767#L16" rel="#L16" > 16</a><a target=_blank ID="L17" href="http://blog.csdn.net/linchaolong/article/details/42321767#L17" rel="#L17" > 17</a><a target=_blank ID="L18" href="http://blog.csdn.net/linchaolong/article/details/42321767#L18" rel="#L18" > 18</a><a target=_blank ID="L19" href="http://blog.csdn.net/linchaolong/article/details/42321767#L19" rel="#L19" > 19</a><a target=_blank ID="L20" href="http://blog.csdn.net/linchaolong/article/details/42321767#L20" rel="#L20" > 20</a><a target=_blank ID="L21" href="http://blog.csdn.net/linchaolong/article/details/42321767#L21" rel="#L21" > 21</a><a target=_blank ID="L22" href="http://blog.csdn.net/linchaolong/article/details/42321767#L22" rel="#L22" > 22</a><a target=_blank ID="L23" href="http://blog.csdn.net/linchaolong/article/details/42321767#L23" rel="#L23" > 23</a><a target=_blank ID="L24" href="http://blog.csdn.net/linchaolong/article/details/42321767#L24" rel="#L24" > 24</a><a target=_blank ID="L25" href="http://blog.csdn.net/linchaolong/article/details/42321767#L25" rel="#L25" > 25</a><a target=_blank ID="L26" href="http://blog.csdn.net/linchaolong/article/details/42321767#L26" rel="#L26" > 26</a><a target=_blank ID="L27" href="http://blog.csdn.net/linchaolong/article/details/42321767#L27" rel="#L27" > 27</a><a target=_blank ID="L28" href="http://blog.csdn.net/linchaolong/article/details/42321767#L28" rel="#L28" > 28</a><a target=_blank ID="L29" href="http://blog.csdn.net/linchaolong/article/details/42321767#L29" rel="#L29" > 29</a><a target=_blank ID="L30" href="http://blog.csdn.net/linchaolong/article/details/42321767#L30" rel="#L30" > 30</a><a target=_blank ID="L31" href="http://blog.csdn.net/linchaolong/article/details/42321767#L31" rel="#L31" > 31</a><a target=_blank ID="L32" href="http://blog.csdn.net/linchaolong/article/details/42321767#L32" rel="#L32" > 32</a><a target=_blank ID="L33" href="http://blog.csdn.net/linchaolong/article/details/42321767#L33" rel="#L33" > 33</a><a target=_blank ID="L34" href="http://blog.csdn.net/linchaolong/article/details/42321767#L34" rel="#L34" > 34</a><a target=_blank ID="L35" href="http://blog.csdn.net/linchaolong/article/details/42321767#L35" rel="#L35" > 35</a><a target=_blank ID="L36" href="http://blog.csdn.net/linchaolong/article/details/42321767#L36" rel="#L36" > 36</a><a target=_blank ID="L37" href="http://blog.csdn.net/linchaolong/article/details/42321767#L37" rel="#L37" > 37</a><a target=_blank ID="L38" href="http://blog.csdn.net/linchaolong/article/details/42321767#L38" rel="#L38" > 38</a><a target=_blank ID="L39" href="http://blog.csdn.net/linchaolong/article/details/42321767#L39" rel="#L39" > 39</a><a target=_blank ID="L40" href="http://blog.csdn.net/linchaolong/article/details/42321767#L40" rel="#L40" > 40</a><a target=_blank ID="L41" href="http://blog.csdn.net/linchaolong/article/details/42321767#L41" rel="#L41" > 41</a><a target=_blank ID="L42" href="http://blog.csdn.net/linchaolong/article/details/42321767#L42" rel="#L42" > 42</a><a target=_blank ID="L43" href="http://blog.csdn.net/linchaolong/article/details/42321767#L43" rel="#L43" > 43</a><a target=_blank ID="L44" href="http://blog.csdn.net/linchaolong/article/details/42321767#L44" rel="#L44" > 44</a><a target=_blank ID="L45" href="http://blog.csdn.net/linchaolong/article/details/42321767#L45" rel="#L45" > 45</a><a target=_blank ID="L46" href="http://blog.csdn.net/linchaolong/article/details/42321767#L46" rel="#L46" > 46</a><a target=_blank ID="L47" href="http://blog.csdn.net/linchaolong/article/details/42321767#L47" rel="#L47" > 47</a><a target=_blank ID="L48" href="http://blog.csdn.net/linchaolong/article/details/42321767#L48" rel="#L48" > 48</a><a target=_blank ID="L49" href="http://blog.csdn.net/linchaolong/article/details/42321767#L49" rel="#L49" > 49</a><a target=_blank ID="L50" href="http://blog.csdn.net/linchaolong/article/details/42321767#L50" rel="#L50" > 50</a><a target=_blank ID="L51" href="http://blog.csdn.net/linchaolong/article/details/42321767#L51" rel="#L51" > 51</a><a target=_blank ID="L52" href="http://blog.csdn.net/linchaolong/article/details/42321767#L52" rel="#L52" > 52</a><a target=_blank ID="L53" href="http://blog.csdn.net/linchaolong/article/details/42321767#L53" rel="#L53" > 53</a><a target=_blank ID="L54" href="http://blog.csdn.net/linchaolong/article/details/42321767#L54" rel="#L54" > 54</a><a target=_blank ID="L55" href="http://blog.csdn.net/linchaolong/article/details/42321767#L55" rel="#L55" > 55</a><a target=_blank ID="L56" href="http://blog.csdn.net/linchaolong/article/details/42321767#L56" rel="#L56" > 56</a><a target=_blank ID="L57" href="http://blog.csdn.net/linchaolong/article/details/42321767#L57" rel="#L57" > 57</a><a target=_blank ID="L58" href="http://blog.csdn.net/linchaolong/article/details/42321767#L58" rel="#L58" > 58</a><a target=_blank ID="L59" href="http://blog.csdn.net/linchaolong/article/details/42321767#L59" rel="#L59" > 59</a><a target=_blank ID="L60" href="http://blog.csdn.net/linchaolong/article/details/42321767#L60" rel="#L60" > 60</a><a target=_blank ID="L61" href="http://blog.csdn.net/linchaolong/article/details/42321767#L61" rel="#L61" > 61</a><a target=_blank ID="L62" href="http://blog.csdn.net/linchaolong/article/details/42321767#L62" rel="#L62" > 62</a><a target=_blank ID="L63" href="http://blog.csdn.net/linchaolong/article/details/42321767#L63" rel="#L63" > 63</a><a target=_blank ID="L64" href="http://blog.csdn.net/linchaolong/article/details/42321767#L64" rel="#L64" > 64</a><a target=_blank ID="L65" href="http://blog.csdn.net/linchaolong/article/details/42321767#L65" rel="#L65" > 65</a><a target=_blank ID="L66" href="http://blog.csdn.net/linchaolong/article/details/42321767#L66" rel="#L66" > 66</a><a target=_blank ID="L67" href="http://blog.csdn.net/linchaolong/article/details/42321767#L67" rel="#L67" > 67</a><a target=_blank ID="L68" href="http://blog.csdn.net/linchaolong/article/details/42321767#L68" rel="#L68" > 68</a><a target=_blank ID="L69" href="http://blog.csdn.net/linchaolong/article/details/42321767#L69" rel="#L69" > 69</a><a target=_blank ID="L70" href="http://blog.csdn.net/linchaolong/article/details/42321767#L70" rel="#L70" > 70</a><a target=_blank ID="L71" href="http://blog.csdn.net/linchaolong/article/details/42321767#L71" rel="#L71" > 71</a><a target=_blank ID="L72" href="http://blog.csdn.net/linchaolong/article/details/42321767#L72" rel="#L72" > 72</a><a target=_blank ID="L73" href="http://blog.csdn.net/linchaolong/article/details/42321767#L73" rel="#L73" > 73</a><a target=_blank ID="L74" href="http://blog.csdn.net/linchaolong/article/details/42321767#L74" rel="#L74" > 74</a><a target=_blank ID="L75" href="http://blog.csdn.net/linchaolong/article/details/42321767#L75" rel="#L75" > 75</a><a target=_blank ID="L76" href="http://blog.csdn.net/linchaolong/article/details/42321767#L76" rel="#L76" > 76</a><a target=_blank ID="L77" href="http://blog.csdn.net/linchaolong/article/details/42321767#L77" rel="#L77" > 77</a><a target=_blank ID="L78" href="http://blog.csdn.net/linchaolong/article/details/42321767#L78" rel="#L78" > 78</a><a target=_blank ID="L79" href="http://blog.csdn.net/linchaolong/article/details/42321767#L79" rel="#L79" > 79</a><a target=_blank ID="L80" href="http://blog.csdn.net/linchaolong/article/details/42321767#L80" rel="#L80" > 80</a><a target=_blank ID="L81" href="http://blog.csdn.net/linchaolong/article/details/42321767#L81" rel="#L81" > 81</a><a target=_blank ID="L82" href="http://blog.csdn.net/linchaolong/article/details/42321767#L82" rel="#L82" > 82</a><a target=_blank ID="L83" href="http://blog.csdn.net/linchaolong/article/details/42321767#L83" rel="#L83" > 83</a><a target=_blank ID="L84" href="http://blog.csdn.net/linchaolong/article/details/42321767#L84" rel="#L84" > 84</a><a target=_blank ID="L85" href="http://blog.csdn.net/linchaolong/article/details/42321767#L85" rel="#L85" > 85</a><a target=_blank ID="L86" href="http://blog.csdn.net/linchaolong/article/details/42321767#L86" rel="#L86" > 86</a><a target=_blank ID="L87" href="http://blog.csdn.net/linchaolong/article/details/42321767#L87" rel="#L87" > 87</a><a target=_blank ID="L88" href="http://blog.csdn.net/linchaolong/article/details/42321767#L88" rel="#L88" > 88</a><a target=_blank ID="L89" href="http://blog.csdn.net/linchaolong/article/details/42321767#L89" rel="#L89" > 89</a><a target=_blank ID="L90" href="http://blog.csdn.net/linchaolong/article/details/42321767#L90" rel="#L90" > 90</a><a target=_blank ID="L91" href="http://blog.csdn.net/linchaolong/article/details/42321767#L91" rel="#L91" > 91</a><a target=_blank ID="L92" href="http://blog.csdn.net/linchaolong/article/details/42321767#L92" rel="#L92" > 92</a><a target=_blank ID="L93" href="http://blog.csdn.net/linchaolong/article/details/42321767#L93" rel="#L93" > 93</a><a target=_blank ID="L94" href="http://blog.csdn.net/linchaolong/article/details/42321767#L94" rel="#L94" > 94</a><a target=_blank ID="L95" href="http://blog.csdn.net/linchaolong/article/details/42321767#L95" rel="#L95" > 95</a><a target=_blank ID="L96" href="http://blog.csdn.net/linchaolong/article/details/42321767#L96" rel="#L96" > 96</a><a target=_blank ID="L97" href="http://blog.csdn.net/linchaolong/article/details/42321767#L97" rel="#L97" > 97</a><a target=_blank ID="L98" href="http://blog.csdn.net/linchaolong/article/details/42321767#L98" rel="#L98" > 98</a><a target=_blank ID="L99" href="http://blog.csdn.net/linchaolong/article/details/42321767#L99" rel="#L99" > 99</a><a target=_blank ID="L100" href="http://blog.csdn.net/linchaolong/article/details/42321767#L100" rel="#L100" > 100</a><a target=_blank ID="L101" href="http://blog.csdn.net/linchaolong/article/details/42321767#L101" rel="#L101" > 101</a><a target=_blank ID="L102" href="http://blog.csdn.net/linchaolong/article/details/42321767#L102" rel="#L102" > 102</a><a target=_blank ID="L103" href="http://blog.csdn.net/linchaolong/article/details/42321767#L103" rel="#L103" > 103</a><a target=_blank ID="L104" href="http://blog.csdn.net/linchaolong/article/details/42321767#L104" rel="#L104" > 104</a><a target=_blank ID="L105" href="http://blog.csdn.net/linchaolong/article/details/42321767#L105" rel="#L105" > 105</a><a target=_blank ID="L106" href="http://blog.csdn.net/linchaolong/article/details/42321767#L106" rel="#L106" > 106</a><a target=_blank ID="L107" href="http://blog.csdn.net/linchaolong/article/details/42321767#L107" rel="#L107" > 107</a><a target=_blank ID="L108" href="http://blog.csdn.net/linchaolong/article/details/42321767#L108" rel="#L108" > 108</a><a target=_blank ID="L109" href="http://blog.csdn.net/linchaolong/article/details/42321767#L109" rel="#L109" > 109</a><a target=_blank ID="L110" href="http://blog.csdn.net/linchaolong/article/details/42321767#L110" rel="#L110" > 110</a><a target=_blank ID="L111" href="http://blog.csdn.net/linchaolong/article/details/42321767#L111" rel="#L111" > 111</a><a target=_blank ID="L112" href="http://blog.csdn.net/linchaolong/article/details/42321767#L112" rel="#L112" > 112</a><a target=_blank ID="L113" href="http://blog.csdn.net/linchaolong/article/details/42321767#L113" rel="#L113" > 113</a><a target=_blank ID="L114" href="http://blog.csdn.net/linchaolong/article/details/42321767#L114" rel="#L114" > 114</a><a target=_blank ID="L115" href="http://blog.csdn.net/linchaolong/article/details/42321767#L115" rel="#L115" > 115</a><a target=_blank ID="L116" href="http://blog.csdn.net/linchaolong/article/details/42321767#L116" rel="#L116" > 116</a><a target=_blank ID="L117" href="http://blog.csdn.net/linchaolong/article/details/42321767#L117" rel="#L117" > 117</a><a target=_blank ID="L118" href="http://blog.csdn.net/linchaolong/article/details/42321767#L118" rel="#L118" > 118</a><a target=_blank ID="L119" href="http://blog.csdn.net/linchaolong/article/details/42321767#L119" rel="#L119" > 119</a><a target=_blank ID="L120" href="http://blog.csdn.net/linchaolong/article/details/42321767#L120" rel="#L120" > 120</a><a target=_blank ID="L121" href="http://blog.csdn.net/linchaolong/article/details/42321767#L121" rel="#L121" > 121</a><a target=_blank ID="L122" href="http://blog.csdn.net/linchaolong/article/details/42321767#L122" rel="#L122" > 122</a><a target=_blank ID="L123" href="http://blog.csdn.net/linchaolong/article/details/42321767#L123" rel="#L123" > 123</a><a target=_blank ID="L124" href="http://blog.csdn.net/linchaolong/article/details/42321767#L124" rel="#L124" > 124</a><a target=_blank ID="L125" href="http://blog.csdn.net/linchaolong/article/details/42321767#L125" rel="#L125" > 125</a><a target=_blank ID="L126" href="http://blog.csdn.net/linchaolong/article/details/42321767#L126" rel="#L126" > 126</a><a target=_blank ID="L127" href="http://blog.csdn.net/linchaolong/article/details/42321767#L127" rel="#L127" > 127</a><a target=_blank ID="L128" href="http://blog.csdn.net/linchaolong/article/details/42321767#L128" rel="#L128" > 128</a><a target=_blank ID="L129" href="http://blog.csdn.net/linchaolong/article/details/42321767#L129" rel="#L129" > 129</a><a target=_blank ID="L130" href="http://blog.csdn.net/linchaolong/article/details/42321767#L130" rel="#L130" > 130</a><a target=_blank ID="L131" href="http://blog.csdn.net/linchaolong/article/details/42321767#L131" rel="#L131" > 131</a><a target=_blank ID="L132" href="http://blog.csdn.net/linchaolong/article/details/42321767#L132" rel="#L132" > 132</a><a target=_blank ID="L133" href="http://blog.csdn.net/linchaolong/article/details/42321767#L133" rel="#L133" > 133</a><a target=_blank ID="L134" href="http://blog.csdn.net/linchaolong/article/details/42321767#L134" rel="#L134" > 134</a><a target=_blank ID="L135" href="http://blog.csdn.net/linchaolong/article/details/42321767#L135" rel="#L135" > 135</a><a target=_blank ID="L136" href="http://blog.csdn.net/linchaolong/article/details/42321767#L136" rel="#L136" > 136</a><a target=_blank ID="L137" href="http://blog.csdn.net/linchaolong/article/details/42321767#L137" rel="#L137" > 137</a><a target=_blank ID="L138" href="http://blog.csdn.net/linchaolong/article/details/42321767#L138" rel="#L138" > 138</a><a target=_blank ID="L139" href="http://blog.csdn.net/linchaolong/article/details/42321767#L139" rel="#L139" > 139</a><a target=_blank ID="L140" href="http://blog.csdn.net/linchaolong/article/details/42321767#L140" rel="#L140" > 140</a><a target=_blank ID="L141" href="http://blog.csdn.net/linchaolong/article/details/42321767#L141" rel="#L141" > 141</a><a target=_blank ID="L142" href="http://blog.csdn.net/linchaolong/article/details/42321767#L142" rel="#L142" > 142</a><a target=_blank ID="L143" href="http://blog.csdn.net/linchaolong/article/details/42321767#L143" rel="#L143" > 143</a><a target=_blank ID="L144" href="http://blog.csdn.net/linchaolong/article/details/42321767#L144" rel="#L144" > 144</a><a target=_blank ID="L145" href="http://blog.csdn.net/linchaolong/article/details/42321767#L145" rel="#L145" > 145</a><a target=_blank ID="L146" href="http://blog.csdn.net/linchaolong/article/details/42321767#L146" rel="#L146" > 146</a><a target=_blank ID="L147" href="http://blog.csdn.net/linchaolong/article/details/42321767#L147" rel="#L147" > 147</a><a target=_blank ID="L148" href="http://blog.csdn.net/linchaolong/article/details/42321767#L148" rel="#L148" > 148</a><a target=_blank ID="L149" href="http://blog.csdn.net/linchaolong/article/details/42321767#L149" rel="#L149" > 149</a><a target=_blank ID="L150" href="http://blog.csdn.net/linchaolong/article/details/42321767#L150" rel="#L150" > 150</a><a target=_blank ID="L151" href="http://blog.csdn.net/linchaolong/article/details/42321767#L151" rel="#L151" > 151</a><a target=_blank ID="L152" href="http://blog.csdn.net/linchaolong/article/details/42321767#L152" rel="#L152" > 152</a><a target=_blank ID="L153" href="http://blog.csdn.net/linchaolong/article/details/42321767#L153" rel="#L153" > 153</a><a target=_blank ID="L154" href="http://blog.csdn.net/linchaolong/article/details/42321767#L154" rel="#L154" > 154</a><a target=_blank ID="L155" href="http://blog.csdn.net/linchaolong/article/details/42321767#L155" rel="#L155" > 155</a><a target=_blank ID="L156" href="http://blog.csdn.net/linchaolong/article/details/42321767#L156" rel="#L156" > 156</a><a target=_blank ID="L157" href="http://blog.csdn.net/linchaolong/article/details/42321767#L157" rel="#L157" > 157</a><a target=_blank ID="L158" href="http://blog.csdn.net/linchaolong/article/details/42321767#L158" rel="#L158" > 158</a><a target=_blank ID="L159" href="http://blog.csdn.net/linchaolong/article/details/42321767#L159" rel="#L159" > 159</a><a target=_blank ID="L160" href="http://blog.csdn.net/linchaolong/article/details/42321767#L160" rel="#L160" > 160</a><a target=_blank ID="L161" href="http://blog.csdn.net/linchaolong/article/details/42321767#L161" rel="#L161" > 161</a><a target=_blank ID="L162" href="http://blog.csdn.net/linchaolong/article/details/42321767#L162" rel="#L162" > 162</a><a target=_blank ID="L163" href="http://blog.csdn.net/linchaolong/article/details/42321767#L163" rel="#L163" > 163</a><a target=_blank ID="L164" href="http://blog.csdn.net/linchaolong/article/details/42321767#L164" rel="#L164" > 164</a><a target=_blank ID="L165" href="http://blog.csdn.net/linchaolong/article/details/42321767#L165" rel="#L165" > 165</a><a target=_blank ID="L166" href="http://blog.csdn.net/linchaolong/article/details/42321767#L166" rel="#L166" > 166</a><a target=_blank ID="L167" href="http://blog.csdn.net/linchaolong/article/details/42321767#L167" rel="#L167" > 167</a><a target=_blank ID="L168" href="http://blog.csdn.net/linchaolong/article/details/42321767#L168" rel="#L168" > 168</a><a target=_blank ID="L169" href="http://blog.csdn.net/linchaolong/article/details/42321767#L169" rel="#L169" > 169</a><a target=_blank ID="L170" href="http://blog.csdn.net/linchaolong/article/details/42321767#L170" rel="#L170" > 170</a><a target=_blank ID="L171" href="http://blog.csdn.net/linchaolong/article/details/42321767#L171" rel="#L171" > 171</a><a target=_blank ID="L172" href="http://blog.csdn.net/linchaolong/article/details/42321767#L172" rel="#L172" > 172</a><a target=_blank ID="L173" href="http://blog.csdn.net/linchaolong/article/details/42321767#L173" rel="#L173" > 173</a><a target=_blank ID="L174" href="http://blog.csdn.net/linchaolong/article/details/42321767#L174" rel="#L174" > 174</a><a target=_blank ID="L175" href="http://blog.csdn.net/linchaolong/article/details/42321767#L175" rel="#L175" > 175</a><a target=_blank ID="L176" href="http://blog.csdn.net/linchaolong/article/details/42321767#L176" rel="#L176" > 176</a><a target=_blank ID="L177" href="http://blog.csdn.net/linchaolong/article/details/42321767#L177" rel="#L177" > 177</a><a target=_blank ID="L178" href="http://blog.csdn.net/linchaolong/article/details/42321767#L178" rel="#L178" > 178</a><a target=_blank ID="L179" href="http://blog.csdn.net/linchaolong/article/details/42321767#L179" rel="#L179" > 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 ); @H_630_404@ CCSize visibleSize = CCDirector :: sharedDirector () -> getVisibleSize (); // 重置 CcmenuItemFont * itemreset = CcmenuItemFont :: create ( "reset" , this , menu_selector ( UpdateLayer :: reset )); itemreset -> setposition ( ccp ( visibleSize . wIDth / 2 ,153)">50 )); menu -> addChild ( itemreset ); // 获取当前版本号 CcmenuItemFont * itemGetClIEntVersion = CcmenuItemFont :: create ( "getClIEntVersion" , menu_selector ( UpdateLayer :: getClIEntVersion )); itemGetClIEntVersion -> setposition ( ccp ( visibleSize . wIDth / 100 )); menu -> addChild ( itemGetClIEntVersion ); // 获取服务器最新版本 CcmenuItemFont * itemGetServerVersion = CcmenuItemFont :: create ( "checkUpdate" , menu_selector ( UpdateLayer :: checkUpdate )); itemGetServerVersion -> setposition ( ccp ( visibleSize . wIDth / 150 )); menu -> addChild ( itemGetServerVersion ); // 更新版本 CcmenuItemFont * itemUpdateVersion = CcmenuItemFont :: create ( "updateVersion" , menu_selector ( UpdateLayer :: update )); itemUpdateVersion -> setposition ( ccp ( visibleSize . wIDth / 200 )); menu -> addChild ( itemUpdateVersion ); // 进入场景 CcmenuItemFont * itemEnterScene = CcmenuItemFont :: create ( "enterScene" , menu_selector ( UpdateLayer :: enterScene )); itemEnterScene -> setposition ( ccp ( visibleSize . wIDth / 250 )); menu -> addChild ( itemEnterScene ); return true ; } 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 (),153)">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 += " ; 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 ,153)">20 ,240)">"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 ( 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 ()); } |
项目地址: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
示例工程
【点击下载源码】
把工程放到引擎projects目录下即可。
#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 ); @H_630_404@ CCSize visibleSize = CCDirector :: sharedDirector () -> getVisibleSize (); // 重置 CcmenuItemFont * itemreset = CcmenuItemFont :: create ( "reset" , this , menu_selector ( UpdateLayer :: reset )); itemreset -> setposition ( ccp ( visibleSize . wIDth / 2 ,153)">50 )); menu -> addChild ( itemreset ); // 获取当前版本号 CcmenuItemFont * itemGetClIEntVersion = CcmenuItemFont :: create ( "getClIEntVersion" , menu_selector ( UpdateLayer :: getClIEntVersion )); itemGetClIEntVersion -> setposition ( ccp ( visibleSize . wIDth / 100 )); menu -> addChild ( itemGetClIEntVersion ); // 获取服务器最新版本 CcmenuItemFont * itemGetServerVersion = CcmenuItemFont :: create ( "checkUpdate" , menu_selector ( UpdateLayer :: checkUpdate )); itemGetServerVersion -> setposition ( ccp ( visibleSize . wIDth / 150 )); menu -> addChild ( itemGetServerVersion ); // 更新版本 CcmenuItemFont * itemUpdateVersion = CcmenuItemFont :: create ( "updateVersion" , menu_selector ( UpdateLayer :: update )); itemUpdateVersion -> setposition ( ccp ( visibleSize . wIDth / 200 )); menu -> addChild ( itemUpdateVersion ); // 进入场景 CcmenuItemFont * itemEnterScene = CcmenuItemFont :: create ( "enterScene" , menu_selector ( UpdateLayer :: enterScene )); itemEnterScene -> setposition ( ccp ( visibleSize . wIDth / 250 )); menu -> addChild ( itemEnterScene ); return true ; } 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 (),153)">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 += " ; 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 ,153)">20 ,240)">"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 ( 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 ()); } |
https://Coding.net/u/linchaolong/p/Cocos2d-x_HotUpdate/git
总结以上是内存溢出为你收集整理的【Cocos2d-x】实现资源热更新全部内容,希望文章能够帮你解决【Cocos2d-x】实现资源热更新所遇到的程序开发问题。
如果觉得内存溢出网站内容还不错,欢迎将内存溢出网站推荐给程序员好友。
欢迎分享,转载请注明来源:内存溢出
评论列表(0条)