Bundle 类型
模块: cc.AssetManager
一个包含一定数量资源(包括场景)的包,你可以加载,预加载,释放此包内的资源
索引
属性(properties)
nameString此 bundle 的名称depsString[]此 bundle 的依赖baseString此 bundle 的根路径, 例如 'http://example.com/bundle1'
方法
constructor创建一个 bundlegetInfoWithPath使用 path 获取资源的配置信息getDirWithPath获取在某个指定文件夹下的所有资源信息getAssetInfo通过 uuid 获取资源信息getSceneInfo通过场景名获取场景信息init初始化此 bundleload通过相对路径加载分包中的资源。preload通过相对路径预加载分包中的资源。loadDir加载目标文件夹中的所有资源, 注意:路径中只能使用斜杠,反斜杠将停止工作preloadDir预加载目标文件夹中的所有资源。loadScene通过场景名称加载分包中的场景。preloadScene通过场景名称预加载分包中的场景.调用完后,你仍然需要通过Bundle.loadScene或cc.director.loadScene来完成加载。get通过路径与类型获取资源。release释放通过 load 或者 loadDir 加载的资源。releaseUnusedAssets释放此包中的所有没有用到的资源。releaseAll释放此包中的所有资源。
Details
属性(properties)
name
此 bundle 的名称
| meta | description |
|---|---|
| 类型 | String |
| 定义于 | cocos2d/core/asset-manager/bundle.js:66 |
deps
此 bundle 的依赖
| meta | description |
|---|---|
| 类型 | String[] |
| 定义于 | cocos2d/core/asset-manager/bundle.js:80 |
base
此 bundle 的根路径, 例如 'http://example.com/bundle1'
| meta | description |
|---|---|
| 类型 | String |
| 定义于 | cocos2d/core/asset-manager/bundle.js:94 |
方法
constructor
创建一个 bundle
| meta | description |
|---|---|
| 定义于 | cocos2d/core/asset-manager/bundle.js:52 |
getInfoWithPath
使用 path 获取资源的配置信息
| meta | description |
|---|---|
| 返回 | Object |
| 定义于 | cocos2d/core/asset-manager/bundle.js:108 |
参数列表
pathstring The relative path of asset, such as 'images/a'typeFunction The constructor of asset, such ascc.Texture2D
示例
var info = bundle.getInfoWithPath('image/a', cc.Texture2D);
getDirWithPath
获取在某个指定文件夹下的所有资源信息
| meta | description |
|---|---|
| 返回 | Object[] |
| 定义于 | cocos2d/core/asset-manager/bundle.js:130 |
参数列表
pathstring The relative path of folder, such as 'images'typeFunction The constructor should be used to filter pathsoutArray The output array
示例
var infos = [];
bundle.getDirWithPath('images', cc.Texture2D, infos);
getAssetInfo
通过 uuid 获取资源信息
| meta | description |
|---|---|
| 返回 | Object |
| 定义于 | cocos2d/core/asset-manager/bundle.js:156 |
参数列表
uuidstring The asset's uuid
示例
var info = bundle.getAssetInfo('fcmR3XADNLgJ1ByKhqcC5Z');
getSceneInfo
通过场景名获取场景信息
| meta | description |
|---|---|
| 返回 | Object |
| 定义于 | cocos2d/core/asset-manager/bundle.js:177 |
参数列表
namestring The name of scene
示例
var info = bundle.getSceneInfo('first.fire');
init
初始化此 bundle
| meta | description |
|---|---|
| 定义于 | cocos2d/core/asset-manager/bundle.js:198 |
参数列表
optionsObject
load
通过相对路径加载分包中的资源。路径是相对分包文件夹路径的相对路径
| meta | description |
|---|---|
| 定义于 | cocos2d/core/asset-manager/bundle.js:216 |
参数列表
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.
示例
// 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
通过相对路径预加载分包中的资源。路径是相对分包文件夹路径的相对路径。调用完后,你仍然需要通过 Bundle.load 来完成加载。
就算预加载还没完成,你也可以直接调用 Bundle.load。
| meta | description |
|---|---|
| 定义于 | cocos2d/core/asset-manager/bundle.js:262 |
参数列表
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.
示例
// 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
加载目标文件夹中的所有资源, 注意:路径中只能使用斜杠,反斜杠将停止工作
| meta | description |
|---|---|
| 定义于 | cocos2d/core/asset-manager/bundle.js:314 |
参数列表
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.
示例
// 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
预加载目标文件夹中的所有资源。调用完后,你仍然需要通过 Bundle.loadDir 来完成加载。
就算预加载还没完成,你也可以直接调用 Bundle.loadDir。
| meta | description |
|---|---|
| 定义于 | cocos2d/core/asset-manager/bundle.js:363 |
参数列表
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.
示例
// 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
通过场景名称加载分包中的场景。
| meta | description |
|---|---|
| 定义于 | cocos2d/core/asset-manager/bundle.js:413 |
参数列表
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
示例
bundle1.loadScene('first', (err, sceneAsset) => cc.director.runScene(sceneAsset));
preloadScene
通过场景名称预加载分包中的场景.调用完后,你仍然需要通过 Bundle.loadScene 或 cc.director.loadScene 来完成加载。
就算预加载还没完成,你也可以直接调用 Bundle.loadScene 或 cc.director.loadScene。
| meta | description |
|---|---|
| 定义于 | cocos2d/core/asset-manager/bundle.js:464 |
参数列表
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.
示例
bundle1.preloadScene('first');
// wait for a while
bundle1.loadScene('first', (err, scene) => cc.director.runScene(scene));
get
通过路径与类型获取资源。在你使用 load 或者 loadDir 之后, 你能通过传路径通过这个 API 获取到这些资源。
| meta | description |
|---|---|
| 返回 | Asset |
| 定义于 | cocos2d/core/asset-manager/bundle.js:508 |
参数列表
pathString The path of assettypeFunction Only asset of type will be returned if this argument is supplied.
示例
bundle1.get('music/hit', cc.AudioClip);
release
释放通过 load 或者 loadDir 加载的资源。详细信息请参考 releaseAsset
| meta | description |
|---|---|
| 定义于 | cocos2d/core/asset-manager/bundle.js:534 |
参数列表
pathString The path of assettypeFunction Only asset of type will be released if this argument is supplied.
示例
// release a texture which is no longer need
bundle1.release('misc/character/cocos');
releaseUnusedAssets
释放此包中的所有没有用到的资源。详细信息请参考 releaseAll
| meta | description |
|---|---|
| 定义于 | cocos2d/core/asset-manager/bundle.js:558 |
示例
// release all unused asset within bundle1
bundle1.releaseUnusedAssets();
releaseAll
释放此包中的所有资源。详细信息请参考 releaseAll
| meta | description |
|---|---|
| 定义于 | cocos2d/core/asset-manager/bundle.js:585 |
示例
// release all asset within bundle1
bundle1.releaseAll();