AssetManager Class
Module: cc
This module controls asset's behaviors and information, include loading, releasing etc. it is a singleton
All member can be accessed with cc.assetManager.
Index
Properties
pipelinePipelineNormal loading pipelinefetchPipelinePipelineFetching pipelinetransformPipelinePipelineUrl transformerbundlesCacheThe collection of bundle which is already loaded, you can remove cache with removeBundleassetsCacheThe collection of asset which is already loaded, you can remove cache with releaseAssetdependUtilDependUtilManage relationship between asset and its dependenciescacheAssetBooleanWhether or not cache the loaded assetforceBooleanWhether or not load asset forcely, if it is true, asset will be loaded regardless of errorutilsHelperSome useful functiondownloaderDownloaderManage all downloading taskparserParserManage all parsing taskbuiltinsBuiltinsManage internal assetpackManagerPackManagerManage all packed assetcacheManagercc.AssetManager.CacheManagerCache manager is a module which controls all caches downloaded from server in non-web platform.presetsObjectThe preset of optionsmainBundleThe builtin 'main' bundleresourcesBundleThe builtin 'resources' bundleinternalBundleThe builtin 'internal' bundle
Methods
initInitialize assetManager with optionsgetBundleGet the bundle which has been loadedremoveBundleRemove this bundle.loadAnyGeneral interface used to load assets with a progression callback and a complete callback.preloadAnyGeneral interface used to preload assets with a progression callback and a complete callback.It is highly recommended that you use more simple API, such aspreloadRes,preloadResDiretc.postLoadNativeLoad 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 assetloadRemoteLoad remote asset with url, such as audio, image, text and so on.loadScriptLoad scriptloadBundleload bundlereleaseAssetRelease asset and it's dependencies.releaseUnusedAssetsRelease all unused assets.releaseAllRelease all assets.
Details
Properties
pipeline
Normal loading pipeline
| meta | description |
|---|---|
| Type | Pipeline |
| Defined in | cocos2d/core/asset-manager/CCAssetManager.js:71 |
fetchPipeline
Fetching pipeline
| meta | description |
|---|---|
| Type | Pipeline |
| Defined in | cocos2d/core/asset-manager/CCAssetManager.js:83 |
transformPipeline
Url transformer
| meta | description |
|---|---|
| Type | Pipeline |
| Defined in | cocos2d/core/asset-manager/CCAssetManager.js:95 |
bundles
The collection of bundle which is already loaded, you can remove cache with removeBundle
| meta | description |
|---|---|
| Type | Cache |
| Defined in | cocos2d/core/asset-manager/CCAssetManager.js:108 |
assets
The collection of asset which is already loaded, you can remove cache with releaseAsset
| meta | description |
|---|---|
| Type | Cache |
| Defined in | cocos2d/core/asset-manager/CCAssetManager.js:122 |
dependUtil
Manage relationship between asset and its dependencies
| meta | description |
|---|---|
| Type | DependUtil |
| Defined in | cocos2d/core/asset-manager/CCAssetManager.js:144 |
cacheAsset
Whether or not cache the loaded asset
| meta | description |
|---|---|
| Type | Boolean |
| Defined in | cocos2d/core/asset-manager/CCAssetManager.js:158 |
force
Whether or not load asset forcely, if it is true, asset will be loaded regardless of error
| meta | description |
|---|---|
| Type | Boolean |
| Defined in | cocos2d/core/asset-manager/CCAssetManager.js:170 |
utils
Some useful function
| meta | description |
|---|---|
| Type | Helper |
| Defined in | cocos2d/core/asset-manager/CCAssetManager.js:182 |
downloader
Manage all downloading task
| meta | description |
|---|---|
| Type | Downloader |
| Defined in | cocos2d/core/asset-manager/CCAssetManager.js:194 |
parser
Manage all parsing task
| meta | description |
|---|---|
| Type | Parser |
| Defined in | cocos2d/core/asset-manager/CCAssetManager.js:206 |
builtins
Manage internal asset
| meta | description |
|---|---|
| Type | Builtins |
| Defined in | cocos2d/core/asset-manager/CCAssetManager.js:218 |
packManager
Manage all packed asset
| meta | description |
|---|---|
| Type | PackManager |
| Defined in | cocos2d/core/asset-manager/CCAssetManager.js:230 |
cacheManager
Cache manager is a module which controls all caches downloaded from server in non-web platform.
| meta | description |
|---|---|
| Type | cc.AssetManager.CacheManager |
| Defined in | cocos2d/core/asset-manager/CCAssetManager.js:244 |
presets
The preset of options
| meta | description |
|---|---|
| Type | Object |
| Defined in | cocos2d/core/asset-manager/CCAssetManager.js:258 |
main
The builtin 'main' bundle
| meta | description |
|---|---|
| Type | Bundle |
| Defined in | cocos2d/core/asset-manager/CCAssetManager.js:317 |
resources
The builtin 'resources' bundle
| meta | description |
|---|---|
| Type | Bundle |
| Defined in | cocos2d/core/asset-manager/CCAssetManager.js:332 |
internal
The builtin 'internal' bundle
| meta | description |
|---|---|
| Type | Bundle |
| Defined in | cocos2d/core/asset-manager/CCAssetManager.js:347 |
Methods
init
Initialize assetManager with options
| meta | description |
|---|---|
| Defined in | cocos2d/core/asset-manager/CCAssetManager.js:362 |
Parameters
optionsObject
getBundle
Get the bundle which has been loaded
| meta | description |
|---|---|
| Returns | Bundle |
| Defined in | cocos2d/core/asset-manager/CCAssetManager.js:390 |
Parameters
nameString The name of bundle
Examples
// ${project}/assets/test1
cc.assetManager.getBundle('test1');
cc.assetManager.getBundle('resources');
removeBundle
Remove this bundle. NOTE: The asset whthin this bundle will not be released automatically, you can call releaseAll manually before remove it if you need
| meta | description |
|---|---|
| Defined in | cocos2d/core/asset-manager/CCAssetManager.js:414 |
Parameters
bundleBundle The bundle to be removed
loadAny
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!
| meta | description |
|---|---|
| Defined in | cocos2d/core/asset-manager/CCAssetManager.js:432 |
Parameters
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
Examples
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
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
| meta | description |
|---|---|
| Defined in | cocos2d/core/asset-manager/CCAssetManager.js:498 |
Parameters
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
Examples
cc.assetManager.preloadAny('0cbZa5Y71CTZAccaIFluuZ', (err) => cc.assetManager.loadAny('0cbZa5Y71CTZAccaIFluuZ'));
postLoadNative
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
| meta | description |
|---|---|
| Defined in | cocos2d/core/asset-manager/CCAssetManager.js:539 |
Parameters
assetAsset The assetoptionsObject Some optional parametersonCompleteFunction Callback invoked when finish loadingerrError The error occured in loading process.
Examples
cc.assetManager.postLoadNative(texture, (err) => console.log(err));
loadRemote
Load remote asset with url, such as audio, image, text and so on.
| meta | description |
|---|---|
| Defined in | cocos2d/core/asset-manager/CCAssetManager.js:594 |
Parameters
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
Examples
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
Load script
| meta | description |
|---|---|
| Defined in | cocos2d/core/asset-manager/CCAssetManager.js:643 |
Parameters
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
Examples
loadScript('http://localhost:8080/index.js', null, (err) => console.log(err));
loadBundle
load bundle
| meta | description |
|---|---|
| Defined in | cocos2d/core/asset-manager/CCAssetManager.js:673 |
Parameters
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
Examples
loadBundle('http://localhost:8080/test', null, (err, bundle) => console.log(err));
releaseAsset
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.
| meta | description |
|---|---|
| Defined in | cocos2d/core/asset-manager/CCAssetManager.js:711 |
Parameters
assetAsset The asset to be released
Examples
// release a texture which is no longer need
cc.assetManager.releaseAsset(texture);
releaseUnusedAssets
Release all unused assets. Refer to releaseAsset for detailed informations.
| meta | description |
|---|---|
| Defined in | cocos2d/core/asset-manager/CCAssetManager.js:737 |
releaseAll
Release all assets. Refer to releaseAsset for detailed informations.
| meta | description |
|---|---|
| Defined in | cocos2d/core/asset-manager/CCAssetManager.js:756 |