resources
文件夹里面的资源,可以关联依赖到文件夹外部的其它资源,同样也可以被外部场景或资源引用到。项目构建时,除了已在 构建发布 面板勾选的场景外,resources
文件夹里面的所有资源,连同它们关联依赖的resources
文件夹外部的资源,都会被导出。所以如果一份资源不需要由脚本直接动态加载,那么不用放在resources
文件夹里。
动态加载 Asset
Creator 提供了 cc.loader.loadRes
这个 API 来专门加载那些位于 resources 目录下的 Asset。和 cc.loader.load
不同的是,loadRes 一次只能加载单个 Asset。调用时,你只要传入相对 resources 的路径即可,并且路径的结尾处不能包含文件扩展名。
// 加载 Prefab
cc.loader.loadRes("test assets/prefab", function (err, prefab) {
var newNode = cc.instantiate(prefab);
cc.director.getScene().addChild(newNode);
});
// 加载 AnimationClip
cc.loader.loadRes("test assets/anim", function (err, clip) {
myNode.getComponent(cc.Animation).addClip(clip, "anim");
});
// 加载 SpriteAtlas(图集),并且获取其中的一个 SpriteFrame
// 注意 atlas 资源文件(plist)通常会和一个同名的图片文件(png)放在一个目录下, 所以需要在第二个参数指定资源类型
cc.loader.loadRes("test assets/sheep", cc.SpriteAtlas, function (err, atlas) {
var frame = atlas.getSpriteFrame('sheep_down_0');
sprite.spriteFrame = frame;
});
例子:
var personOb = { name:"神秘人", icon:"pic/map_person/NPC1", position:cc.p(100,-100) } this._buildPersonWithOb(personOb)
_addSpriteFrameToContainer:function(container, name){ //动态添加图片 container.spriteFrame = this.loadWaitImage cc.loader.loadRes(name, cc.SpriteFrame, function (err, atlas) { container.spriteFrame = atlas }); //延迟动态添加图片 // container.spriteFrame = this.loadWaitImage // cc.loader.loadRes(name, cc.SpriteFrame, function (err, atlas) { // setTimeout(function() { // container.spriteFrame = atlas // }, 1000); //}); }, _buildPersonWithOb:function(personOb){ var that = this cc.loader.loadRes("prefab/person",function (err, prefab) { //实例化预制件 var newNode = cc.instantiate(prefab); //延迟添加图片属性 that._addSpriteFrameToContainer(newNode.getChildByName('pic').getComponent(cc.Sprite), personOb.icon) //给预制件添加图片属性 // var container = newNode.getChildByName('pic').getComponent(cc.Sprite) // var atlas = personOb.icon // container.spriteFrame = atlas // newNode.getChildByName("pic").getComponent(cc.Sprite),personOb.icon //给预制件添加文本框属性 newNode.getChildByName('name').getComponent(cc.Label).string = personOb.name //给预制件添加坐标 newNode.setPosition(personOb.position) //给预制件添加到背景中 that.NPC.addChild(newNode) }) },
这是在resources里面的预存的图片和预制件的添加。