CocosCreator 动态加载与远程加载资源汇总

版权声明:本文为博主原创文章,欢迎转载,转载标明出处(http://blog.csdn.net/zyw_java)。微信公众号:javenlife https://blog.csdn.net/zyw_java/article/details/88078085

CocosCreator 动态加载与远程加载资源汇总

概述

  • 所有需要通过 cc.loader.loadRes 动态加载的资源,都必须放置在 resources 文件夹或它的子文件夹下。如果一份资源仅仅是被 resources 中的其它资源所依赖,而不需要直接被 cc.loader.loadRes 调用,那么请不要放在 resources 文件夹里。

  • resources 文件夹里面的资源,可以引用文件夹外部的其它资源,同样也可以被外部场景或资源引用到。项目构建时,除了已在构建发布面板勾选的场景外,resources 文件夹里面的所有资源,连同它们关联依赖的 resources 文件夹外部的资源,都会被导出。

  • 关于 resources 的更多信息,可参考文档:获取和加载资源 - 动态加载 Asset

动态加载资源

资源下载

链接: 百度网盘 提取码: pj6f

动态加载预制资源


newNode(position, callBack) {
    let newNode = null;
    if (!this.nodePool) {
        this.nodePool = new cc.NodePool();
    }
    if (this.nodePool.size() > 0) {
        newNode = this.nodePool.get();
        this.setNode(newNode, position, callBack);
    } else {
        cc.loader.loadRes("prefab/leftOright", cc.Prefab, function (err, prefab) {
            if (err) {
                return;
            }
            newNode = cc.instantiate(prefab);
            this.setNode(newNode, position, callBack);
        }.bind(this));
    }
},

动态加载字体

    setCurrentWater(type, number) {
        let currentWaterLabel = this.targetWaterNode.getChildByName("current").getComponent(cc.Label);
        currentWaterLabel.string = number;
        this.setFont("fonts/Yellowchars", currentWaterLabel);
    },

    /**
     * 设置字体
     * @param {*} url
     * @param {*} label 
     */
    setFont(url, label) {
        //动态加载字体
        cc.loader.loadRes(url, cc.Font, function (err, res) {
            if (err) {
                return;
            }
            label.font = res;
        });
    },

动态加载龙骨资源

异步动态加载龙骨资源

//异步加载龙骨动画
this.runAsyncGetDragonRes("huajipao", this._currentTimeScale)
    .then(function (data) {
        console.log("回调:" + data.timeScale);
        that.armatureDisPlay = data;
    })
    .catch(function (reason) {
        console.log(reason);
    });


/**
 * 获取龙骨资源并播放动画
 * @param {*} dragonName 龙骨文件夹名称 
 * @param {*} timeScale 动画缩放率
 */
runAsyncGetDragonRes(dragonName, timeScale) {
    var p = new Promise(function (resolve, reject) {
        cc.loader.loadResDir('dragon/' + dragonName, function (err, assets) {
            if (err) {
                return;
            }
            if (assets.length <= 0) {
                return;
            }
            var newHero = new cc.Node();
            
            this.node.getChildByName("players").addChild(newHero);
            newHero.setPosition(cc.v2(0, 0));
            newHero.setScale(1, 1);
            let armatureDisPlay = newHero.addComponent(dragonBones.ArmatureDisplay);
            for (let i in assets) {
                if (assets[i] instanceof dragonBones.DragonBonesAsset) {
                    armatureDisPlay.dragonAsset = assets[i];
                }
                if (assets[i] instanceof dragonBones.DragonBonesAtlasAsset) {
                    armatureDisPlay.dragonAtlasAsset = assets[i];
                }
            }
            armatureDisPlay.armatureName = 'Armature';//龙骨支干
            armatureDisPlay.playAnimation('newAnimation');//龙骨动画名称
            armatureDisPlay.timeScale = timeScale;
            resolve(armatureDisPlay);
        }.bind(this));
    }.bind(this));
    return p;
},

动态加载声音

 cc.loader.loadRes("music/bgm", cc.AudioClip, function (err, clip) {
     if (err) {
         return;
     }
     this._dynamicAudio = clip;
 }.bind(this));


playBgmAudio() {
  if (Global.isAudio && this._dynamicAudio && (!this.bgmAudioId || this.getState(this.bgmAudioId) != cc.audioEngine.AudioState.PLAYING)) {
    this.bgmAudioId = cc.audioEngine.play(this._dynamicAudio, true, 1);
  }
},
    
getState(id) {
  return cc.audioEngine.getState(id);
},

远程加载

远程加载图片

// 设置图片
function setImg(imgNode, spriteFrame) {
    imgNode.getComponent(cc.Sprite).spriteFrame = spriteFrame;
}
// 加载远程资源
function loadImgByUrl(imgNode, remoteUrl, imageType) {
    if (!imageType) {
        imageType = "png";
    }
    cc.loader.load({
        url: remoteUrl,
        type: imageType
    }, function (err, texture) {
        if (err) {
            return;
        }
        setImg(imgNode, new cc.SpriteFrame(texture));
    });
}
// 用绝对路径加载设备存储内的资源,比如相册
function loadLocal(imgNode, absolutePath) {
    cc.loader.load(absolutePath, function (err, texture) {
        if (err) {
            return;
        }
        setImg(imgNode, new cc.SpriteFrame(texture));
    });
}

猜你喜欢

转载自blog.csdn.net/zyw_java/article/details/88078085