JMNetImage.js 5.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169
  1. /**
  2. * 在线图片
  3. * 主要功能:根据图片下载地址加载在线图片
  4. */
  5. cc.Class({
  6. extends: cc.Component,
  7. editor: {
  8. menu: '嘉米公用/JMNetImage'
  9. },
  10. properties: {
  11. iconUrl: {
  12. type: cc.String,
  13. get () {
  14. return this._iconUrl;
  15. },
  16. set (value) {
  17. if (this._iconUrl !== value) {
  18. this._iconUrl = value;
  19. if (this._sprite) {
  20. this._sprite.spriteFrame = this._defaultSpriteFrame;
  21. }
  22. this.updateSprite();
  23. }
  24. },
  25. tooltip: '图片下载地址'
  26. },
  27. _iconUrl: '',
  28. defaultSpriteFrame: {
  29. type: cc.SpriteFrame,
  30. get () {
  31. return this._defaultSpriteFrame;
  32. },
  33. set (value) {
  34. if (this._defaultSpriteFrame !== value) {
  35. this._defaultSpriteFrame = value;
  36. if (!this.iconUrl || this.iconUrl.length == 0) {
  37. if (this._sprite) {
  38. this._sprite.spriteFrame = this._defaultSpriteFrame;
  39. }
  40. }
  41. }
  42. },
  43. tooltip: '默认图片'
  44. },
  45. _defaultSpriteFrame: undefined
  46. },
  47. onEnable: function () {
  48. cc.game.on('e_mgr_net_image_download_ok', this._handleDownloadOK, this);
  49. },
  50. onDisable: function () {
  51. cc.game.targetOff(this);
  52. },
  53. start () {
  54. this._sprite = this.getComponent(cc.Sprite);
  55. if (!this._defaultSpriteFrame) {
  56. this._defaultSpriteFrame = this._sprite.spriteFrame;
  57. } else {
  58. this._sprite.spriteFrame = this._defaultSpriteFrame;
  59. }
  60. this.updateSprite();
  61. },
  62. /**
  63. * 刷新精灵
  64. *
  65. * @author Pyden
  66. * @date 2019-03-21
  67. * @returns
  68. */
  69. updateSprite () {
  70. if (!this._sprite) {
  71. return;
  72. }
  73. if (!this.iconUrl || this.iconUrl.length == 0) {
  74. this._sprite.spriteFrame = this._defaultSpriteFrame;
  75. return;
  76. }
  77. // 目前仅支持原生平台(mobile app, desktop app, or simulator)
  78. if (!CC_JSB) {
  79. // web 平台 直接load资源
  80. // 如果浏览器没有缓存,会去下载然后回调
  81. // 如果浏览器有缓存,会去磁盘本地加载然后回调
  82. // http://www.a.com/icon.png
  83. // http://www.a.com/icon.png?v=1.0
  84. // 上面两个地址虽然指向了同一个服务器icon资源
  85. // 但是对于浏览器来说是两个不同url,会进行两次缓存
  86. // 需要避免修改后缀造成的多次请求,也可以使用这个特性刷新本地缓存
  87. cc.loader.load({url: this.iconUrl, type: 'png'}, (error, texture) => {
  88. if (!error && cc.isValid(this.node, true)) {
  89. this._sprite.spriteFrame = new cc.SpriteFrame(texture);
  90. }
  91. });
  92. } else {
  93. // native 平台
  94. // 如果本地有图标了,则直接展示
  95. let fullPath = G.NetImageMgr.getImageFullPathWithUrl(this.iconUrl);
  96. if (fullPath) {
  97. // 资源在image文件夹中
  98. cc.loader.load(fullPath, (error, texture) => {
  99. if (error) {
  100. G.LogUtils.error(error);
  101. return;
  102. }
  103. // 类型转换
  104. if (texture instanceof Image) {
  105. let t = new cc.Texture2D();
  106. t.initWithElement(texture);
  107. texture = t;
  108. }
  109. if (cc.isValid(this.node, true)) {
  110. this._sprite.spriteFrame = new cc.SpriteFrame(texture);
  111. }
  112. });
  113. } else {
  114. // 真实地址
  115. let md5Path = cc.url.raw('resources/res_image/icon/' + cc.path.basename(this.iconUrl));
  116. if (jsb.fileUtils.isFileExist(md5Path)) {
  117. // 资源在本地包内
  118. let url = 'res_image/icon/' + cc.path.mainFileName(cc.path.basename(this.iconUrl));
  119. cc.loader.loadRes(url, (error, texture) => {
  120. if (error) {
  121. G.LogUtils.error(error);
  122. return;
  123. }
  124. // 类型转换
  125. if (texture instanceof Image) {
  126. let t = new cc.Texture2D();
  127. t.initWithElement(texture);
  128. texture = t;
  129. }
  130. if (cc.isValid(this.node, true)) {
  131. this._sprite.spriteFrame = new cc.SpriteFrame(texture);
  132. }
  133. });
  134. } else {
  135. // 缓存目录和包内都没有就去下载
  136. G.NetImageMgr.downloadImage(this.iconUrl);
  137. }
  138. }
  139. }
  140. },
  141. // ---------------- 私有方法分割线 ----------------
  142. /**
  143. * 下载成功的回调
  144. *
  145. * @author Pyden
  146. * @date 2019-03-21
  147. * @param {string} url 下载地址
  148. */
  149. _handleDownloadOK (url) {
  150. if (url == this.iconUrl) {
  151. this.updateSprite();
  152. }
  153. }
  154. });