| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500 |
- System.register(["cc"], function (_export, _context) {
- "use strict";
- var _cclegacy, __checkObsolete__, __checkObsoleteInNamespace__, Asset, assetManager, error, js, resources, warn, ResLoader, _crd, resLoader;
- function asyncGeneratorStep(gen, resolve, reject, _next, _throw, key, arg) { try { var info = gen[key](arg); var value = info.value; } catch (error) { reject(error); return; } if (info.done) { resolve(value); } else { Promise.resolve(value).then(_next, _throw); } }
- function _asyncToGenerator(fn) { return function () { var self = this, args = arguments; return new Promise(function (resolve, reject) { var gen = fn.apply(self, args); function _next(value) { asyncGeneratorStep(gen, resolve, reject, _next, _throw, "next", value); } function _throw(err) { asyncGeneratorStep(gen, resolve, reject, _next, _throw, "throw", err); } _next(undefined); }); }; }
- _export("ResLoader", void 0);
- return {
- setters: [function (_cc) {
- _cclegacy = _cc.cclegacy;
- __checkObsolete__ = _cc.__checkObsolete__;
- __checkObsoleteInNamespace__ = _cc.__checkObsoleteInNamespace__;
- Asset = _cc.Asset;
- assetManager = _cc.assetManager;
- error = _cc.error;
- js = _cc.js;
- resources = _cc.resources;
- warn = _cc.warn;
- }],
- execute: function () {
- _crd = true;
- _cclegacy._RF.push({}, "1a2e4jFffpHrYjrpxbnC760", "ResLoader", undefined);
- __checkObsolete__(['Asset', 'AssetManager', '__private', 'assetManager', 'error', 'js', 'resources', 'warn']);
- /**
- * 游戏资源管理
- * 1、加载默认resources文件夹中资源
- * 2、加载默认bundle远程资源
- * 3、主动传递bundle名时,优先加载传递bundle名资源包中的资源
- *
- * @help https://gitee.com/dgflash/oops-framework/wikis/pages?sort_id=12037901&doc_id=2873565
- */
- _export("ResLoader", ResLoader = class ResLoader {
- constructor() {
- //#region 资源配置数据
- /** 全局默认加载的资源包名 */
- this.defaultBundleName = "resources";
- /** 是否使用远程 CDN 资源 */
- this.cdn = false;
- /** 资源包配置 */
- this.bundles = new Map();
- }
- /** 下载时的最大并发数 - 项目设置 -> 项目数据 -> 资源下载并发数,设置默认值;初始值为15 */
- get maxConcurrency() {
- return assetManager.downloader.maxConcurrency;
- }
- set maxConcurrency(value) {
- assetManager.downloader.maxConcurrency = value;
- }
- /** 下载时每帧可以启动的最大请求数 - 默认值为15 */
- get maxRequestsPerFrame() {
- return assetManager.downloader.maxRequestsPerFrame;
- }
- set maxRequestsPerFrame(value) {
- assetManager.downloader.maxRequestsPerFrame = value;
- }
- /** 失败重试次数 - 默认值为0 */
- get maxRetryCount() {
- return assetManager.downloader.maxRetryCount;
- }
- set maxRetryCount(value) {
- assetManager.downloader.maxRetryCount = value;
- }
- /** 重试的间隔时间,单位为毫秒 - 默认值为2000毫秒 */
- get retryInterval() {
- return assetManager.downloader.retryInterval;
- }
- set retryInterval(value) {
- assetManager.downloader.retryInterval = value;
- }
- //#endregion
- init(config) {
- this.cdn = config.enable;
- for (var _bundleName in config.packages) {
- this.bundles.set(_bundleName, config.packages[_bundleName]);
- }
- } //#region 加载远程资源
- /**
- * 加载远程资源
- * @param url 资源地址
- * @param options 资源参数,例:{ ext: ".png" }
- * @param onComplete 加载完成回调
- * @example
- var opt: IRemoteOptions = { ext: ".png" };
- var onComplete = (err: Error | null, data: ImageAsset) => {
- const texture = new Texture2D();
- texture.image = data;
-
- const spriteFrame = new SpriteFrame();
- spriteFrame.texture = texture;
-
- var sprite = this.sprite.addComponent(Sprite);
- sprite.spriteFrame = spriteFrame;
- }
- oops.res.loadRemote<ImageAsset>(this.url, opt, onComplete);
- */
- loadRemote(url) {
- var options = null;
- var onComplete = null;
- if ((arguments.length <= 1 ? 0 : arguments.length - 1) == 2) {
- options = arguments.length <= 1 ? undefined : arguments[1];
- onComplete = arguments.length <= 2 ? undefined : arguments[2];
- } else {
- onComplete = arguments.length <= 1 ? undefined : arguments[1];
- }
- assetManager.loadRemote(url, options, onComplete);
- } //#endregion
- //#region 资源包管理
- /**
- * 加载资源包
- * @param url 资源地址
- * @param v 资源MD5版本号
- * @example
- var serverUrl = "http://192.168.1.8:8080/"; // 服务器地址
- var md5 = "8e5c0"; // Cocos Creator 构建后的MD5字符
- await oops.res.loadBundle(serverUrl,md5);
- */
- loadBundle(url, v) {
- return new Promise((resolve, reject) => {
- assetManager.loadBundle(url, {
- version: v
- }, (err, bundle) => {
- if (err) {
- return error(err);
- }
- resolve(bundle);
- });
- });
- }
- /**
- * 释放资源包与包中所有资源
- * @param bundleName 资源地址
- */
- removeBundle(bundleName) {
- var bundle = assetManager.bundles.get(bundleName);
- if (bundle) {
- bundle.releaseAll();
- assetManager.removeBundle(bundle);
- }
- } //#endregion
- //#region 预加载资源
- /**
- * 加载一个资源
- * @param bundleName 远程包名
- * @param paths 资源路径
- * @param type 资源类型
- * @param onProgress 加载进度回调
- * @param onComplete 加载完成回调
- */
- preload(bundleName, paths, type, onProgress, onComplete) {
- var args = null;
- if (typeof paths === "string" || paths instanceof Array) {
- args = this.parseLoadResArgs(paths, type, onProgress, onComplete);
- args.bundle = bundleName;
- } else {
- args = this.parseLoadResArgs(bundleName, paths, type, onProgress);
- args.bundle = this.defaultBundleName;
- }
- args.preload = true;
- this.loadByArgs(args);
- }
- /**
- * 异步加载一个资源
- * @param bundleName 远程包名
- * @param paths 资源路径
- * @param type 资源类型
- */
- preloadAsync(bundleName, paths, type) {
- return new Promise((resolve, reject) => {
- this.preload(bundleName, paths, type, (err, data) => {
- if (err) {
- warn(err.message);
- }
- resolve(data);
- });
- });
- }
- /**
- * 预加载文件夹中的资源
- * @param bundleName 远程包名
- * @param dir 文件夹名
- * @param type 资源类型
- * @param onProgress 加载进度回调
- * @param onComplete 加载完成回调
- */
- preloadDir(bundleName, dir, type, onProgress, onComplete) {
- var args = null;
- if (typeof dir === "string") {
- args = this.parseLoadResArgs(dir, type, onProgress, onComplete);
- args.bundle = bundleName;
- } else {
- args = this.parseLoadResArgs(bundleName, dir, type, onProgress);
- args.bundle = this.defaultBundleName;
- }
- args.dir = args.paths;
- args.preload = true;
- this.loadByArgs(args);
- } //#endregion
- //#region 资源加载、获取、释放
- /**
- * 加载一个资源
- * @param bundleName 远程包名
- * @param paths 资源路径
- * @param type 资源类型
- * @param onProgress 加载进度回调
- * @param onComplete 加载完成回调
- * @example
- oops.res.load("spine_path", sp.SkeletonData, (err: Error | null, sd: sp.SkeletonData) => {
- });
- */
- load(bundleName, paths, type, onProgress, onComplete) {
- var args = null;
- if (typeof paths === "string" || paths instanceof Array) {
- args = this.parseLoadResArgs(paths, type, onProgress, onComplete);
- args.bundle = bundleName;
- } else {
- args = this.parseLoadResArgs(bundleName, paths, type, onProgress);
- args.bundle = this.defaultBundleName;
- }
- this.loadByArgs(args);
- }
- /**
- * 异步加载一个资源
- * @param bundleName 远程包名
- * @param paths 资源路径
- * @param type 资源类型
- */
- loadAsync(bundleName, paths, type) {
- return new Promise((resolve, reject) => {
- this.load(bundleName, paths, type, (err, asset) => {
- if (err) {
- warn(err.message);
- }
- resolve(asset);
- });
- });
- }
- /**
- * 加载文件夹中的资源
- * @param bundleName 远程包名
- * @param dir 文件夹名
- * @param type 资源类型
- * @param onProgress 加载进度回调
- * @param onComplete 加载完成回调
- * @example
- // 加载进度事件
- var onProgressCallback = (finished: number, total: number, item: any) => {
- console.log("资源加载进度", finished, total);
- }
- // 加载完成事件
- var onCompleteCallback = () => {
- console.log("资源加载完成");
- }
- oops.res.loadDir("game", onProgressCallback, onCompleteCallback);
- */
- loadDir(bundleName, dir, type, onProgress, onComplete) {
- var args = null;
- if (typeof dir === "string") {
- args = this.parseLoadResArgs(dir, type, onProgress, onComplete);
- args.bundle = bundleName;
- } else {
- args = this.parseLoadResArgs(bundleName, dir, type, onProgress);
- args.bundle = this.defaultBundleName;
- }
- args.dir = args.paths;
- this.loadByArgs(args);
- }
- /**
- * 通过资源相对路径释放资源
- * @param path 资源路径
- * @param bundleName 远程资源包名
- */
- release(path, bundleName) {
- if (bundleName === void 0) {
- bundleName = this.defaultBundleName;
- }
- var bundle = assetManager.getBundle(bundleName);
- if (bundle) {
- var asset = bundle.get(path);
- if (asset) {
- this.releasePrefabtDepsRecursively(asset);
- }
- }
- }
- /**
- * 通过相对文件夹路径删除所有文件夹中资源
- * @param path 资源文件夹路径
- * @param bundleName 远程资源包名
- */
- releaseDir(path, bundleName) {
- if (bundleName === void 0) {
- bundleName = this.defaultBundleName;
- }
- var bundle = assetManager.getBundle(bundleName);
- if (bundle) {
- var infos = bundle.getDirWithPath(path);
- if (infos) {
- infos.map(info => {
- this.releasePrefabtDepsRecursively(info.uuid);
- });
- }
- if (path == "" && bundleName != "resources") {
- assetManager.removeBundle(bundle);
- }
- }
- }
- /** 释放预制依赖资源 */
- releasePrefabtDepsRecursively(uuid) {
- if (uuid instanceof Asset) {
- uuid.decRef(); // assetManager.releaseAsset(uuid);
- } else {
- var asset = assetManager.assets.get(uuid);
- if (asset) {
- asset.decRef(); // assetManager.releaseAsset(asset);
- }
- }
- }
- /**
- * 获取资源
- * @param path 资源路径
- * @param type 资源类型
- * @param bundleName 远程资源包名
- */
- get(path, type, bundleName) {
- if (bundleName === void 0) {
- bundleName = this.defaultBundleName;
- }
- var bundle = assetManager.getBundle(bundleName);
- return bundle.get(path, type);
- } //#endregion
- parseLoadResArgs(paths, type, onProgress, onComplete) {
- var pathsOut = paths;
- var typeOut = type;
- var onProgressOut = onProgress;
- var onCompleteOut = onComplete;
- if (onComplete === undefined) {
- var isValidType = js.isChildClassOf(type, Asset);
- if (onProgress) {
- onCompleteOut = onProgress;
- if (isValidType) {
- onProgressOut = null;
- }
- } else if (onProgress === undefined && !isValidType) {
- onCompleteOut = type;
- onProgressOut = null;
- typeOut = null;
- }
- if (onProgress !== undefined && !isValidType) {
- onProgressOut = type;
- typeOut = null;
- }
- }
- return {
- paths: pathsOut,
- type: typeOut,
- onProgress: onProgressOut,
- onComplete: onCompleteOut
- };
- }
- loadByBundleAndArgs(bundle, args) {
- if (args.dir) {
- if (args.preload) {
- bundle.preloadDir(args.paths, args.type, args.onProgress, args.onComplete);
- } else {
- bundle.loadDir(args.paths, args.type, args.onProgress, args.onComplete);
- }
- } else {
- if (args.preload) {
- bundle.preload(args.paths, args.type, args.onProgress, args.onComplete);
- } else {
- bundle.load(args.paths, args.type, args.onProgress, args.onComplete);
- }
- }
- }
- loadByArgs(args) {
- var _this = this;
- return _asyncToGenerator(function* () {
- if (args.bundle) {
- var bundle = assetManager.bundles.get(args.bundle); // 获取缓存中的资源包
- if (bundle) {
- _this.loadByBundleAndArgs(bundle, args);
- } // 自动加载资源包
- else {
- var v = _this.cdn ? _this.bundles.get(args.bundle) : "";
- bundle = yield _this.loadBundle(args.bundle, v);
- if (bundle) _this.loadByBundleAndArgs(bundle, args);
- }
- } // 默认资源包
- else {
- _this.loadByBundleAndArgs(resources, args);
- }
- })();
- }
- /** 打印缓存中所有资源信息 */
- dump() {
- assetManager.assets.forEach((value, key) => {
- console.log(assetManager.assets.get(key));
- });
- console.log("\u5F53\u524D\u8D44\u6E90\u603B\u6570:" + assetManager.assets.count);
- }
- });
- _export("resLoader", resLoader = new ResLoader());
- _cclegacy._RF.pop();
- _crd = false;
- }
- };
- });
- //# sourceMappingURL=bd21ad9b63d9e3cba9431abca9625ce120a237d7.js.map
|