loader 类型

继承于 Pipeline

模块: cc父模块: cc

Loader for resource loading process. It's a singleton object.

索引

属性(properties)
  • assetLoader Object The asset loader in cc.loader's pipeline, it's by default the first pipe….
  • md5Pipe Object The md5 pipe in cc.loader's pipeline, it could be absent if the project isn't build with md5 option….
  • downloader Object The downloader in cc.loader's pipeline, it's by default the second pipe….
  • loader Object The loader in cc.loader's pipeline, it's by default the third pipe….
方法
  • getXMLHttpRequest Gets a new XMLHttpRequest instance.
  • addDownloadHandlers Add custom supported types handler or modify existing type handler for download process.
  • addLoadHandlers Add custom supported types handler or modify existing type handler for load process.
  • load Load resources with a progression callback and a complete callback….
  • loadRes Load resources from the "resources" folder inside the "assets" folder of your project….
  • loadResArray This method is like loadRes except that it accepts array of url.
  • loadResDir Load all assets in a folder inside the "assets/resources" folder of your project….
  • getRes Get resource data by id. …
  • getDependsRecursively 获取一个指定资源的所有依赖资源,包含它自身,并保存在数组中返回。
  • release 通过 id(通常是资源 url)来释放一个资源或者一个资源数组。
  • releaseAsset 通过资源对象自身来释放资源。
  • releaseRes 释放通过 loadRes 加载的资源。
  • releaseResDir 释放通过 loadResDir 加载的资源。
  • releaseAll 释放所有资源。
  • setAutoRelease 设置当场景切换时是否自动释放资源。
  • setAutoReleaseRecursively 设置当场景切换时是否自动释放资源及资源引用的其它资源。
  • isAutoRelease 返回指定的资源是否有被设置为自动释放,不论场景的“Auto Release Assets”如何设置。
  • constructor 构造函数,通过一系列的 pipe 来构造一个新的 pipeline,pipes 将会在给定的顺序中被锁定。
  • insertPipe 在给定的索引位置插入一个新的 pipe。
  • insertPipeAfter 在当前 pipeline 的一个已知 pipe 后面插入一个新的 pipe。
  • appendPipe 添加一个新的 pipe 到 pipeline 尾部。
  • flowIn 让新的 item 流入 pipeline 中。
  • copyItemStates 从一个源 item 向所有目标 item 复制它的 pipe 状态,用于避免重复通过部分 pipe。
  • getItem 根据 id 获取一个 item
  • removeItem 移除指定的已完成 item。
  • clear 清空当前 pipeline,该函数将清理 items。

Details

属性(properties)

assetLoader
The asset loader in cc.loader's pipeline, it's by default the first pipe.It's used to identify an asset's type, and determine how to download it.
metadescription
类型Object
定义于cocos2d/core/load-pipeline/CCLoader.js:100
md5Pipe
The md5 pipe in cc.loader's pipeline, it could be absent if the project isn't build with md5 option.It's used to modify the url to the real downloadable url with md5 suffix.
metadescription
类型Object
定义于cocos2d/core/load-pipeline/CCLoader.js:108
downloader
The downloader in cc.loader's pipeline, it's by default the second pipe.It's used to download files with several handlers: pure text, image, script, audio, font, uuid.You can add your own download function with addDownloadHandlers
metadescription
类型Object
定义于cocos2d/core/load-pipeline/CCLoader.js:116
loader
The loader in cc.loader's pipeline, it's by default the third pipe.It's used to parse downloaded content with several handlers: JSON, image, plist, fnt, uuid.You can add your own download function with addLoadHandlers
metadescription
类型Object
定义于cocos2d/core/load-pipeline/CCLoader.js:125



#### 方法



##### getXMLHttpRequest


Gets a new XMLHttpRequest instance.

metadescription
返回XMLHttpRequest
定义于cocos2d/core/load-pipeline/CCLoader.js:155
addDownloadHandlers

Add custom supported types handler or modify existing type handler for download process.

metadescription
定义于cocos2d/core/load-pipeline/CCLoader.js:162
参数列表
  • extMap Object Custom supported types with corresponded handler
示例
  1. cc.loader.addDownloadHandlers({
  2. // This will match all url with `.scene` extension or all url with `scene` type
  3. 'scene' : function (url, callback) {}
  4. });
addLoadHandlers

Add custom supported types handler or modify existing type handler for load process.

metadescription
定义于cocos2d/core/load-pipeline/CCLoader.js:176
参数列表
  • extMap Object Custom supported types with corresponded handler
示例
  1. cc.loader.addLoadHandlers({
  2. // This will match all url with `.scene` extension or all url with `scene` type
  3. 'scene' : function (url, callback) {}
  4. });
load

Load resources with a progression callback and a complete callback.The progression callback is the same as Pipeline's onProgressThe complete callback is almost the same as Pipeline's onCompleteThe only difference is when user pass a single url as resources, the complete callback will set its result directly as the second parameter.

metadescription
定义于cocos2d/core/load-pipeline/CCLoader.js:190
参数列表
  • resources String | String[] | Object Url list in an array
  • progressCallback Function Callback invoked when progression change
    • completedCount Number The number of the items that are already completed
    • totalCount Number The total number of the items
    • item Object The latest item which flow out the pipeline
  • completeCallback Function Callback invoked when all resources loaded
示例
  1. cc.loader.load('a.png', function (err, tex) {
  2. cc.log('Result should be a texture: ' + (tex instanceof cc.Texture2D));
  3. });
  4. cc.loader.load('http://example.com/a.png', function (err, tex) {
  5. cc.log('Should load a texture from external url: ' + (tex instanceof cc.Texture2D));
  6. });
  7. cc.loader.load({url: 'http://example.com/getImageREST?file=a.png', type: 'png'}, function (err, tex) {
  8. cc.log('Should load a texture from RESTful API by specify the type: ' + (tex instanceof cc.Texture2D));
  9. });
  10. cc.loader.load(['a.png', 'b.json'], function (errors, results) {
  11. if (errors) {
  12. for (var i = 0; i < errors.length; i++) {
  13. cc.log('Error url [' + errors[i] + ']: ' + results.getError(errors[i]));
  14. }
  15. }
  16. var aTex = results.getContent('a.png');
  17. var bJsonObj = results.getContent('b.json');
  18. });
loadRes

Load resources from the "resources" folder inside the "assets" folder of your project.Note: All asset URLs in Creator use forward slashes, URLs using backslashes will not work.

metadescription
定义于cocos2d/core/load-pipeline/CCLoader.js:418
参数列表
  • url String Url of the target resource.
  1. The url is relative to the "resources" folder, extensions must be omitted.
  • type Function Only asset of type will be loaded if this argument is supplied.
  • progressCallback Function Callback invoked when progression change.
    • completedCount Number The number of the items that are already completed.
    • totalCount Number The total number of the items.
    • item Object The latest item which flow out the pipeline.
  • completeCallback Function Callback invoked when the resource loaded.
    • error Error The error info or null if loaded successfully.
    • resource Object The loaded resource if it can be found otherwise returns null.
示例
  1. // load the prefab (project/assets/resources/misc/character/cocos) from resources folder
  2. cc.loader.loadRes('misc/character/cocos', function (err, prefab) {
  3. if (err) {
  4. cc.error(err.message || err);
  5. return;
  6. }
  7. cc.log('Result should be a prefab: ' + (prefab instanceof cc.Prefab));
  8. });
  9. // load the sprite frame of (project/assets/resources/imgs/cocos.png) from resources folder
  10. cc.loader.loadRes('imgs/cocos', cc.SpriteFrame, function (err, spriteFrame) {
  11. if (err) {
  12. cc.error(err.message || err);
  13. return;
  14. }
  15. cc.log('Result should be a sprite frame: ' + (spriteFrame instanceof cc.SpriteFrame));
  16. });
loadResArray

This method is like loadRes except that it accepts array of url.

metadescription
定义于cocos2d/core/load-pipeline/CCLoader.js:541
参数列表
  • urls String[] Array of URLs of the target resource.
  1. The url is relative to the "resources" folder, extensions must be omitted.
  • type Function Only asset of type will be loaded if this argument is supplied.
  • progressCallback Function Callback invoked when progression change.
    • completedCount Number The number of the items that are already completed.
    • totalCount Number The total number of the items.
    • item Object The latest item which flow out the pipeline.
  • completeCallback 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
  1. with the error. If all assets are loaded successfully, error will be null.
  1. If nothing to load, assets will be an empty array.
示例
  1. // load the SpriteFrames from resources folder
  2. var spriteFrames;
  3. var urls = ['misc/characters/character_01', 'misc/weapons/weapons_01'];
  4. cc.loader.loadResArray(urls, cc.SpriteFrame, function (err, assets) {
  5. if (err) {
  6. cc.error(err);
  7. return;
  8. }
  9. spriteFrames = assets;
  10. // ...
  11. });
loadResDir

Load all assets in a folder inside the "assets/resources" folder of your project.Note: All asset URLs in Creator use forward slashes, URLs using backslashes will not work.

metadescription
定义于cocos2d/core/load-pipeline/CCLoader.js:602
参数列表
  • url String Url of the target folder.
  1. The url is relative to the "resources" folder, extensions must be omitted.
  • type Function Only asset of type will be loaded if this argument is supplied.
  • progressCallback Function Callback invoked when progression change.
    • completedCount Number The number of the items that are already completed.
    • totalCount Number The total number of the items.
    • item Object The latest item which flow out the pipeline.
  • completeCallback 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
  1. with the error. If all assets are loaded successfully, error will be null.
  1. If nothing to load, assets will be an empty array.
  • urls String[] An array that lists all the URLs of loaded assets.
示例
  1. // load the texture (resources/imgs/cocos.png) and the corresponding sprite frame
  2. cc.loader.loadResDir('imgs/cocos', function (err, assets) {
  3. if (err) {
  4. cc.error(err);
  5. return;
  6. }
  7. var texture = assets[0];
  8. var spriteFrame = assets[1];
  9. });
  10. // load all textures in "resources/imgs/"
  11. cc.loader.loadResDir('imgs', cc.Texture2D, function (err, textures) {
  12. var texture1 = textures[0];
  13. var texture2 = textures[1];
  14. });
  15. // load all JSONs in "resources/data/"
  16. cc.loader.loadResDir('data', function (err, objects, urls) {
  17. var data = objects[0];
  18. var url = urls[0];
  19. });
getRes

Get resource data by id. When you load resources with load or loadRes,the url will be the unique identity of the resource.After loaded, you can acquire them by passing the url to this API.

metadescription
返回Any
定义于cocos2d/core/load-pipeline/CCLoader.js:681
参数列表
  • url String
  • type Function Only asset of type will be returned if this argument is supplied.
getDependsRecursively

获取一个指定资源的所有依赖资源,包含它自身,并保存在数组中返回。owner 参数接收以下几种类型:1. 资源 asset 对象;2. 资源目录下的 url;3. 资源的 uuid。返回的数组将仅保存依赖资源的 uuid,获取这些 uuid 后,你可以从 loader 释放这些资源;通过 getRes 获取某个资源或者进行其他你需要的操作。想要释放一个资源及其依赖资源,可以参考 release。下面是一些示例代码:

metadescription
返回Array
定义于cocos2d/core/load-pipeline/CCLoader.js:718
参数列表
示例
  1. // Release all dependencies of a loaded prefab
  2. var deps = cc.loader.getDependsRecursively(prefab);
  3. cc.loader.release(deps);
  4. // Retrieve all dependent textures
  5. var deps = cc.loader.getDependsRecursively('prefabs/sample');
  6. var textures = [];
  7. for (var i = 0; i < deps.length; ++i) {
  8. var item = cc.loader.getRes(deps[i]);
  9. if (item instanceof cc.Texture2D) {
  10. textures.push(item);
  11. }
  12. }
release

通过 id(通常是资源 url)来释放一个资源或者一个资源数组。从 v1.3 开始,这个方法不仅会从 loader 中删除资源的缓存引用,还会清理它的资源内容。比如说,当你释放一个 texture 资源,这个 texture 和它的 gl 贴图数据都会被释放。在复杂项目中,我们建议你结合 getDependsRecursively 来使用,便于在设备内存告急的情况下更快地释放不再需要的资源的内存。注意,这个函数可能会导致资源贴图或资源所依赖的贴图不可用,如果场景中存在节点仍然依赖同样的贴图,它们可能会变黑并报 GL 错误。如果你只想删除一个资源的缓存引用,请使用 Pipeline/removeItem:method

metadescription
定义于cocos2d/core/load-pipeline/CCLoader.js:759
参数列表
示例
  1. // Release a texture which is no longer need
  2. cc.loader.release(texture);
  3. // Release all dependencies of a loaded prefab
  4. var deps = cc.loader.getDependsRecursively('prefabs/sample');
  5. cc.loader.release(deps);
  6. // If there is no instance of this prefab in the scene, the prefab and its dependencies like textures, sprite frames, etc, will be freed up.
  7. // If you have some other nodes share a texture in this prefab, you can skip it in two ways:
  8. // 1. Forbid auto release a texture before release
  9. cc.loader.setAutoRelease(texture2d, false);
  10. // 2. Remove it from the dependencies array
  11. var deps = cc.loader.getDependsRecursively('prefabs/sample');
  12. var index = deps.indexOf(texture2d._uuid);
  13. if (index !== -1)
  14. deps.splice(index, 1);
  15. cc.loader.release(deps);
releaseAsset

通过资源对象自身来释放资源。详细信息请参考 release

metadescription
定义于cocos2d/core/load-pipeline/CCLoader.js:822
参数列表
releaseRes

释放通过 loadRes 加载的资源。详细信息请参考 release

metadescription
定义于cocos2d/core/load-pipeline/CCLoader.js:836
参数列表
  • url String
  • type Function Only asset of type will be released if this argument is supplied.
releaseResDir

释放通过 loadResDir 加载的资源。详细信息请参考 release

metadescription
定义于cocos2d/core/load-pipeline/CCLoader.js:854
参数列表
  • url String
  • type Function Only asset of type will be released if this argument is supplied.
releaseAll

释放所有资源。详细信息请参考 release

metadescription
定义于cocos2d/core/load-pipeline/CCLoader.js:870
setAutoRelease

设置当场景切换时是否自动释放资源。默认情况下,当加载新场景时,旧场景的资源根据旧场景是否勾选“Auto Release Assets”,将会被释放或者保留。而使用 cc.loader.loadRescc.loader.loadResDir 动态加载的资源,则不受场景设置的影响,默认不自动释放。使用这个 API 可以在单个资源上改变这个默认行为,强制在切换场景时保留或者释放指定资源。参考:cc.loader.setAutoReleaseRecursivelycc.loader.isAutoRelease

metadescription
定义于cocos2d/core/load-pipeline/CCLoader.js:891
参数列表
  • assetOrUrlOrUuid Asset | String asset object or the raw asset's url or uuid
  • autoRelease Boolean indicates whether should release automatically
示例
  1. // auto release the texture event if "Auto Release Assets" disabled in current scene
  2. cc.loader.setAutoRelease(texture2d, true);
  3. // don't release the texture even if "Auto Release Assets" enabled in current scene
  4. cc.loader.setAutoRelease(texture2d, false);
  5. // first parameter can be url
  6. cc.loader.setAutoRelease(audioUrl, false);
setAutoReleaseRecursively

设置当场景切换时是否自动释放资源及资源引用的其它资源。默认情况下,当加载新场景时,旧场景的资源根据旧场景是否勾选“Auto Release Assets”,将会被释放或者保留。而使用 cc.loader.loadRescc.loader.loadResDir 动态加载的资源,则不受场景设置的影响,默认不自动释放。使用这个 API 可以在指定资源及资源递归引用到的所有资源上改变这个默认行为,强制在切换场景时保留或者释放指定资源。参考:cc.loader.setAutoReleasecc.loader.isAutoRelease

metadescription
定义于cocos2d/core/load-pipeline/CCLoader.js:931
参数列表
  • assetOrUrlOrUuid Asset | String asset object or the raw asset's url or uuid
  • autoRelease Boolean indicates whether should release automatically
示例
  1. // auto release the SpriteFrame and its Texture event if "Auto Release Assets" disabled in current scene
  2. cc.loader.setAutoReleaseRecursively(spriteFrame, true);
  3. // don't release the SpriteFrame and its Texture even if "Auto Release Assets" enabled in current scene
  4. cc.loader.setAutoReleaseRecursively(spriteFrame, false);
  5. // don't release the Prefab and all the referenced assets
  6. cc.loader.setAutoReleaseRecursively(prefab, false);
isAutoRelease

返回指定的资源是否有被设置为自动释放,不论场景的“Auto Release Assets”如何设置。参考:cc.loader.setAutoReleasecc.loader.setAutoReleaseRecursively

metadescription
返回Boolean
定义于cocos2d/core/load-pipeline/CCLoader.js:979
参数列表
  • assetOrUrl Asset | String asset object or the raw asset's url
constructor

构造函数,通过一系列的 pipe 来构造一个新的 pipeline,pipes 将会在给定的顺序中被锁定。一个 pipe 就是一个对象,它包含了字符串类型的 ‘id’ 和 ‘handle’ 函数,在 pipeline 中 id 必须是唯一的。它还可以包括 ‘async’ 属性以确定它是否是一个异步过程。

metadescription
定义于cocos2d/core/load-pipeline/pipeline.js:112
参数列表
示例
  1. var pipeline = new Pipeline([
  2. {
  3. id: 'Downloader',
  4. handle: function (item, callback) {},
  5. async: true
  6. },
  7. {id: 'Parser', handle: function (item) {}, async: false}
  8. ]);
insertPipe

在给定的索引位置插入一个新的 pipe。一个 pipe 必须包含一个字符串类型的 ‘id’ 和 ‘handle’ 函数,该 id 在 pipeline 必须是唯一标识。

metadescription
定义于cocos2d/core/load-pipeline/pipeline.js:156
参数列表
  • pipe Object The pipe to be inserted
  • index Number The index to insert
insertPipeAfter

!enInsert a pipe to the end of an existing pipe. The existing pipe must be a valid pipe in the pipeline.!zh在当前 pipeline 的一个已知 pipe 后面插入一个新的 pipe。

metadescription
定义于cocos2d/core/load-pipeline/pipeline.js:199
参数列表
  • refPipe Object An existing pipe in the pipeline.
  • newPipe Object The pipe to be inserted.
appendPipe

添加一个新的 pipe 到 pipeline 尾部。 该 pipe 必须包含一个字符串类型 ‘id’ 和 ‘handle’ 函数,该 id 在 pipeline 必须是唯一标识。

metadescription
定义于cocos2d/core/load-pipeline/pipeline.js:216
参数列表
  • pipe Object The pipe to be appended
flowIn

让新的 item 流入 pipeline 中。这里的每个 item 可以是一个简单字符串类型的 url 或者是一个对象,如果它是一个对象的话,他必须要包含 ‘id’ 属性。你也可以指定它的 ‘type’ 属性类型,默认情况下,该类型是 ‘url’ 的后缀名。也通过添加一个 包含 ‘skips’ 属性的 item 对象,你就可以跳过 skips 中包含的 pipe。该对象可以包含任何附加属性。

metadescription
定义于cocos2d/core/load-pipeline/pipeline.js:240
参数列表
示例
  1. pipeline.flowIn([
  2. 'res/Background.png',
  3. {
  4. id: 'res/scene.json',
  5. type: 'scene',
  6. name: 'scene',
  7. skips: ['Downloader']
  8. }
  9. ]);
copyItemStates

从一个源 item 向所有目标 item 复制它的 pipe 状态,用于避免重复通过部分 pipe。当一个源 item 生成了一系列新的 items 时很有用,你希望让这些新的依赖项进入 pipeline,但是又不希望它们通过源 item 已经经过的 pipe,但是你可能希望他们源 item 已经通过并跳过所有 pipes,这个时候就可以使用这个 API。

metadescription
定义于cocos2d/core/load-pipeline/pipeline.js:325
参数列表
  • srcItem Object The source item
  • dstItems Array | Object A single destination item or an array of destination items
getItem

根据 id 获取一个 item

metadescription
返回Object
定义于cocos2d/core/load-pipeline/pipeline.js:354
参数列表
removeItem

移除指定的已完成 item。这将仅仅从 pipeline 或者 loader 中删除其缓存,并不会释放它所依赖的资源。cc.loader 中提供了另一种删除资源及其依赖的清理方法,请参考 cc.loader.release

metadescription
返回Boolean
定义于cocos2d/core/load-pipeline/pipeline.js:374
参数列表
clear

清空当前 pipeline,该函数将清理 items。

metadescription
定义于cocos2d/core/load-pipeline/pipeline.js:394