Skip to content

Commit

Permalink
adjust example for cc.assetManager (cocos#776)
Browse files Browse the repository at this point in the history
  • Loading branch information
SantyWang authored and knoxHuang committed Nov 11, 2019
1 parent 741774a commit e74358c
Show file tree
Hide file tree
Showing 41 changed files with 726 additions and 728 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ cc.Class({
onLoad: function () {
var animation = this.getComponent(cc.Animation);

cc.loader.loadRes("test_assets/atlas", cc.SpriteAtlas, (err, atlas) => {
cc.assetManager.loadRes("test_assets/atlas", cc.SpriteAtlas, (err, atlas) => {
var spriteFrames = atlas.getSpriteFrames();

var clip = cc.AnimationClip.createWithSpriteFrames(spriteFrames, 10);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@ cc.Class({
Txt: "test_assets/text",
Texture: "test_assets/PurpleMonster",
Font: "test_assets/font",
Plist: "test_assets/atom.plist",
Plist: "test_assets/atom",
SpriteFrame: "test_assets/image",
Prefab: "test_assets/prefab",
Animation: "test_assets/sprite-anim",
Expand All @@ -42,7 +42,7 @@ cc.Class({

onDestroy () {
if (this._curRes) {
cc.loader.releaseAsset(this._curRes);
cc.assetManager.release(this._curRes);
}
},

Expand Down Expand Up @@ -85,32 +85,34 @@ cc.Class({
switch (this._curType) {
case 'SpriteFrame':
// specify the type to load sub asset from texture's url
cc.loader.loadRes(url, cc.SpriteFrame, loadCallBack);
cc.assetManager.loadRes(url, cc.SpriteFrame, loadCallBack);
break;
case 'Spine':
// specify the type to avoid the duplicated name from spine atlas
cc.loader.loadRes(url, sp.SkeletonData, loadCallBack);
cc.assetManager.loadRes(url, sp.SkeletonData, loadCallBack);
break;
case 'Font':
cc.loader.loadRes(url, cc.Font, loadCallBack);
cc.assetManager.loadRes(url, cc.Font, loadCallBack);
break;
case 'Plist':
cc.loader.loadRes(url, cc.ParticleAsset, loadCallBack);
cc.assetManager.loadRes(url, cc.ParticleAsset, loadCallBack);
break;
case 'Animation':
case 'Prefab':
case 'Scene':
case 'Texture':
case 'Txt':
case 'Audio':
cc.loader.loadRes(url, loadCallBack);
cc.assetManager.loadRes(url, loadCallBack);
break;
case 'Scene':
cc.assetManager.loadScene(url, loadCallBack);
break;
case 'CORS':
cc.loader.load(url, loadCallBack);
this.loadTips.textKey = "CORS image should report texImage2D error under WebGL and works ok under Canvas"
cc.assetManager.loadRemoteTexture(url, loadCallBack);
this.loadTips.textKey = "CORS image should report texImage2D error under WebGL and works ok under Canvas";
break;
default:
cc.loader.load(url, loadCallBack);
cc.assetManager.load({url}, loadCallBack);
break;
}
},
Expand Down Expand Up @@ -141,7 +143,7 @@ cc.Class({

_onShowResClick: function (event) {
if (this._curType === "Scene") {
cc.director.runScene(this._curRes.scene);
cc.director.runScene(this._curRes);

return;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -13,9 +13,8 @@ cc.Class({
loadSpriteFrame: function () {
var url = this._url[0];
this._releaseResource(url, cc.SpriteAtlas);
cc.loader.loadRes(url, cc.SpriteAtlas, (err, atlas) => {
cc.assetManager.loadRes(url, cc.SpriteAtlas, (err, atlas) => {
this._removeAllChildren();
cc.loader.setAutoRelease(atlas, true);
var node = new cc.Node();
this.content.addChild(node);
node.position = cc.v2(0, 0);
Expand All @@ -27,9 +26,8 @@ cc.Class({
loadPrefab: function () {
var url = this._url[1];
this._releaseResource(url, cc.Prefab);
cc.loader.loadRes(url, cc.Prefab, (err, prefab) => {
cc.assetManager.loadRes(url, cc.Prefab, (err, prefab) => {
this._removeAllChildren();
cc.loader.setAutoRelease(prefab, true);
var node = cc.instantiate(prefab);
this.content.addChild(node);
node.position = cc.v2(0, 0);
Expand All @@ -47,8 +45,6 @@ cc.Class({

_releaseResource: function (url, type) {
this._removeAllChildren();
var res = cc.loader.getRes(url, type);
var all = cc.loader.getDependsRecursively(res);
cc.loader.release(all);
cc.assetManager.releaseRes(url, type);
}
});
Original file line number Diff line number Diff line change
Expand Up @@ -29,9 +29,7 @@ cc.Class({
this.scrollView.content.removeAllChildren(true);
for (var i = 0; i < this._assets.length; ++i) {
var asset = this._assets[i];
// 需要释放所有资源依赖
var deps = cc.loader.getDependsRecursively(asset);
cc.loader.release(deps);
cc.assetManager.release(asset);
}
},

Expand All @@ -50,7 +48,7 @@ cc.Class({
this.scrollView.scrollToTop();
this.btnClearAll.active = false; // 防止加载的过程中清除资源

cc.loader.loadResDir("test_assets", (err, assets) => {
cc.assetManager.loadResDir("test_assets", (err, assets) => {
if (!this.isValid) {
return;
}
Expand Down Expand Up @@ -83,7 +81,7 @@ cc.Class({
this.scrollView.scrollToTop();
this.btnClearAll.active = false; // 防止加载的过程中清除资源

cc.loader.loadResDir("test_assets", cc.SpriteFrame, (err, assets) => {
cc.assetManager.loadResDir("test_assets", cc.SpriteFrame, (err, assets) => {
if (!this.isValid) {
return;
}
Expand Down
5 changes: 1 addition & 4 deletions assets/cases/05_scripting/08_module/Monster.js
Original file line number Diff line number Diff line change
Expand Up @@ -36,15 +36,12 @@ cc.Class({
this.defense.string = info.defense;

var image = this.image;
cc.loader.loadRes(info.imageUrl, cc.SpriteFrame, function (error, spriteFrame) {
cc.assetManager.loadRes(info.imageUrl, cc.SpriteFrame, function (error, spriteFrame) {
if (!error) {
image.spriteFrame = spriteFrame;
}
});

//cc.loader.load(, function (error, res) {
// console.log(res);
//}.bind(this));
}

// called every frame, uncomment this function to activate update callback
Expand Down
19 changes: 9 additions & 10 deletions assets/cases/05_scripting/10_loadingBar/LoadingBarCtrl.js
Original file line number Diff line number Diff line change
Expand Up @@ -27,29 +27,28 @@ cc.Class({
// use this for initialization
onLoad: function () {
this._urls = [
cc.url.raw("resources/audio/ding.wav"),
cc.url.raw("resources/audio/cheering.wav"),
cc.url.raw("resources/audio/music_logo.mp3"),
cc.url.raw("resources/test_assets/audio.mp3"),
cc.url.raw("resources/loadingBar/font.png"),
cc.url.raw("resources/loadingBar/mikado_outline_shadow.png"),
cc.url.raw("resources/loadingBar/enligsh-chinese.png")
'audio/ding',
'audio/cheering',
'audio/music_logo',
'test_assets/audio',
'loadingBar/font',
'loadingBar/mikado_outline_shadow',
'loadingBar/enligsh-chinese'
];
this.resource = null;
this.progressBar.progress = 0;
this._clearAll();
this.progressTips.textKey = i18n.t("cases/05_scripting/10_loadingBar/LoadingBarCtrl.js.3");
this.node.on(cc.Node.EventType.TOUCH_START, function () {
if (this.resource) { return; }
cc.loader.load(this._urls, this._progressCallback.bind(this), this._completeCallback.bind(this));
cc.assetManager.loadRes(this._urls, this._progressCallback.bind(this), this._completeCallback.bind(this));
}, this);
},

_clearAll: function () {
for (var i = 0; i < this._urls.length; ++i) {
var url = this._urls[i];
var deps = cc.loader.getDependsRecursively(url);
cc.loader.release(deps);
cc.assetManager.releaseRes(url);
}
},

Expand Down
4 changes: 2 additions & 2 deletions assets/cases/05_scripting/11_network/NetworkCtrl.js
Original file line number Diff line number Diff line change
Expand Up @@ -57,7 +57,7 @@ cc.Class({
},

sendXHR: function () {
var xhr = cc.loader.getXMLHttpRequest();
var xhr = new XMLHttpRequest();
this.streamXHREventsToLabel(xhr, this.xhr, this.xhrResp, 'GET');

xhr.open("GET", "https://httpbin.org/get?show_env=1", true);
Expand All @@ -74,7 +74,7 @@ cc.Class({
},

sendXHRAB: function () {
var xhr = cc.loader.getXMLHttpRequest();
var xhr = new XMLHttpRequest();
this.streamXHREventsToLabel(xhr, this.xhrAB, this.xhrABResp, "POST");

xhr.open("POST", "https://httpbin.org/post");
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,7 @@ cc.Class({
}
switch (task.requestURL) {
case this.imgUrl:
cc.loader.load(task.storagePath, (err, tex) => {
cc.assetManager.loadRemoteTexture(task.storagePath, (err, tex) => {
this.sprite.spriteFrame = new cc.SpriteFrame(tex);
this.sprite.node.active = true;
this.label.node.active = false;
Expand All @@ -59,7 +59,7 @@ cc.Class({
this.sprite.node.active = false;
this.label.node.active = true;
this.label.string = 'Audio Download Complete.';
cc.loader.load(task.storagePath, (err, clip) => {
cc.assetManager.loadRemoteAudio(task.storagePath, (err, clip) => {
this._audioID = cc.audioEngine.play(clip);
});
}
Expand Down Expand Up @@ -96,7 +96,7 @@ cc.Class({

this._downloading = true;
this.label.string = 'Downloading image (mem)';
cc.loader.load(this.tempImgUrl, (error, tex) => {
cc.assetManager.loadRemoteTexture(this.tempImgUrl, (error, tex) => {
this._downloading = false;
if (error) {
console.log("Load remote image failed: " + error);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@ cc.Class({
this._audioUrl = this.inputUrlBox.string;
// download audio
if (this._audioUrl) {
cc.loader.load({url: this._audioUrl, type: 'mp3'}, this.onProgress.bind(this), this.audioLoadComplete.bind(this));
cc.assetManager.loadRemoteAudio(this._audioUrl, this.audioLoadComplete.bind(this));
this.remindLabel.textKey = i18n.t('cases/05_scripting/11_network/download-web.fire.2');
}
else {
Expand All @@ -45,10 +45,6 @@ cc.Class({
this._audioPlayer.setAudioTask(res);
},

onProgress (completedCount, totalCount) {
this.remindLabel.textKey = `${i18n.t('cases/05_scripting/11_network/download-web.fire.3')} ${(completedCount/totalCount) * 100}`;
},

update () {
this._audioPlayer.currentTime();
},
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@ cc.Class({
this._picUrl = this.inputUrlBox.string;
// download pic
if (this._picUrl) {
cc.loader.load({url: this._picUrl, type: 'png'}, this.onProgress.bind(this), this.picLoadComplete.bind(this));
cc.assetManager.loadRemoteTexture(this._picUrl, this.picLoadComplete.bind(this));
this.remindLabel.textKey = i18n.t('cases/05_scripting/11_network/download-web.fire.2');
}
else {
Expand All @@ -48,10 +48,6 @@ cc.Class({
let spriteFrame = new cc.SpriteFrame(res);
this.picNode.spriteFrame = spriteFrame;
this.picNode.node.active = true;
},

onProgress (completedCount, totalCount) {
this.remindLabel.textKey = `${i18n.t('cases/05_scripting/11_network/download-web.fire.3')} ${(completedCount/totalCount) * 100}`;
},
}

});
2 changes: 1 addition & 1 deletion assets/cases/3d/dynamic-load-material.js
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ cc.Class({

start () {
this.scheduleOnce(() => {
cc.loader.loadRes('materials/dynamic-load-material', cc.Material, (err, material) => {
cc.assetManager.loadRes('materials/dynamic-load-material', cc.Material, (err, material) => {
if (err) {
cc.error(err);
return;
Expand Down
Original file line number Diff line number Diff line change
@@ -1,7 +1,8 @@
{
"ver": "1.0.1",
"ver": "1.1.0",
"uuid": "c25acf3a-52ab-4fad-a085-1b5fdacd5358",
"bundleMode": "none",
"bundleName": "",
"isSubpackage": false,
"subpackageName": "",
"subMetas": {}
}
Loading

0 comments on commit e74358c

Please sign in to comment.