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
name
String
The name of this bundledeps
String[]
The dependency of this bundlebase
String
The root path of this bundle, such like 'http://example.com/bundle1'
Methods
constructor
Create a bundlegetInfoWithPath
Get asset's info using path, only valid when asset is in bundle folder.getDirWithPath
Get all asset's info within specific foldergetAssetInfo
Get asset's info with uuidgetSceneInfo
Get scene'info with nameinit
Initialize this bundle with optionsload
Load the asset within this bundle by the path which is relative to bundle's pathpreload
Preload the asset within this bundle by the path which is relative to bundle's path.loadDir
Load all assets under a folder inside the bundle folder.
...preloadDir
Preload all assets under a folder inside the bundle folder.
After calling this method, you still need to finish loading by callingBundle.loadDir
.loadScene
Loads the scene within this bundle by its name.preloadScene
Preloads the scene within this bundle by its name.get
Get asset within this bundle by path and type.release
Release the asset loaded by load or loadDir and it's dependencies.releaseUnusedAssets
Release all unused assets within this bundle.releaseAll
Release 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
path
string The relative path of asset, such as 'images/a'type
Function 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
path
string The relative path of folder, such as 'images'type
Function The constructor should be used to filter pathsout
Array 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
uuid
string 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
name
string 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
options
Object
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
paths
String | String[] Paths of the target assets.The path is relative to the bundle's folder, extensions must be omitted.type
Function Only asset of type will be loaded if this argument is supplied.onProgress
Function Callback invoked when progression change.finish
Number The number of the items that are already completed.total
Number The total number of the items.item
RequestItem The finished request item.
onComplete
Function 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
paths
String | String[] Paths of the target asset.The path is relative to bundle folder, extensions must be omitted.type
Function Only asset of type will be loaded if this argument is supplied.onProgress
Function Callback invoked when progression change.finish
Number The number of the items that are already completed.total
Number The total number of the items.item
RequestItem The finished request item.
onComplete
Function Callback invoked when the resource loaded.error
Error The error info or null if loaded successfully.items
RequestItem[] 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
dir
string path of the target folder.The path is relative to the bundle folder, extensions must be omitted.type
Function Only asset of type will be loaded if this argument is supplied.onProgress
Function Callback invoked when progression change.onComplete
Function 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
dir
string path of the target folder.The path is relative to the bundle folder, extensions must be omitted.type
Function Only asset of type will be preloaded if this argument is supplied.onProgress
Function Callback invoked when progression change.onComplete
Function A callback which is called when all assets have been loaded, or an error occurs.error
Error 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.items
RequestItem[] 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
sceneName
String The name of the scene to load.options
Object Some optional parametersonProgress
Function Callback invoked when progression change.onComplete
Function callback, will be called after scene launched.err
Error The occurred error, null indicetes successsceneAsset
SceneAsset 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
sceneName
String The name of the scene to preload.options
Object Some optional parametersonProgress
Function callback, will be called when the load progression change.finish
Number The number of the items that are already completedtotal
Number The total number of the itemsitem
RequestItem The latest request item
onComplete
Function callback, will be called after scene loaded.error
Error 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
path
String The path of assettype
Function 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
path
String The path of assettype
Function 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();