AssetManager 类型

模块: cc

此模块管理资源的行为和信息,包括加载,释放等,这是一个单例,所有成员能够通过 cc.assetManager 调用

索引

属性(properties)
  • pipeline Pipeline 正常加载管线
  • fetchPipeline Pipeline 下载管线
  • transformPipeline Pipeline Url 转换器
  • bundles Cache 已加载 bundle 的集合, 你能通过 removeBundle 来移除缓存
  • assets Cache 已加载资源的集合, 你能通过 releaseAsset 来移除缓存
  • dependUtil DependUtil 管理资源依赖关系
  • cacheAsset Boolean 是否缓存已加载的资源
  • force Boolean 是否强制加载资源, 如果为 true ,加载资源将会忽略报错
  • utils Helper 一些有用的方法
  • downloader Downloader 管理所有下载任务
  • parser Parser 管理所有解析任务
  • builtins Builtins 管理内置资源
  • packManager PackManager 管理所有合并后的资源
  • cacheManager cc.AssetManager.CacheManager 缓存管理器是一个模块,在非 WEB 平台上,用于管理所有从服务器上下载下来的缓存
  • presets Object 可选参数的预设集
  • main Bundle 内置 main 包
  • resources Bundle 内置 resources 包
  • internal Bundle 内置 internal 包
方法
  • init 初始化资源管理器
  • getBundle 获取已加载的分包
  • removeBundle 移除此包, 注意:这个包内的资源不会自动释放, 如果需要的话你可以在摧毁之前手动调用 releaseAll 进行释放
  • loadAny 通用加载资源接口,可传入进度回调以及完成回调,通过组合 request 和 options 参数,几乎可以实现和扩展所有想要的加载效果。
  • preloadAny 通用预加载资源接口,可传入进度回调以及完成回调,非常建议你使用更简单的 API ,例如 preloadRes, preloadResDir 等。
  • postLoadNative 加载资源的原生文件,如果你勾选了'延迟加载资源'选项,你可能需要在使用资源之前调用此方法来加载原生文件
  • loadRemote 使用 url 加载远程资源,例如音频,图片,文本等等。
  • loadScript 加载脚本
  • loadBundle 加载资源包
  • releaseAsset 释放资源以及其依赖资源, 这个方法不仅会从 assetManager 中删除资源的缓存引用,还会清理它的资源内容。
  • releaseUnusedAssets 释放所有没有用到的资源。
  • releaseAll 释放所有资源。

Details

属性(properties)

pipeline

正常加载管线

meta description
类型 Pipeline
定义于 cocos2d/core/asset-manager/CCAssetManager.js:71
fetchPipeline

下载管线

meta description
类型 Pipeline
定义于 cocos2d/core/asset-manager/CCAssetManager.js:83
transformPipeline

Url 转换器

meta description
类型 Pipeline
定义于 cocos2d/core/asset-manager/CCAssetManager.js:95
bundles

已加载 bundle 的集合, 你能通过 removeBundle 来移除缓存

meta description
类型 Cache
定义于 cocos2d/core/asset-manager/CCAssetManager.js:108
assets

已加载资源的集合, 你能通过 releaseAsset 来移除缓存

meta description
类型 Cache
定义于 cocos2d/core/asset-manager/CCAssetManager.js:122
dependUtil

管理资源依赖关系

meta description
类型 DependUtil
定义于 cocos2d/core/asset-manager/CCAssetManager.js:144
cacheAsset

是否缓存已加载的资源

meta description
类型 Boolean
定义于 cocos2d/core/asset-manager/CCAssetManager.js:158
force

是否强制加载资源, 如果为 true ,加载资源将会忽略报错

meta description
类型 Boolean
定义于 cocos2d/core/asset-manager/CCAssetManager.js:170
utils

一些有用的方法

meta description
类型 Helper
定义于 cocos2d/core/asset-manager/CCAssetManager.js:182
downloader

管理所有下载任务

meta description
类型 Downloader
定义于 cocos2d/core/asset-manager/CCAssetManager.js:194
parser

管理所有解析任务

meta description
类型 Parser
定义于 cocos2d/core/asset-manager/CCAssetManager.js:206
builtins

管理内置资源

meta description
类型 Builtins
定义于 cocos2d/core/asset-manager/CCAssetManager.js:218
packManager

管理所有合并后的资源

meta description
类型 PackManager
定义于 cocos2d/core/asset-manager/CCAssetManager.js:230
cacheManager

缓存管理器是一个模块,在非 WEB 平台上,用于管理所有从服务器上下载下来的缓存

meta description
类型 cc.AssetManager.CacheManager
定义于 cocos2d/core/asset-manager/CCAssetManager.js:244
presets

可选参数的预设集

meta description
类型 Object
定义于 cocos2d/core/asset-manager/CCAssetManager.js:258
main

内置 main 包

meta description
类型 Bundle
定义于 cocos2d/core/asset-manager/CCAssetManager.js:317
resources

内置 resources 包

meta description
类型 Bundle
定义于 cocos2d/core/asset-manager/CCAssetManager.js:332
internal

内置 internal 包

meta description
类型 Bundle
定义于 cocos2d/core/asset-manager/CCAssetManager.js:347

方法

init

初始化资源管理器

meta description
定义于 cocos2d/core/asset-manager/CCAssetManager.js:362
参数列表
getBundle

获取已加载的分包

meta description
返回 Bundle
定义于 cocos2d/core/asset-manager/CCAssetManager.js:390
参数列表
  • name String The name of bundle
示例
// ${project}/assets/test1
cc.assetManager.getBundle('test1');

cc.assetManager.getBundle('resources');
removeBundle

移除此包, 注意:这个包内的资源不会自动释放, 如果需要的话你可以在摧毁之前手动调用 releaseAll 进行释放

meta description
定义于 cocos2d/core/asset-manager/CCAssetManager.js:414
参数列表
  • bundle Bundle The bundle to be removed
loadAny

通用加载资源接口,可传入进度回调以及完成回调,通过组合 request 和 options 参数,几乎可以实现和扩展所有想要的加载效果。非常建议你使用更简单的API,例如 load、loadDir 等。 options 中的自定义参数将会分发到 requests 的每一项中,如果request中已存在同名的参数则以 requests 中为准,同时如果有其他 依赖资源,则 options 中的参数会继续向依赖项中分发。request中的自定义参数都会以 options 形式传入加载流程中的 downloader, parser 的方法中, 你可以 扩展 downloader, parser 收集参数完成想实现的效果。

保留关键字: uuid, url, path, dir, scene, type, priority, preset, audioLoadMode, ext, bundle, onFileProgress, maxConcurrency, maxRequestsPerFrame maxRetryCount, version, responseType, withCredentials, mimeType, timeout, header, reload, cacheAsset, cacheEnabled, 请不要使用这些字段为自定义参数!

meta description
定义于 cocos2d/core/asset-manager/CCAssetManager.js:432
参数列表
  • requests string | string[] | Object | Object[] The request you want to load
  • options Object Optional parameters
  • onProgress Function Callback invoked when progression change
    • finished Number The number of the items that are already completed
    • total Number The total number of the items
    • item RequestItem The current request item
  • onComplete Function Callback invoked when finish loading
    • err Error The error occured in loading process.
    • data Object The loaded content
示例
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' });
preloadAny

通用预加载资源接口,可传入进度回调以及完成回调,非常建议你使用更简单的 API ,例如 preloadRes, preloadResDir 等。preloadAny 和 loadAny 几乎一样,区别在于 preloadAny 只会下载资源,不会去解析资源,你需要调用 cc.assetManager.loadAny(preloadTask) 来完成资源加载。

meta description
定义于 cocos2d/core/asset-manager/CCAssetManager.js:498
参数列表
  • requests string | string[] | Object | Object[] The request you want to preload
  • options Object Optional parameters
  • onProgress Function Callback invoked when progression change
    • finished Number The number of the items that are already completed
    • total Number The total number of the items
    • item RequestItem The current request item
  • onComplete Function Callback invoked when finish preloading
    • err Error The error occured in preloading process.
    • items RequestItem[] The preloaded content
示例
cc.assetManager.preloadAny('0cbZa5Y71CTZAccaIFluuZ', (err) => cc.assetManager.loadAny('0cbZa5Y71CTZAccaIFluuZ'));
postLoadNative

加载资源的原生文件,如果你勾选了'延迟加载资源'选项,你可能需要在使用资源之前调用此方法来加载原生文件

meta description
定义于 cocos2d/core/asset-manager/CCAssetManager.js:539
参数列表
  • asset Asset The asset
  • options Object Some optional parameters
  • onComplete Function Callback invoked when finish loading
    • err Error The error occured in loading process.
示例
cc.assetManager.postLoadNative(texture, (err) => console.log(err));
loadRemote

使用 url 加载远程资源,例如音频,图片,文本等等。

meta description
定义于 cocos2d/core/asset-manager/CCAssetManager.js:594
参数列表
  • url string The url of asset
  • options Object Some optional parameters
    • audioLoadMode cc.AudioClip.LoadMode Indicate which mode audio you want to load
    • ext string If the url does not have a extension name, you can specify one manually.
  • onComplete Function Callback invoked when finish loading
    • err Error The error occured in loading process.
    • asset Asset The loaded texture
示例
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));
cc.assetManager.loadRemote('http://www.cloud.com/test3', { ext: '.png' }, (err, texture) => console.log(err));
loadScript

加载脚本

meta description
定义于 cocos2d/core/asset-manager/CCAssetManager.js:643
参数列表
  • url string | string[] Url of the script
  • options Object Some optional paramters
    • async boolean Indicate whether or not loading process should be async
  • onComplete Function Callback when script loaded or failed
    • err Error The occurred error, null indicetes success
示例
loadScript('http://localhost:8080/index.js', null, (err) => console.log(err));
loadBundle

加载资源包

meta description
定义于 cocos2d/core/asset-manager/CCAssetManager.js:673
参数列表
  • nameOrUrl string The name or root path of bundle
  • options Object Some optional paramter, same like downloader.downloadFile
    • version string The version of this bundle, you can check config.json in this bundle
  • onComplete Function Callback when bundle loaded or failed
    • err Error The occurred error, null indicetes success
    • bundle Bundle The loaded bundle
示例
loadBundle('http://localhost:8080/test', null, (err, bundle) => console.log(err));
releaseAsset

释放资源以及其依赖资源, 这个方法不仅会从 assetManager 中删除资源的缓存引用,还会清理它的资源内容。 比如说,当你释放一个 texture 资源,这个 texture 和它的 gl 贴图数据都会被释放。 注意,这个函数可能会导致资源贴图或资源所依赖的贴图不可用,如果场景中存在节点仍然依赖同样的贴图,它们可能会变黑并报 GL 错误。

meta description
定义于 cocos2d/core/asset-manager/CCAssetManager.js:711
参数列表
  • asset Asset The asset to be released
示例
// release a texture which is no longer need
cc.assetManager.releaseAsset(texture);
releaseUnusedAssets

释放所有没有用到的资源。详细信息请参考 releaseAsset

meta description
定义于 cocos2d/core/asset-manager/CCAssetManager.js:737
releaseAll

释放所有资源。详细信息请参考 releaseAsset

meta description
定义于 cocos2d/core/asset-manager/CCAssetManager.js:756

条与 "" 相匹配的结果

    没有与 "" 匹配的结果