AssetManager 类型
模块: cc
此模块管理资源的行为和信息,包括加载,释放等,这是一个单例,所有成员能够通过 cc.assetManager 调用
索引
属性(properties)
pipelinePipeline正常加载管线fetchPipelinePipeline下载管线transformPipelinePipelineUrl 转换器bundlesCache已加载 bundle 的集合, 你能通过 removeBundle 来移除缓存assetsCache已加载资源的集合, 你能通过 releaseAsset 来移除缓存dependUtilDependUtil管理资源依赖关系cacheAssetBoolean是否缓存已加载的资源forceBoolean是否强制加载资源, 如果为 true ,加载资源将会忽略报错utilsHelper一些有用的方法downloaderDownloader管理所有下载任务parserParser管理所有解析任务builtinsBuiltins管理内置资源packManagerPackManager管理所有合并后的资源cacheManagercc.AssetManager.CacheManager缓存管理器是一个模块,在非 WEB 平台上,用于管理所有从服务器上下载下来的缓存presetsObject可选参数的预设集mainBundle内置 main 包resourcesBundle内置 resources 包internalBundle内置 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 |
参数列表
optionsObject
getBundle
获取已加载的分包
| meta | description |
|---|---|
| 返回 | Bundle |
| 定义于 | cocos2d/core/asset-manager/CCAssetManager.js:390 |
参数列表
nameString 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 |
参数列表
bundleBundle 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 |
参数列表
requestsstring | string[] | Object | Object[] The request you want to loadoptionsObject Optional parametersonProgressFunction Callback invoked when progression changefinishedNumber The number of the items that are already completedtotalNumber The total number of the itemsitemRequestItem The current request item
onCompleteFunction Callback invoked when finish loading
示例
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 |
参数列表
requestsstring | string[] | Object | Object[] The request you want to preloadoptionsObject Optional parametersonProgressFunction Callback invoked when progression changefinishedNumber The number of the items that are already completedtotalNumber The total number of the itemsitemRequestItem The current request item
onCompleteFunction Callback invoked when finish preloadingerrError The error occured in preloading process.itemsRequestItem[] The preloaded content
示例
cc.assetManager.preloadAny('0cbZa5Y71CTZAccaIFluuZ', (err) => cc.assetManager.loadAny('0cbZa5Y71CTZAccaIFluuZ'));
postLoadNative
加载资源的原生文件,如果你勾选了'延迟加载资源'选项,你可能需要在使用资源之前调用此方法来加载原生文件
| meta | description |
|---|---|
| 定义于 | cocos2d/core/asset-manager/CCAssetManager.js:539 |
参数列表
assetAsset The assetoptionsObject Some optional parametersonCompleteFunction Callback invoked when finish loadingerrError 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 |
参数列表
urlstring The url of assetoptionsObject Some optional parametersaudioLoadModecc.AudioClip.LoadMode Indicate which mode audio you want to loadextstring If the url does not have a extension name, you can specify one manually.
onCompleteFunction Callback invoked when finish loading
示例
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 |
参数列表
urlstring | string[] Url of the scriptoptionsObject Some optional paramtersasyncboolean Indicate whether or not loading process should be async
onCompleteFunction Callback when script loaded or failederrError 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 |
参数列表
nameOrUrlstring The name or root path of bundleoptionsObject Some optional paramter, same like downloader.downloadFileversionstring The version of this bundle, you can check config.json in this bundle
onCompleteFunction Callback when bundle loaded or failed
示例
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 |
参数列表
assetAsset 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 |