ResourceUtil.ts 1.5 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455
  1. import { _decorator, Component, error, Node, resources } from 'cc';
  2. const { ccclass, property } = _decorator;
  3. @ccclass('ResourceUtil')
  4. export class ResourceUtil{
  5. public static loadRes (url: string, type: any, cb: Function = ()=>{}) {
  6. resources.load(url, (err: any, res: any)=>{
  7. if (err) {
  8. error(err.message || err);
  9. cb(err, res);
  10. return;
  11. }
  12. cb && cb(null, res);
  13. })
  14. }
  15. /**
  16. * 移除数组中predicate(断言)返回为真值的所有元素
  17. * @param {Array} array 一个用来迭代的集合.
  18. * @param {Function} predicate 一个迭代函数
  19. * @returns
  20. */
  21. public static remove(array: any[], predicate: Function) {
  22. var result: any[] = [];
  23. var indexes: any[] = [];
  24. array.forEach(function (item, index) {
  25. if (predicate(item)) {
  26. result.push(item);
  27. indexes.push(index);
  28. }
  29. });
  30. ResourceUtil._basePullAt(array, indexes);
  31. return result;
  32. }
  33. private static _basePullAt(array: any[], indexes: any[]) {
  34. var length = array ? indexes.length : 0;
  35. var lastIndex = length - 1;
  36. var previous;
  37. while (length--) {
  38. var index = indexes[length];
  39. if (length === lastIndex || index !== previous) {
  40. previous = index;
  41. Array.prototype.splice.call(array, index, 1);
  42. }
  43. }
  44. return array;
  45. }
  46. }