Bundle 类型

模块: cc.AssetManager

一个包含一定数量资源(包括场景)的包,你可以加载,预加载,释放此包内的资源

索引

属性(properties)
方法
  • constructor 创建一个 bundle
  • getInfoWithPath 使用 path 获取资源的配置信息
  • getDirWithPath 获取在某个指定文件夹下的所有资源信息
  • getAssetInfo 通过 uuid 获取资源信息
  • getSceneInfo 通过场景名获取场景信息
  • init 初始化此 bundle
  • load 通过相对路径加载分包中的资源。
  • preload 通过相对路径预加载分包中的资源。
  • loadDir 加载目标文件夹中的所有资源, 注意:路径中只能使用斜杠,反斜杠将停止工作
  • preloadDir 预加载目标文件夹中的所有资源。
  • loadScene 通过场景名称加载分包中的场景。
  • preloadScene 通过场景名称预加载分包中的场景.调用完后,你仍然需要通过 Bundle.loadScenecc.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
参数列表
  • path string The relative path of asset, such as 'images/a'
  • type Function The constructor of asset, such as cc.Texture2D
示例
var info = bundle.getInfoWithPath('image/a', cc.Texture2D);
getDirWithPath

获取在某个指定文件夹下的所有资源信息

meta description
返回 Object[]
定义于 cocos2d/core/asset-manager/bundle.js:130
参数列表
  • path string The relative path of folder, such as 'images'
  • type Function The constructor should be used to filter paths
  • out Array The output array
示例
var infos = [];
bundle.getDirWithPath('images', cc.Texture2D, infos);
getAssetInfo

通过 uuid 获取资源信息

meta description
返回 Object
定义于 cocos2d/core/asset-manager/bundle.js:156
参数列表
  • uuid string The asset's uuid
示例
var info = bundle.getAssetInfo('fcmR3XADNLgJ1ByKhqcC5Z');
getSceneInfo

通过场景名获取场景信息

meta description
返回 Object
定义于 cocos2d/core/asset-manager/bundle.js:177
参数列表
  • name string The name of scene
示例
var info = bundle.getSceneInfo('first.fire');
init

初始化此 bundle

meta description
定义于 cocos2d/core/asset-manager/bundle.js:198
参数列表
load

通过相对路径加载分包中的资源。路径是相对分包文件夹路径的相对路径

meta description
定义于 cocos2d/core/asset-manager/bundle.js:216
参数列表
  • 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.
    • error Error The error info or null if loaded successfully.
    • assets Asset | Asset[] The loaded assets.
示例
// 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
参数列表
  • 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.
示例
// 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
参数列表
  • 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.
    • finish Number The number of the items that are already completed.
    • total Number The total number of the items.
    • item Object The latest request item
  • 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 loaded successfully, error will be null.
    • assets Asset[] | Asset An array of all loaded assets.
示例
// 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
参数列表
  • 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.
    • finish Number The number of the items that are already completed.
    • total Number The total number of the items.
    • item Object The latest request item
  • 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.
示例
// 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
参数列表
  • sceneName String The name of the scene to load.
  • options Object Some optional parameters
  • 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 Object The latest request item
  • onComplete Function callback, will be called after scene launched.
    • err Error The occurred error, null indicetes success
    • sceneAsset SceneAsset The scene asset
示例
bundle1.loadScene('first', (err, sceneAsset) => cc.director.runScene(sceneAsset));
preloadScene

通过场景名称预加载分包中的场景.调用完后,你仍然需要通过 Bundle.loadScenecc.director.loadScene 来完成加载。 就算预加载还没完成,你也可以直接调用 Bundle.loadScenecc.director.loadScene

meta description
定义于 cocos2d/core/asset-manager/bundle.js:464
参数列表
  • sceneName String The name of the scene to preload.
  • options Object Some optional parameters
  • onProgress Function callback, will be called when the load progression change.
    • finish Number The number of the items that are already completed
    • total Number The total number of the items
    • item RequestItem The latest request item
  • onComplete Function callback, will be called after scene loaded.
    • error Error 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
参数列表
  • path String The path of asset
  • type Function 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
参数列表
  • path String The path of asset
  • type Function 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();

results matching ""

    No results matching ""