斷更了幾天憋活,主要是梳理整個(gè)和CCAssetManager相關(guān)的類岂津。CCAssetManager作為整個(gè)引擎的核心資源管理器。需要特別仔細(xì)的分析悦即。
const preprocess = require('./preprocess');//正常加載管線里面的加載任務(wù)function
const fetch = require('./fetch');//下載管線里面的請(qǐng)求任務(wù)
const Cache = require('./cache');//容器類
const helper = require('./helper');//核心長(zhǎng)uid和短uid的轉(zhuǎn)換吮成,判斷是scene還是prefab等f(wàn)unction
const releaseManager = require('./releaseManager');//資源釋放控制類
const dependUtil = require('./depend-util');//資源依賴管理類,單例
const load = require('./load');//正常加載管線里面的任務(wù)function
const Pipeline = require('./pipeline');//任務(wù)管線類
const Task = require('./task');//管線中的任務(wù)function
const RequestItem = require('./request-item');//加載任務(wù)里面的具體信息盐欺,uid赁豆,url,isNative等信息
const downloader = require('./downloader');//管理所有下載過(guò)程冗美,downloader 是個(gè)單例,比如下載次數(shù)等
const parser = require('./parser');//解析已下載的文件魔种,parser 是一個(gè)單例
const packManager = require('./pack-manager');//處理打包資源,包括拆包粉洼,加載节预,緩存等等叶摄,這是一個(gè)單例
const Bundle = require('./bundle');//一個(gè)包含一定數(shù)量資源(包括場(chǎng)景)的包,你可以加載安拟,預(yù)加載蛤吓,釋放此包內(nèi)的資源
const builtins = require('./builtins');//此模塊包含內(nèi)建資源,這是一個(gè)單例糠赦,
const factory = require('./factory');//一個(gè)工廠模式類会傲,批量創(chuàng)建了加載jpg,audio等不同方式的handler function
const { parse, combine } = require('./urlTransformer');//加載資源的info數(shù)據(jù)的轉(zhuǎn)換
const { parseParameters, asyncify } = require('./utilities');//公用函數(shù)eg拙泽,獲取依賴等
/*
以下是新建的幾個(gè)容器 變量,在shared.js文件創(chuàng)建的實(shí)例
bundles 一個(gè)類map 存儲(chǔ)bundle//默認(rèn)會(huì)存儲(chǔ) internal main resources 三個(gè)bundle
有遠(yuǎn)程加載的淌山,也會(huì)存儲(chǔ)到這個(gè)里面
*/
const { assets, files, parsed, pipeline, transformPipeline,
? ? fetchPipeline, RequestType, bundles, BuiltinBundleName } = require('./shared');
/**
* @module cc
*/
/**
* !#en
* This module controls asset's behaviors and information, include loading, releasing etc. it is a singleton
* All member can be accessed with `cc.assetManager`.
*
* !#zh
* 此模塊管理資源的行為和信息,包括加載顾瞻,釋放等泼疑,這是一個(gè)單例,所有成員能夠通過(guò) `cc.assetManager` 調(diào)用
*
* @class AssetManager
*/
function AssetManager () {
? ? this._preprocessPipe = preprocess;//沒(méi)發(fā)現(xiàn)有地方調(diào)用_preprocessPipe這個(gè)變量
? ? this._fetchPipe = fetch;//沒(méi)發(fā)現(xiàn)有地方調(diào)用_fetchPipe這個(gè)變量
? ? this._loadPipe = load;//沒(méi)發(fā)現(xiàn)有地方調(diào)用_loadPipe這個(gè)變量
? ? /**
? ? * !#en
? ? * Normal loading pipeline
? ? *
? ? * !#zh
? ? * 正常加載管線
? ? * 將兩個(gè)function添加到 pipeline實(shí)例里面的pipes數(shù)組里面
? ? * @property pipeline
? ? * @type {Pipeline}
? ? */
? ? this.pipeline = pipeline.append(preprocefss).append(load);
? ? /**
? ? * !#en
? ? * Fetching pipeline
? ? *
? ? * !#zh
? ? * 下載管線
? ? * 將兩個(gè)function添加到 pipeline實(shí)例里面的pipes數(shù)組里面
? ? * @property fetchPipeline
? ? * @type {Pipeline}
? ? */
? ? this.fetchPipeline = fetchPipeline.append(preprocess).append(fetch);
? ? /**
? ? * !#en
? ? * Url transformer
? ? *
? ? * !#zh
? ? * Url 轉(zhuǎn)換器
? ? * 將兩個(gè)function添加到 pipeline實(shí)例里面的pipes數(shù)組里面
? ? * @property transformPipeline
? ? * @type {Pipeline}
? ? */
? ? this.transformPipeline = transformPipeline.append(parse).append(combine);
? ? /**
? ? * !#en
? ? * The collection of bundle which is already loaded, you can remove cache with {{#crossLink "AssetManager/removeBundle:method"}}{{/crossLink}}
? ? *
? ? * !#zh
? ? * 已加載 bundle 的集合荷荤, 你能通過(guò) {{#crossLink "AssetManager/removeBundle:method"}}{{/crossLink}} 來(lái)移除緩存
? ? *
? ? * @property bundles
? ? * @type {Cache}
? ? * @typescript
? ? * bundles: AssetManager.Cache<AssetManager.Bundle>
? ? */
? ? this.bundles = bundles;
? ? /**
? ? * !#en
? ? * The collection of asset which is already loaded, you can remove cache with {{#crossLink "AssetManager/releaseAsset:method"}}{{/crossLink}}
? ? *
? ? * !#zh
? ? * 已加載資源的集合退渗, 你能通過(guò) {{#crossLink "AssetManager/releaseAsset:method"}}{{/crossLink}} 來(lái)移除緩存
? ? * assets 存儲(chǔ) texture2d等資源集合
? ? * @property assets
? ? * @type {Cache}
? ? * @typescript
? ? * assets: AssetManager.Cache<cc.Asset>
? ? */
? ? this.assets = assets;
? ? /*
? ? 沒(méi)找到有調(diào)用_files的地方,直接看shares的file注解
? ? */
? ? this._files = files;
? ? /*
? ? 沒(méi)找到對(duì)_parsed的引用和操作蕴纳,直接看shared.js
? ? parsed 是針對(duì)load資源的解析后存儲(chǔ)的 容器会油。
? ? */
? ? this._parsed = parsed;
? ? /*
? ? ? ? importBase: 'assets/others/import',
? ? ? ? nativeBase: 'assets/others/native'
? ? ? ? */
? ? this.generalImportBase = '';
? ? this.generalNativeBase = '';
? ? /**
? ? * !#en
? ? * Manage relationship between asset and its dependencies
? ? *
? ? * !#zh
? ? * 管理資源依賴關(guān)系
? ? *
? ? * @property dependUtil
? ? * @type {DependUtil}
? ? */
? ? this.dependUtil = dependUtil;
? ? /*
? ? 釋放的管理類
? ? */
? ? this._releaseManager = releaseManager;
? ? /**
? ? * !#en
? ? * Whether or not cache the loaded asset
? ? *
? ? * !#zh
? ? * 是否緩存已加載的資源
? ? *
? ? * @property cacheAsset
? ? * @type {boolean}
? ? */
? ? this.cacheAsset = true;
? ? /**
? ? * !#en
? ? * Whether or not load asset forcely, if it is true, asset will be loaded regardless of error
? ? *
? ? * !#zh
? ? * 是否強(qiáng)制加載資源, 如果為 true ,加載資源將會(huì)忽略報(bào)錯(cuò)
? ? * 【這個(gè)參數(shù)很有用袱蚓,具體項(xiàng)目很容易遇到加載報(bào)錯(cuò)的情況】
? ? * @property force
? ? * @type {boolean}
? ? */
? ? this.force = false;
? ? /**
? ? * !#en
? ? * Some useful function
? ? *
? ? * !#zh
? ? * 一些有用的方法
? ? * 包括uuid的長(zhǎng)短轉(zhuǎn)換钞啸,通過(guò)url返回uuid几蜻,
? ? * 判斷asset 是scene 還是prefab等公用方法
? ? * @property utils
? ? * @type {Helper}
? ? */
? ? this.utils = helper;
? ? /**
? ? * !#en
? ? * Manage all downloading task
? ? *
? ? * !#zh
? ? * 管理所有下載任務(wù)
? ? *
? ? * @property downloader
? ? * @type {Downloader}
? ? */
? ? this.downloader = downloader;
? ? /**
? ? * !#en
? ? * Manage all parsing task
? ? *
? ? * !#zh
? ? * 管理所有解析任務(wù)
? ? *
? ? * @property parser
? ? * @type {Parser}
? ? */
? ? this.parser = parser;
? ? /**
? ? * !#en
? ? * Manage internal asset
? ? *
? ? * !#zh
? ? * 管理內(nèi)置資源
? ? *
? ? * @property builtins
? ? * @type {Builtins}
? ? */
? ? this.builtins = builtins;
? ? /**
? ? * !#en
? ? * Manage all packed asset
? ? *
? ? * !#zh
? ? * 管理所有合并后的資源
? ? *
? ? * @property packManager
? ? * @type {PackManager}
? ? */
? ? this.packManager = packManager;
? ? this.factory = factory;
? ? /**
? ? * !#en
? ? * Cache manager is a module which controls all caches downloaded from server in non-web platform.
? ? *
? ? * !#zh
? ? * 緩存管理器是一個(gè)模塊喇潘,在非 WEB 平臺(tái)上,用于管理所有從服務(wù)器上下載下來(lái)的緩存
? ? *
? ? * @property cacheManager
? ? * @type {cc.AssetManager.CacheManager}
? ? * @typescript
? ? * cacheManager: cc.AssetManager.CacheManager|null
? ? */
? ? this.cacheManager = null;
? ? /**
? ? * !#en
? ? * The preset of options
? ? *
? ? * !#zh
? ? * 可選參數(shù)的預(yù)設(shè)集
? ? *
? ? * @property presets
? ? * @type {Object}
? ? * @typescript
? ? * presets: Record<string, Record<string, any>>
? ? */
? ? this.presets = {
? ? ? ? 'default': {
? ? ? ? ? ? priority: 0,
? ? ? ? },
? ? ? ? 'preload': {
? ? ? ? ? ? maxConcurrency: 2,
? ? ? ? ? ? maxRequestsPerFrame: 2,
? ? ? ? ? ? priority: -1,
? ? ? ? },
? ? ? ? 'scene': {
? ? ? ? ? ? maxConcurrency: 8,
? ? ? ? ? ? maxRequestsPerFrame: 8,
? ? ? ? ? ? priority: 1,
? ? ? ? },
? ? ? ? 'bundle': {
? ? ? ? ? ? maxConcurrency: 8,
? ? ? ? ? ? maxRequestsPerFrame: 8,
? ? ? ? ? ? priority: 2,
? ? ? ? },
? ? ? ? 'remote': {
? ? ? ? ? ? maxRetryCount: 4
? ? ? ? },
? ? ? ? 'script': {
? ? ? ? ? ? priority: 2
? ? ? ? }
? ? }
}
/*
靜態(tài)類的引用
*/
AssetManager.Pipeline = Pipeline;
AssetManager.Task = Task;
AssetManager.Cache = Cache;
AssetManager.RequestItem = RequestItem;
AssetManager.Bundle = Bundle;
AssetManager.BuiltinBundleName = BuiltinBundleName;
AssetManager.prototype = {
? ? constructor: AssetManager,
? ? /**
? ? * !#en
? ? * The builtin 'main' bundle
? ? *
? ? * !#zh
? ? * 內(nèi)置 main 包
? ? *
? ? * @property main
? ? * @readonly
? ? * @type {Bundle}
? ? */
? ? get main () {
? ? ? ? return bundles.get(BuiltinBundleName.MAIN);
? ? },
? ? /**
? ? * !#en
? ? * The builtin 'resources' bundle
? ? *
? ? * !#zh
? ? * 內(nèi)置 resources 包
? ? *
? ? * @property resources
? ? * @readonly
? ? * @type {Bundle}
? ? */
? ? get resources () {
? ? ? ? return bundles.get(BuiltinBundleName.RESOURCES);////獲取到的是 resources目錄針對(duì)的bundle對(duì)象
? ? },
? ? /**
? ? * !#en
? ? * The builtin 'internal' bundle
? ? *
? ? * !#zh
? ? * 內(nèi)置 internal 包
? ? *
? ? * @property internal
? ? * @readonly
? ? * @type {Bundle}
? ? */
? ? get internal () {
? ? ? ? return bundles.get(BuiltinBundleName.INTERNAL);
? ? },
? ? /**
? ? * !#en
? ? * Initialize assetManager with options
? ? *
? ? * !#zh
? ? * 初始化資源管理器
? ? *
? ? * @method init
? ? * @param {Object} options
? ? *
? ? * @typescript
? ? * init(options: Record<string, any>): void
? ? */
? ? init (options) {
? ? ? ? options = options || Object.create(null);//創(chuàng)建一個(gè)類似map的容器
? ? ? ? this._files.clear();
? ? ? ? this._parsed.clear();
? ? ? ? this._releaseManager.init();//釋放資源管理器初始化
? ? ? ? /*
? ? ? ? assets 存儲(chǔ) texture2d等asset資源
? ? ? ? */
? ? ? ? this.assets.clear();
? ? ? ? /*
? ? ? ? bundles存儲(chǔ)的一個(gè)包梭稚,里面是集成的很多內(nèi)容颖低,比如可以把代碼和資源集成為一個(gè)bundle
? ? ? ? */
? ? ? ? this.bundles.clear();
? ? ? ? this.packManager.init();
? ? ? ? /*
? ? ? ? /* 這個(gè)是md5的后綴
? ? bundleVers: { internal: "f1413", resources: "d99d6", main: "d3b73" }//Bundle 的 md5 值
? ? ? */ */
? ? ? ? this.downloader.init(options.bundleVers);//
? ? ? ? this.parser.init();
? ? ? ? this.dependUtil.init();
? ? ? ? /*
? ? ? ? 查看 urlTransformer.js 為了轉(zhuǎn)換ttf路徑名稱 設(shè)置的這兩個(gè)個(gè)參數(shù)
? ? ? ? 預(yù)覽的時(shí)候這兩個(gè)參數(shù)的默認(rèn)值
? ? ? ? importBase: 'assets/others/import',
? ? ? ? nativeBase: 'assets/others/native'
? ? ? ? web運(yùn)行--"assets/internal/import"
? ? ? ? ? ? */
? ? ? ? this.generalImportBase = options.importBase;
? ? ? ? this.generalNativeBase = options.nativeBase;
? ? },
? ? /**
? ? * !#en
? ? * Get the bundle which has been loaded
? ? *
? ? * !#zh
? ? * 獲取已加載的分包
? ? *
? ? * @method getBundle
? ? * @param {String} name - The name of bundle
? ? * @return {Bundle} - The loaded bundle
? ? *
? ? * @example
? ? * // ${project}/assets/test1
? ? * cc.assetManager.getBundle('test1');
? ? *
? ? * cc.assetManager.getBundle('resources');
? ? *
? ? * @typescript
? ? * getBundle (name: string): cc.AssetManager.Bundle
? ? */
? ? getBundle (name) {
? ? ? ? return bundles.get(name);
? ? },
? ? /**
? ? * !#en
? ? * Remove this bundle. NOTE: The asset whthin this bundle will not be released automatically, you can call {{#crossLink "Bundle/releaseAll:method"}}{{/crossLink}} manually before remove it if you need
? ? *
? ? * !#zh
? ? * 移除此包, 注意:這個(gè)包內(nèi)的資源不會(huì)自動(dòng)釋放, 如果需要的話你可以在摧毀之前手動(dòng)調(diào)用 {{#crossLink "Bundle/releaseAll:method"}}{{/crossLink}} 進(jìn)行釋放
? ? *
? ? * @method removeBundle
? ? * @param {Bundle} bundle - The bundle to be removed
? ? *
? ? * @typescript
? ? * removeBundle(bundle: cc.AssetManager.Bundle): void
? ? */
? ? removeBundle (bundle) {
? ? ? ? bundle._destroy();
? ? ? ? bundles.remove(bundle.name);
? ? },
? ? /**
? ? * !#en
? ? * General interface used to load assets with a progression callback and a complete callback. You can achieve almost all effect you want with combination of `requests` and `options`.
? ? * It is highly recommended that you use more simple API, such as `load`, `loadDir` etc. Every custom parameter in `options` will be distribute to each of `requests`.
? ? * if request already has same one, the parameter in request will be given priority. Besides, if request has dependencies, `options` will distribute to dependencies too.
? ? * Every custom parameter in `requests` will be tranfered to handler of `downloader` and `parser` as `options`.
? ? * You can register you own handler downloader or parser to collect these custom parameters for some effect.
? ? *
? ? * Reserved Keyword: `uuid`, `url`, `path`, `dir`, `scene`, `type`, `priority`, `preset`, `audioLoadMode`, `ext`, `bundle`, `onFileProgress`, `maxConcurrency`, `maxRequestsPerFrame`
? ? * `maxRetryCount`, `version`, `responseType`, `withCredentials`, `mimeType`, `timeout`, `header`, `reload`, `cacheAsset`, `cacheEnabled`,
? ? * Please DO NOT use these words as custom options!
? ? *
? ? * !#zh
? ? * 通用加載資源接口,可傳入進(jìn)度回調(diào)以及完成回調(diào)弧烤,通過(guò)組合 `request` 和 `options` 參數(shù)忱屑,幾乎可以實(shí)現(xiàn)和擴(kuò)展所有想要的加載效果。非常建議你使用更簡(jiǎn)單的API暇昂,例如 `load`莺戒、`loadDir` 等。
? ? * `options` 中的自定義參數(shù)將會(huì)分發(fā)到 `requests` 的每一項(xiàng)中急波,如果request中已存在同名的參數(shù)則以 `requests` 中為準(zhǔn)从铲,同時(shí)如果有其他
? ? * 依賴資源,則 `options` 中的參數(shù)會(huì)繼續(xù)向依賴項(xiàng)中分發(fā)澄暮。request中的自定義參數(shù)都會(huì)以 `options` 形式傳入加載流程中的 `downloader`, `parser` 的方法中, 你可以
? ? * 擴(kuò)展 `downloader`, `parser` 收集參數(shù)完成想實(shí)現(xiàn)的效果名段。
? ? *
? ? * 保留關(guān)鍵字: `uuid`, `url`, `path`, `dir`, `scene`, `type`, `priority`, `preset`, `audioLoadMode`, `ext`, `bundle`, `onFileProgress`, `maxConcurrency`, `maxRequestsPerFrame`
? ? * `maxRetryCount`, `version`, `responseType`, `withCredentials`, `mimeType`, `timeout`, `header`, `reload`, `cacheAsset`, `cacheEnabled`,
? ? * 請(qǐng)不要使用這些字段為自定義參數(shù)!
? ? *
? ? * @method loadAny
? ? * @param {string|string[]|Object|Object[]} requests - The request you want to load
? ? * @param {Object} [options] - Optional parameters
? ? * @param {Function} [onProgress] - Callback invoked when progression change
? ? * @param {Number} onProgress.finished - The number of the items that are already completed
? ? * @param {Number} onProgress.total - The total number of the items
? ? * @param {RequestItem} onProgress.item - The current request item
? ? * @param {Function} [onComplete] - Callback invoked when finish loading
? ? * @param {Error} onComplete.err - The error occured in loading process.
? ? * @param {Object} onComplete.data - The loaded content
? ? *
? ? * @example
? ? * cc.assetManager.loadAny({url: 'http://example.com/a.png'}, (err, img) => cc.log(img));
? ? * cc.assetManager.loadAny(['60sVXiTH1D/6Aft4MRt9VC'], (err, assets) => cc.log(assets));
? ? * cc.assetManager.loadAny([{ uuid: '0cbZa5Y71CTZAccaIFluuZ'}, {url: 'http://example.com/a.png'}], (err, assets) => cc.log(assets));
? ? * cc.assetManager.downloader.register('.asset', (url, options, onComplete) => {
? ? *? ? ? url += '?userName=' + options.userName + "&password=" + options.password;
? ? *? ? ? cc.assetManager.downloader.downloadFile(url, null, onComplete);
? ? * });
? ? * cc.assetManager.parser.register('.asset', (file, options, onComplete) => {
? ? *? ? ? var json = JSON.parse(file);
? ? *? ? ? var skin = json[options.skin];
? ? *? ? ? var model = json[options.model];
? ? *? ? ? onComplete(null, {skin, model});
? ? * });
? ? * cc.assetManager.loadAny({ url: 'http://example.com/my.asset', skin: 'xxx', model: 'xxx', userName: 'xxx', password: 'xxx' });
? ? *
? ? * @typescript
? ? * loadAny(requests: string | string[] | Record<string, any> | Record<string, any>[], options: Record<string, any>, onProgress: (finished: number, total: number, item: cc.AssetManager.RequestItem) => void, onComplete: (err: Error, data: any) => void): void
? ? * loadAny(requests: string | string[] | Record<string, any> | Record<string, any>[], options: Record<string, any>, onComplete: (err: Error, data: any) => void): void
? ? * loadAny(requests: string | string[] | Record<string, any> | Record<string, any>[], options: Record<string, any>): void
? ? * loadAny(requests: string | string[] | Record<string, any> | Record<string, any>[], onProgress: (finished: number, total: number, item: cc.AssetManager.RequestItem) => void, onComplete: (err: Error, data: any) => void): void
? ? * loadAny(requests: string | string[] | Record<string, any> | Record<string, any>[], onComplete: (err: Error, data: any) => void): void
? ? * loadAny(requests: string | string[] | Record<string, any> | Record<string, any>[]): void
? ? */
? ? /*
? ? requests參數(shù): load任務(wù)的源
? ? eg:load插件script的時(shí)候這里就是個(gè)array
? ? [
? ? "/plugins/ccservices-scripts/cocos-analytics-init.js",
? ? "/plugins/ccservices-scripts/cocosAnalytics.min.2.2.1.js",
? ? "/plugins/assets/script/Lib/news.js"
? ? ]
? ? option: {preset: "script"__requestType__:"url"}
? ? */
? ? loadAny (requests, options, onProgress, onComplete) {
? ? ? ? var { options, onProgress, onComplete } = parseParameters(options, onProgress, onComplete);
? ? ? ? options.preset = options.preset || 'default';//加載的類型 比如uid url dir path
? ? ? ? //新建一個(gè)任務(wù)阱扬,
? ? ? ? let task = new Task({input: requests, onProgress, onComplete: asyncify(onComplete), options});
? ? //? ? 添加到normal任務(wù)管線 【一共三個(gè)管線,這個(gè)是normal】伸辟,異步執(zhí)行任務(wù)
? ? ? ? pipeline.async(task);//pipeline里面的pipes數(shù)組 有具體的pipe麻惶,
? ? },
? ? /**
? ? * !#en
? ? * General interface used to preload assets with a progression callback and a complete callback.It is highly recommended that you use more simple API, such as `preloadRes`, `preloadResDir` etc.
? ? * Everything about preload is just likes `cc.assetManager.loadAny`, the difference is `cc.assetManager.preloadAny` will only download asset but not parse asset. You need to invoke `cc.assetManager.loadAny(preloadTask)`
? ? * to finish loading asset
? ? *
? ? * !#zh
? ? * 通用預(yù)加載資源接口,可傳入進(jìn)度回調(diào)以及完成回調(diào)信夫,非常建議你使用更簡(jiǎn)單的 API 窃蹋,例如 `preloadRes`, `preloadResDir` 等。`preloadAny` 和 `loadAny` 幾乎一樣静稻,區(qū)別在于 `preloadAny` 只會(huì)下載資源脐彩,不會(huì)去解析資源,你需要調(diào)用 `cc.assetManager.loadAny(preloadTask)`
? ? * 來(lái)完成資源加載姊扔。
? ? *
? ? * @method preloadAny
? ? * @param {string|string[]|Object|Object[]} requests - The request you want to preload
? ? * @param {Object} [options] - Optional parameters
? ? * @param {Function} [onProgress] - Callback invoked when progression change
? ? * @param {Number} onProgress.finished - The number of the items that are already completed
? ? * @param {Number} onProgress.total - The total number of the items
? ? * @param {RequestItem} onProgress.item - The current request item
? ? * @param {Function} [onComplete] - Callback invoked when finish preloading
? ? * @param {Error} onComplete.err - The error occured in preloading process.
? ? * @param {RequestItem[]} onComplete.items - The preloaded content
? ? *
? ? * @example
? ? * cc.assetManager.preloadAny('0cbZa5Y71CTZAccaIFluuZ', (err) => cc.assetManager.loadAny('0cbZa5Y71CTZAccaIFluuZ'));
? ? *
? ? * @typescript
? ? * preloadAny(requests: string | string[] | Record<string, any> | Record<string, any>[], options: Record<string, any>, onProgress: (finished: number, total: number, item: cc.AssetManager.RequestItem) => void, onComplete: (err: Error, items: cc.AssetManager.RequestItem[]) => void): void
? ? * preloadAny(requests: string | string[] | Record<string, any> | Record<string, any>[], options: Record<string, any>, onComplete: (err: Error, items: cc.AssetManager.RequestItem[]) => void): void
? ? * preloadAny(requests: string | string[] | Record<string, any> | Record<string, any>[], onProgress: (finished: number, total: number, item: cc.AssetManager.RequestItem) => void, onComplete: (err: Error, items: cc.AssetManager.RequestItem[]) => void): void
? ? * preloadAny(requests: string | string[] | Record<string, any> | Record<string, any>[], onComplete: (err: Error, items: cc.AssetManager.RequestItem[]) => void): void
? ? * preloadAny(requests: string | string[] | Record<string, any> | Record<string, any>[], options: Record<string, any>): void
? ? * preloadAny(requests: string | string[] | Record<string, any> | Record<string, any>[]): void
? ? */
? ? preloadAny (requests, options, onProgress, onComplete) {
? ? ? ? var { options, onProgress, onComplete } = parseParameters(options, onProgress, onComplete);
? ? ? ? options.preset = options.preset || 'preload';
? ? ? ? var task = new Task({input: requests, onProgress, onComplete: asyncify(onComplete), options});
? ? ? ? fetchPipeline.async(task);
? ? },
? ? /**
? ? * !#en
? ? * Load native file of asset, if you check the option 'Async Load Assets', you may need to load native file with this before you use the asset
? ? *
? ? * !#zh
? ? * 加載資源的原生文件惠奸,如果你勾選了'延遲加載資源'選項(xiàng),你可能需要在使用資源之前調(diào)用此方法來(lái)加載原生文件
? ? *
? ? * @method postLoadNative
? ? * @param {Asset} asset - The asset
? ? * @param {Object} [options] - Some optional parameters
? ? * @param {Function} [onComplete] - Callback invoked when finish loading
? ? * @param {Error} onComplete.err - The error occured in loading process.
? ? *
? ? * @example
? ? * cc.assetManager.postLoadNative(texture, (err) => console.log(err));
? ? *
? ? * @typescript
? ? * postLoadNative(asset: cc.Asset, options: Record<string, any>, onComplete: (err: Error) => void): void
? ? * postLoadNative(asset: cc.Asset, options: Record<string, any>): void
? ? * postLoadNative(asset: cc.Asset, onComplete: (err: Error) => void): void
? ? * postLoadNative(asset: cc.Asset): void
? ? */
? ? postLoadNative (asset, options, onComplete) {
? ? ? ? if (!(asset instanceof cc.Asset)) throw new Error('input is not asset');
? ? ? ? var { options, onComplete } = parseParameters(options, undefined, onComplete);
? ? ? ? if (!asset._native || asset._nativeAsset) {
? ? ? ? ? ? return asyncify(onComplete)(null);
? ? ? ? }
? ? ? ? var depend = dependUtil.getNativeDep(asset._uuid);
? ? ? ? if (depend) {
? ? ? ? ? ? if (!bundles.has(depend.bundle)) {
? ? ? ? ? ? ? ? var bundle = bundles.find(function (bundle) {
? ? ? ? ? ? ? ? ? ? return bundle.getAssetInfo(asset._uuid);
? ? ? ? ? ? ? ? });
? ? ? ? ? ? ? ? if (bundle) {
? ? ? ? ? ? ? ? ? ? depend.bundle = bundle.name;
? ? ? ? ? ? ? ? }
? ? ? ? ? ? }
? ? ? ? ? ? this.loadAny(depend, options, function (err, native) {
? ? ? ? ? ? ? ? if (!err) {
? ? ? ? ? ? ? ? ? ? !asset._nativeAsset && (asset._nativeAsset = native);
? ? ? ? ? ? ? ? }
? ? ? ? ? ? ? ? else {
? ? ? ? ? ? ? ? ? ? cc.error(err.message, err.stack);
? ? ? ? ? ? ? ? }
? ? ? ? ? ? ? ? onComplete && onComplete(err);
? ? ? ? ? ? });
? ? ? ? }
? ? },
? ? /**
? ? * !#en
? ? * Load remote asset with url, such as audio, image, text and so on.
? ? *
? ? * !#zh
? ? * 使用 url 加載遠(yuǎn)程資源恰梢,例如音頻佛南,圖片,文本等等嵌言。
? ? *
? ? * @method loadRemote
? ? * @param {string} url - The url of asset
? ? * @param {Object} [options] - Some optional parameters
? ? * @param {cc.AudioClip.LoadMode} [options.audioLoadMode] - Indicate which mode audio you want to load
? ? * @param {Function} [onComplete] - Callback invoked when finish loading
? ? * @param {Error} onComplete.err - The error occured in loading process.
? ? * @param {Asset} onComplete.asset - The loaded texture
? ? *
? ? * @example
? ? * cc.assetManager.loadRemote('http://www.cloud.com/test1.jpg', (err, texture) => console.log(err));
? ? * cc.assetManager.loadRemote('http://www.cloud.com/test2.mp3', (err, audioClip) => console.log(err));
? ? *
? ? * @typescript
? ? * loadRemote<T extends cc.Asset>(url: string, options: Record<string, any>, onComplete: (err: Error, asset: T) => void): void
? ? * loadRemote<T extends cc.Asset>(url: string, options: Record<string, any>): void
? ? * loadRemote<T extends cc.Asset>(url: string, onComplete: (err: Error, asset: T) => void): void
? ? * loadRemote<T extends cc.Asset>(url: string): void
? ? */
? ? loadRemote (url, options, onComplete) {
? ? ? ? var { options, onComplete } = parseParameters(options, undefined, onComplete);
? ? ? ? options.__isNative__ = true;
? ? ? ? options.preset = options.preset || 'remote';
? ? ? ? this.loadAny({url}, options, null, function (err, data) {
? ? ? ? ? ? if (err) {
? ? ? ? ? ? ? ? cc.error(err.message, err.stack);
? ? ? ? ? ? ? ? onComplete && onComplete(err, null);
? ? ? ? ? ? }
? ? ? ? ? ? else {
? ? ? ? ? ? ? ? factory.create(url, data, options.ext || cc.path.extname(url), options, onComplete);
? ? ? ? ? ? }
? ? ? ? });
? ? },
? ? /**
? ? * !#en
? ? * Load script
? ? *
? ? * !#zh
? ? * 加載腳本
? ? *
? ? * @method loadScript
? ? * @param {string|string[]} url - Url of the script
? ? * @param {Object} [options] - Some optional paramters
? ? * @param {boolean} [options.isAsync] - Indicate whether or not loading process should be async
? ? * @param {Function} [onComplete] - Callback when script loaded or failed
? ? * @param {Error} onComplete.err - The occurred error, null indicetes success
? ? *
? ? * @example
? ? * loadScript('http://localhost:8080/index.js', null, (err) => console.log(err));
? ? *
? ? * @typescript
? ? * loadScript(url: string|string[], options: Record<string, any>, onComplete: (err: Error) => void): void;
? ? * loadScript(url: string|string[], options: Record<string, any>): void;
? ? * loadScript(url: string|string[], onComplete: (err: Error) => void): void;
? ? * loadScript(url: string|string[]): void;
? ? */
? ? /*
? ? eg:url 是個(gè)string 數(shù)組
? ? [
? ? "/plugins/ccservices-scripts/cocos-analytics-init.js",
? ? "/plugins/ccservices-scripts/cocosAnalytics.min.2.2.1.js",
? ? "/plugins/assets/script/Lib/news.js"
? ? ]
? ? options 是cb
? ? onComplete 是 undefined
? ? */
? ? loadScript (url, options, onComplete) {
? ? ? ? /*
? ? ? ? 交換參數(shù)嗅回,當(dāng)?shù)诙€(gè)參數(shù)不是progress func的時(shí)候重新定義obj進(jìn)行交叉互換
? ? ? ? */
? ? ? ? var { options, onComplete } = parseParameters(options, undefined, onComplete);
? ? ? ? /*
? ? ? ? 執(zhí)行轉(zhuǎn)換后 options為一個(gè)空對(duì)象了,onComplete 變成了cb摧茴,傳入的第二個(gè)參數(shù) */
? ? ? ? options.__requestType__ = RequestType.URL;//靜態(tài)變量 'url'
? ? ? ? options.preset = options.preset || 'script';
? ? ? ? this.loadAny(url, options, onComplete);
? ? },
? ? /**
? ? * !#en
? ? * load bundle
? ? *
? ? * !#zh
? ? * 加載資源包
? ? *
? ? * @method loadBundle
? ? * @param {string} nameOrUrl - The name or root path of bundle
? ? * @param {Object} [options] - Some optional paramter, same like downloader.downloadFile
? ? * @param {string} [options.version] - The version of this bundle, you can check config.json in this bundle
? ? * @param {Function} [onComplete] - Callback when bundle loaded or failed
? ? * @param {Error} onComplete.err - The occurred error, null indicetes success
? ? * @param {Bundle} onComplete.bundle - The loaded bundle
? ? *
? ? * @example
? ? * loadBundle('http://localhost:8080/test', null, (err, bundle) => console.log(err));
? ? *
? ? * @typescript
? ? * loadBundle(nameOrUrl: string, options: Record<string, any>, onComplete: (err: Error, bundle: cc.AssetManager.Bundle) => void): void
? ? * loadBundle(nameOrUrl: string, options: Record<string, any>): void
? ? * loadBundle(nameOrUrl: string, onComplete: (err: Error, bundle: cc.AssetManager.Bundle) => void): void
? ? * loadBundle(nameOrUrl: string): void
? ? */
? ? loadBundle (nameOrUrl, options, onComplete) {
? ? ? ? var { options, onComplete } = parseParameters(options, undefined, onComplete);
? ? ? ? let bundleName = cc.path.basename(nameOrUrl);
? ? ? ? if (this.bundles.has(bundleName)) {
? ? ? ? ? ? return asyncify(onComplete)(null, this.getBundle(bundleName));
? ? ? ? }
? ? ? ? options.preset = options.preset || 'bundle';
? ? ? ? options.ext = 'bundle';
? ? ? ? this.loadRemote(nameOrUrl, options, onComplete);
? ? },
? ? /**
? ? * !#en
? ? * Release asset and it's dependencies.
? ? * This method will not only remove the cache of the asset in assetManager, but also clean up its content.
? ? * For example, if you release a texture, the texture asset and its gl texture data will be freed up.
? ? * Notice, this method may cause the texture to be unusable, if there are still other nodes use the same texture, they may turn to black and report gl errors.
? ? *
? ? * !#zh
? ? * 釋放資源以及其依賴資源, 這個(gè)方法不僅會(huì)從 assetManager 中刪除資源的緩存引用绵载,還會(huì)清理它的資源內(nèi)容。
? ? * 比如說(shuō)苛白,當(dāng)你釋放一個(gè) texture 資源娃豹,這個(gè) texture 和它的 gl 貼圖數(shù)據(jù)都會(huì)被釋放。
? ? * 注意购裙,這個(gè)函數(shù)可能會(huì)導(dǎo)致資源貼圖或資源所依賴的貼圖不可用懂版,如果場(chǎng)景中存在節(jié)點(diǎn)仍然依賴同樣的貼圖,它們可能會(huì)變黑并報(bào) GL 錯(cuò)誤躏率。
? ? *
? ? * @method releaseAsset
? ? * @param {Asset} asset - The asset to be released
? ? *
? ? * @example
? ? * // release a texture which is no longer need
? ? * cc.assetManager.releaseAsset(texture);
? ? *
? ? * @typescript
? ? * releaseAsset(asset: cc.Asset): void
? ? */
? ? releaseAsset (asset) {
? ? ? ? releaseManager.tryRelease(asset, true);
? ? },
? ? /**
? ? * !#en
? ? * Release all unused assets. Refer to {{#crossLink "AssetManager/releaseAsset:method"}}{{/crossLink}} for detailed informations.
? ? *
? ? * !#zh
? ? * 釋放所有沒(méi)有用到的資源躯畴。詳細(xì)信息請(qǐng)參考 {{#crossLink "AssetManager/releaseAsset:method"}}{{/crossLink}}
? ? *
? ? * @method releaseUnusedAssets
? ? * @private
? ? *
? ? * @typescript
? ? * releaseUnusedAssets(): void
? ? */
? ? releaseUnusedAssets () {
? ? ? ? assets.forEach(function (asset) {
? ? ? ? ? ? releaseManager.tryRelease(asset);
? ? ? ? });
? ? },
? ? /**
? ? * !#en
? ? * Release all assets. Refer to {{#crossLink "AssetManager/releaseAsset:method"}}{{/crossLink}} for detailed informations.
? ? *
? ? * !#zh
? ? * 釋放所有資源。詳細(xì)信息請(qǐng)參考 {{#crossLink "AssetManager/releaseAsset:method"}}{{/crossLink}}
? ? *
? ? * @method releaseAll
? ? *
? ? * @typescript
? ? * releaseAll(): void
? ? */
? ? releaseAll () {
? ? ? ? assets.forEach(function (asset) {
? ? ? ? ? ? releaseManager.tryRelease(asset, true);
? ? ? ? });
? ? },
? ? _transform (input, options) {
? ? ? ? var subTask = Task.create({input, options});
? ? ? ? var urls = [];
? ? ? ? try {
? ? ? ? ? ? var result = transformPipeline.sync(subTask);
? ? ? ? ? ? for (var i = 0, l = result.length; i < l; i++) {
? ? ? ? ? ? ? ? var item = result[i];
? ? ? ? ? ? ? ? var url = item.url;
? ? ? ? ? ? ? ? item.recycle();
? ? ? ? ? ? ? ? urls.push(url);
? ? ? ? ? ? }
? ? ? ? }
? ? ? ? catch (e) {
? ? ? ? ? ? for (var i = 0, l = subTask.output.length; i < l; i++) {
? ? ? ? ? ? ? ? subTask.output[i].recycle();
? ? ? ? ? ? }
? ? ? ? ? ? cc.error(e.message, e.stack);
? ? ? ? }
? ? ? ? subTask.recycle();
? ? ? ? return urls.length > 1 ? urls : urls[0];
? ? }
};
cc.AssetManager = AssetManager;
/**
* @module cc
*/
/**
* @property assetManager
* @type {AssetManager}
*/
cc.assetManager = new AssetManager();
/*
看下面定義薇芝,
cc.resources 和 cc.assetManager.resources 等價(jià)
*/
Object.defineProperty(cc, 'resources', {
? ? /**
? ? * !#en
? ? * cc.resources is a bundle and controls all asset under assets/resources
? ? *
? ? * !#zh
? ? * cc.resources 是一個(gè) bundle蓬抄,用于管理所有在 assets/resources 下的資源
? ? *
? ? * @property resources
? ? * @readonly
? ? * @type {AssetManager.Bundle}
? ? */
? ? get () {
? ? ? ? return bundles.get(BuiltinBundleName.RESOURCES);//獲取到的是 resources目錄針對(duì)的bundle對(duì)象
? ? }
});
module.exports = cc.assetManager;
/**
* !#en
* This module controls asset's behaviors and information, include loading, releasing etc.
* All member can be accessed with `cc.assetManager`. All class or enum can be accessed with `cc.AssetManager`
*
* !#zh
* 此模塊管理資源的行為和信息,包括加載夯到,釋放等嚷缭,所有成員能夠通過(guò) `cc.assetManager` 調(diào)用. 所有類型或枚舉能通過(guò) `cc.AssetManager` 訪問(wèn)
*
* @module cc.AssetManager
*/