bb73e37e-d24d-475e-9a84-8a791281b83d.js
3.07 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
"use strict";
cc._RF.push(module, 'bb73eN+0k1HXpqEinkSgbg9', 'ResUtils');
// script/common/utils/ResUtils.ts
"use strict";
Object.defineProperty(exports, "__esModule", { value: true });
exports.ResUtils = void 0;
var ResUtils;
(function (ResUtils) {
async function loadRes(resource, type, maxTryCount = 3) {
return new Promise((resolve, reject) => {
let tryCount = 0;
function loadFunc() {
tryCount++;
cc.resources.load(resource, type, (err, res) => {
if (err) {
if (tryCount < maxTryCount) {
setTimeout(() => {
loadFunc();
}, 500);
}
else {
reject(err);
}
}
else {
if (res.json) {
resolve(res.json);
}
else if (res.text) {
resolve(res.text);
}
else {
resolve(res);
}
}
});
}
loadFunc();
});
}
ResUtils.loadRes = loadRes;
//动态加载图片的方法
async function loadOnlineImg(container, url) {
return new Promise((resolve, reject) => {
cc.assetManager.loadRemote(url, function (err, texture) {
if (err) {
reject(err);
}
else {
var sprite = new cc.SpriteFrame(texture);
container.spriteFrame = sprite;
resolve(texture);
}
});
});
}
ResUtils.loadOnlineImg = loadOnlineImg;
/**释放资源 */
function releaseRes(resource) {
cc.resources.release(resource);
}
ResUtils.releaseRes = releaseRes;
/**批量释放资源 */
function releaseResArray(resources) {
for (let i = 0; i < resources.length; i++) {
cc.resources.release(resources[i]);
}
}
ResUtils.releaseResArray = releaseResArray;
function releaseAsset(asset) {
cc.assetManager.releaseAsset(asset);
}
ResUtils.releaseAsset = releaseAsset;
function releaseAssetArray(assets) {
assets.forEach((asset) => cc.assetManager.releaseAsset(asset));
}
ResUtils.releaseAssetArray = releaseAssetArray;
/**加载预制体并实例化node */
async function createWithPrefab(resource) {
let prefab = await ResUtils.loadRes(resource, cc.Prefab);
if (!prefab)
throw new Error("Load prefab failed!");
let node = cc.instantiate(prefab);
if (node) {
return node;
}
else {
throw new Error("Instantiate node failed!");
}
}
ResUtils.createWithPrefab = createWithPrefab;
//检
})(ResUtils = exports.ResUtils || (exports.ResUtils = {}));
cc._RF.pop();