Bundle Class
Module: cc.AssetManager
A bundle contains an amount of assets(includes scene), you can load, preload, release asset which is in this bundle
Index
Properties
nameStringThe name of this bundledepsString[]The dependency of this bundlebaseStringThe root path of this bundle, such like 'http://example.com/bundle1'
Methods
constructorCreate a bundlegetInfoWithPathGet asset's info using path, only valid when asset is in bundle folder.getDirWithPathGet all asset's info within specific foldergetAssetInfoGet asset's info with uuidgetSceneInfoGet scene'info with nameinitInitialize this bundle with optionsloadLoad the asset within this bundle by the path which is relative to bundle's pathpreloadPreload the asset within this bundle by the path which is relative to bundle's path.loadDirLoad all assets under a folder inside the bundle folder.
...preloadDirPreload all assets under a folder inside the bundle folder.
After calling this method, you still need to finish loading by callingBundle.loadDir.loadSceneLoads the scene within this bundle by its name.preloadScenePreloads the scene within this bundle by its name.getGet asset within this bundle by path and type.releaseRelease the asset loaded by load or loadDir and it's dependencies.releaseUnusedAssetsRelease all unused assets within this bundle.releaseAllRelease all assets within this bundle.
Details
Properties
name
The name of this bundle
| meta | description |
|---|---|
| Type | String |
| Defined in | cocos2d/core/asset-manager/bundle.js:66 |
deps
The dependency of this bundle
| meta | description |
|---|---|
| Type | String[] |
| Defined in | cocos2d/core/asset-manager/bundle.js:80 |
base
The root path of this bundle, such like 'http://example.com/bundle1'
| meta | description |
|---|---|
| Type | String |
| Defined in | cocos2d/core/asset-manager/bundle.js:94 |
Methods
constructor
Create a bundle
| meta | description |
|---|---|
| Defined in | cocos2d/core/asset-manager/bundle.js:52 |
getInfoWithPath
Get asset's info using path, only valid when asset is in bundle folder.
| meta | description |
|---|---|
| Returns | Object |
| Defined in | cocos2d/core/asset-manager/bundle.js:108 |
Parameters
pathstring The relative path of asset, such as 'images/a'typeFunction The constructor of asset, such ascc.Texture2D
Examples
var info = bundle.getInfoWithPath('image/a', cc.Texture2D);
getDirWithPath
Get all asset's info within specific folder
| meta | description |
|---|---|
| Returns | Object[] |
| Defined in | cocos2d/core/asset-manager/bundle.js:130 |
Parameters
pathstring The relative path of folder, such as 'images'typeFunction The constructor should be used to filter pathsoutArray The output array
Examples
var infos = [];
bundle.getDirWithPath('images', cc.Texture2D, infos);
getAssetInfo
Get asset's info with uuid
| meta | description |
|---|---|
| Returns | Object |
| Defined in | cocos2d/core/asset-manager/bundle.js:156 |
Parameters
uuidstring The asset's uuid
Examples
var info = bundle.getAssetInfo('fcmR3XADNLgJ1ByKhqcC5Z');
getSceneInfo
Get scene'info with name
| meta | description |
|---|---|
| Returns | Object |
| Defined in | cocos2d/core/asset-manager/bundle.js:177 |
Parameters
namestring The name of scene
Examples
var info = bundle.getSceneInfo('first.fire');
init
Initialize this bundle with options
| meta | description |
|---|---|
| Defined in | cocos2d/core/asset-manager/bundle.js:198 |
Parameters
optionsObject
load
Load the asset within this bundle by the path which is relative to bundle's path
| meta | description |
|---|---|
| Defined in | cocos2d/core/asset-manager/bundle.js:216 |
Parameters
pathsString | String[] Paths of the target assets.The path is relative to the bundle's folder, extensions must be omitted.typeFunction Only asset of type will be loaded if this argument is supplied.onProgressFunction Callback invoked when progression change.finishNumber The number of the items that are already completed.totalNumber The total number of the items.itemRequestItem The finished request item.
onCompleteFunction Callback invoked when all assets loaded.
Examples
// load the texture (${project}/assets/resources/textures/background.jpg) from resources
cc.resources.load('textures/background', cc.Texture2D, (err, texture) => console.log(err));
// load the audio (${project}/assets/resources/music/hit.mp3) from resources
cc.resources.load('music/hit', cc.AudioClip, (err, audio) => console.log(err));
// load the prefab (${project}/assets/bundle1/misc/character/cocos) from bundle1 folder
bundle1.load('misc/character/cocos', cc.Prefab, (err, prefab) => console.log(err));
// load the sprite frame (${project}/assets/some/xxx/bundle2/imgs/cocos.png) from bundle2 folder
bundle2.load('imgs/cocos', cc.SpriteFrame, null, (err, spriteFrame) => console.log(err));
preload
Preload the asset within this bundle by the path which is relative to bundle's path.
After calling this method, you still need to finish loading by calling Bundle.load.
It will be totally fine to call Bundle.load at any time even if the preloading is not
yet finished
| meta | description |
|---|---|
| Defined in | cocos2d/core/asset-manager/bundle.js:262 |
Parameters
pathsString | String[] Paths of the target asset.The path is relative to bundle folder, extensions must be omitted.typeFunction Only asset of type will be loaded if this argument is supplied.onProgressFunction Callback invoked when progression change.finishNumber The number of the items that are already completed.totalNumber The total number of the items.itemRequestItem The finished request item.
onCompleteFunction Callback invoked when the resource loaded.errorError The error info or null if loaded successfully.itemsRequestItem[] The preloaded items.
Examples
// preload the texture (${project}/assets/resources/textures/background.jpg) from resources
cc.resources.preload('textures/background', cc.Texture2D);
// preload the audio (${project}/assets/resources/music/hit.mp3) from resources
cc.resources.preload('music/hit', cc.AudioClip);
// wait for while
cc.resources.load('music/hit', cc.AudioClip, (err, audioClip) => {});
* // preload the prefab (${project}/assets/bundle1/misc/character/cocos) from bundle1 folder
bundle1.preload('misc/character/cocos', cc.Prefab);
// load the sprite frame of (${project}/assets/bundle2/imgs/cocos.png) from bundle2 folder
bundle2.preload('imgs/cocos', cc.SpriteFrame);
// wait for while
bundle2.load('imgs/cocos', cc.SpriteFrame, (err, spriteFrame) => {});
loadDir
Load all assets under a folder inside the bundle folder.
Note: All asset paths in Creator use forward slashes, paths using backslashes will not work.
| meta | description |
|---|---|
| Defined in | cocos2d/core/asset-manager/bundle.js:314 |
Parameters
dirstring path of the target folder.The path is relative to the bundle folder, extensions must be omitted.typeFunction Only asset of type will be loaded if this argument is supplied.onProgressFunction Callback invoked when progression change.onCompleteFunction A callback which is called when all assets have been loaded, or an error occurs.
Examples
// load all audios (resources/audios/)
cc.resources.loadDir('audios', cc.AudioClip, (err, audios) => {});
// load all textures in "resources/imgs/"
cc.resources.loadDir('imgs', cc.Texture2D, null, function (err, textures) {
var texture1 = textures[0];
var texture2 = textures[1];
});
// load all prefabs (${project}/assets/bundle1/misc/characters/) from bundle1 folder
bundle1.loadDir('misc/characters', cc.Prefab, (err, prefabs) => console.log(err));
// load all sprite frame (${project}/assets/some/xxx/bundle2/skills/) from bundle2 folder
bundle2.loadDir('skills', cc.SpriteFrame, null, (err, spriteFrames) => console.log(err));
preloadDir
Preload all assets under a folder inside the bundle folder.
After calling this method, you still need to finish loading by calling Bundle.loadDir.
It will be totally fine to call Bundle.loadDir at any time even if the preloading is not yet finished
| meta | description |
|---|---|
| Defined in | cocos2d/core/asset-manager/bundle.js:363 |
Parameters
dirstring path of the target folder.The path is relative to the bundle folder, extensions must be omitted.typeFunction Only asset of type will be preloaded if this argument is supplied.onProgressFunction Callback invoked when progression change.onCompleteFunction A callback which is called when all assets have been loaded, or an error occurs.errorError If one of the asset failed, the complete callback is immediately called with the error. If all assets are preloaded successfully, error will be null.itemsRequestItem[] An array of all preloaded items.
Examples
// preload all audios (resources/audios/)
cc.resources.preloadDir('audios', cc.AudioClip);
// preload all textures in "resources/imgs/"
cc.resources.preloadDir('imgs', cc.Texture2D);
// wait for while
cc.resources.loadDir('imgs', cc.Texture2D, (err, textures) => {});
// preload all prefabs (${project}/assets/bundle1/misc/characters/) from bundle1 folder
bundle1.preloadDir('misc/characters', cc.Prefab);
// preload all sprite frame (${project}/assets/some/xxx/bundle2/skills/) from bundle2 folder
bundle2.preloadDir('skills', cc.SpriteFrame);
// wait for while
bundle2.loadDir('skills', cc.SpriteFrame, (err, spriteFrames) => {});
loadScene
Loads the scene within this bundle by its name.
| meta | description |
|---|---|
| Defined in | cocos2d/core/asset-manager/bundle.js:413 |
Parameters
sceneNameString The name of the scene to load.optionsObject Some optional parametersonProgressFunction Callback invoked when progression change.onCompleteFunction callback, will be called after scene launched.errError The occurred error, null indicetes successsceneAssetSceneAsset The scene asset
Examples
bundle1.loadScene('first', (err, sceneAsset) => cc.director.runScene(sceneAsset));
preloadScene
Preloads the scene within this bundle by its name. After calling this method, you still need to finish loading by calling Bundle.loadScene or cc.director.loadScene.
It will be totally fine to call Bundle.loadDir at any time even if the preloading is not yet finished
| meta | description |
|---|---|
| Defined in | cocos2d/core/asset-manager/bundle.js:464 |
Parameters
sceneNameString The name of the scene to preload.optionsObject Some optional parametersonProgressFunction callback, will be called when the load progression change.finishNumber The number of the items that are already completedtotalNumber The total number of the itemsitemRequestItem The latest request item
onCompleteFunction callback, will be called after scene loaded.errorError null or the error object.
Examples
bundle1.preloadScene('first');
// wait for a while
bundle1.loadScene('first', (err, scene) => cc.director.runScene(scene));
get
Get asset within this bundle by path and type.
After you load asset with load or loadDir,
you can acquire them by passing the path to this API.
| meta | description |
|---|---|
| Returns | Asset |
| Defined in | cocos2d/core/asset-manager/bundle.js:508 |
Parameters
pathString The path of assettypeFunction Only asset of type will be returned if this argument is supplied.
Examples
bundle1.get('music/hit', cc.AudioClip);
release
Release the asset loaded by load or loadDir and it's dependencies. Refer to releaseAsset for detailed informations.
| meta | description |
|---|---|
| Defined in | cocos2d/core/asset-manager/bundle.js:534 |
Parameters
pathString The path of assettypeFunction Only asset of type will be released if this argument is supplied.
Examples
// release a texture which is no longer need
bundle1.release('misc/character/cocos');
releaseUnusedAssets
Release all unused assets within this bundle. Refer to releaseAll for detailed informations.
| meta | description |
|---|---|
| Defined in | cocos2d/core/asset-manager/bundle.js:558 |
Examples
// release all unused asset within bundle1
bundle1.releaseUnusedAssets();
releaseAll
Release all assets within this bundle. Refer to releaseAll for detailed informations.
| meta | description |
|---|---|
| Defined in | cocos2d/core/asset-manager/bundle.js:585 |
Examples
// release all asset within bundle1
bundle1.releaseAll();