bb73e37e-d24d-475e-9a84-8a791281b83d.js 3.07 KB
"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();