resourceUtil.ts 7.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252
  1. import { _decorator, Prefab, Node, SpriteComponent, SpriteFrame, ImageAsset, resources, error, Texture2D, instantiate, isValid, find, TextAsset, JsonAsset } from "cc";
  2. const { ccclass } = _decorator;
  3. @ccclass("resourceUtil")
  4. export class resourceUtil {
  5. /**
  6. * 加载资源
  7. * @param url 资源路径
  8. * @param type 资源类型
  9. * @param cb 回调
  10. * @method loadRes
  11. */
  12. public static loadRes (url: string, type: any, cb: Function = ()=>{}) {
  13. resources.load(url, (err: any, res: any)=>{
  14. if (err) {
  15. error(err.message || err);
  16. cb(err, res);
  17. return;
  18. }
  19. cb && cb(null, res);
  20. })
  21. }
  22. /**
  23. * 获取特效prefab
  24. * @param modulePath 路径
  25. * @returns
  26. */
  27. public static loadEffectRes (modulePath: string) {
  28. return new Promise((resolve, reject)=>{
  29. this.loadRes(`prefab/effect/${modulePath}`, Prefab, (err: any, prefab: Prefab)=>{
  30. if (err) {
  31. console.error('effect load failed', modulePath);
  32. reject && reject();
  33. return;
  34. }
  35. resolve && resolve(prefab);
  36. })
  37. })
  38. }
  39. /**
  40. * 获取模型数据
  41. * @param modulePath 模型路径
  42. * @returns
  43. */
  44. public static loadModelRes (modulePath: string) {
  45. return new Promise((resolve, reject)=>{
  46. this.loadRes(`prefab/model/${modulePath}`, Prefab, (err: any, prefab: Prefab)=>{
  47. if (err) {
  48. console.error("model load failed", modulePath);
  49. reject && reject();
  50. return;
  51. }
  52. resolve && resolve(prefab);
  53. })
  54. })
  55. }
  56. /**
  57. * 获取多模型数据
  58. * @param path 资源路径
  59. * @param arrName 资源名称
  60. * @param progressCb 过程回调函数
  61. * @param completeCb 完成回调函数
  62. */
  63. public static loadModelResArr (path: string ,arrName: Array<string>, progressCb: any, completeCb: any) {
  64. let arrUrls = arrName.map((item)=>{
  65. return `${path}/${item}`;
  66. })
  67. resources.load(arrUrls, Prefab, progressCb, completeCb);
  68. }
  69. /**
  70. * 获取贴图资源
  71. * @param path 贴图路径
  72. * @returns
  73. */
  74. public static loadSpriteFrameRes(path: string) {
  75. return new Promise((resolve, reject)=>{
  76. this.loadRes(path, SpriteFrame, (err: any, img: ImageAsset)=>{
  77. if (err) {
  78. console.error('spriteFrame load failed!', path, err);
  79. reject && reject();
  80. return;
  81. }
  82. let texture = new Texture2D();
  83. texture.image = img;
  84. let sf = new SpriteFrame();
  85. sf.texture = texture;
  86. resolve && resolve(sf);
  87. })
  88. })
  89. }
  90. /**
  91. * 获取关卡数据
  92. * @param level 关卡
  93. * @param cb 回调函数
  94. */
  95. public static getMap (level: number, cb: Function) {
  96. let levelStr: string = 'map';
  97. //前面补0
  98. if (level >= 100) {
  99. levelStr += level;
  100. } else if (level >= 10) {
  101. levelStr += '0' + level;
  102. } else {
  103. levelStr += '00' + level;
  104. }
  105. this.loadRes(`map/config/${levelStr}`, null, (err: {}, txtAsset: any)=>{
  106. if (err) {
  107. cb(err, txtAsset);
  108. return;
  109. }
  110. let content: string = '';
  111. if (txtAsset._file) {
  112. //@ts-ignore
  113. if (window['LZString']) {
  114. //@ts-ignore
  115. content = window['LZString'].decompressFromEncodedURIComponent(txtAsset._file);
  116. }
  117. var objJson = JSON.parse(content);
  118. cb(null, objJson);
  119. } else if (txtAsset.text) {
  120. //@ts-ignore
  121. if (window['LZString']) {
  122. //@ts-ignore
  123. content = window['LZString'].decompressFromEncodedURIComponent(txtAsset.text);
  124. }
  125. var objJson = JSON.parse(content);
  126. cb(null, objJson);
  127. } else if (txtAsset.json) {
  128. cb(null, txtAsset.json);
  129. } else {
  130. cb('failed');
  131. }
  132. });
  133. }
  134. /**
  135. * 获取关卡数据
  136. * @param type 关卡类型
  137. * @param arrName 资源名称
  138. * @param progressCb 过程回调函数
  139. * @param completeCb 完成回调函数
  140. */
  141. public static getMapObj(type: string, arrName: Array<string>, progressCb?:any, completeCb?:any) {
  142. let arrUrls: string[] = [];
  143. for (let idx = 0; idx < arrName.length; idx++) {
  144. arrUrls.push(`map/${type}/${arrName[idx]}`)
  145. }
  146. resources.load(arrUrls, Prefab, progressCb, completeCb);
  147. }
  148. /**
  149. * 获取UI prefab
  150. * @param prefabPath prefab路径
  151. * @param cb 回调函数
  152. */
  153. public static getUIPrefabRes (prefabPath: string, cb?: Function) {
  154. this.loadRes("prefab/ui/" + prefabPath, Prefab, cb);
  155. }
  156. /**
  157. * 创建ui界面
  158. * @param path ui路径
  159. * @param cb 回调函数
  160. * @param parent 父节点
  161. */
  162. public static createUI (path: string, cb?: Function, parent?: Node) {
  163. this.getUIPrefabRes(path, function (err: {}, prefab: Prefab) {
  164. if (err) return;
  165. let node: Node = instantiate(prefab);
  166. node.setPosition(0, 0, 0);
  167. if (!parent) {
  168. parent = find("Canvas") as Node;
  169. }
  170. parent.addChild(node);
  171. cb && cb(null, node);
  172. });
  173. }
  174. /**
  175. * 获取json数据
  176. * @param fileName 文件名
  177. * @param cb 回调函数
  178. */
  179. public static getJsonData (fileName: string, cb: Function) {
  180. this.loadRes("datas/" + fileName, null, function (err: any, content: JsonAsset) {
  181. if (err) {
  182. error(err.message || err);
  183. return;
  184. }
  185. if (content.json) {
  186. cb(err, content.json);
  187. } else {
  188. cb('failed!!!');
  189. }
  190. });
  191. }
  192. /**
  193. * 获取文本数据
  194. * @param fileName 文件名
  195. * @param cb 回调函数
  196. */
  197. public static getTextData (fileName:string, cb: Function) {
  198. this.loadRes("datas/" + fileName, null, function (err: any, content: TextAsset) {
  199. if (err) {
  200. error(err.message || err);
  201. return;
  202. }
  203. let text: string = content.text;
  204. cb(err, text);
  205. });
  206. }
  207. /**
  208. * 设置精灵贴图
  209. * @param path 资源路径
  210. * @param sprite 精灵
  211. * @param cb 回调函数
  212. */
  213. public static setSpriteFrame (path: string, sprite: SpriteComponent, cb: Function) {
  214. this.loadRes(path + '/spriteFrame', SpriteFrame, (err: any, spriteFrame: SpriteFrame)=> {
  215. if (err) {
  216. console.error('set sprite frame failed! err:', path, err);
  217. cb(err);
  218. return;
  219. }
  220. if (sprite && isValid(sprite)) {
  221. sprite.spriteFrame = spriteFrame;
  222. cb(null);
  223. }
  224. });
  225. }
  226. }