cocos creator基礎(chǔ)-(十三)cc.Loader使用

1: 掌握cc.loader加載本地資源;

2: 掌握cc.loader加載遠程資源;

3: 掌握資源釋放的機制與autorelease;

4: 掌握手動釋放資源;


cc.Loader


1:有三個默認的Pipeline:

  (1) assetLoader: 主要用于加載資源, 加載asset類型資源,和釋放這些資源;

  (2) downloader: 主要用于下載文件, 文本,圖像,腳本,聲音,字體, 自定義的download;

  (3) loader: 第三個默認的Pipeline,可以加載json, image, plist, fnt, uuid;

2: 資源分為本地(assets目錄下)與遠程資源;

3: 加載異步的;



本地資源加載


1: 在代碼里面加載資源必須要求資源在assets/resources/文件夾下;

  (1)根據(jù)場景的依賴關(guān)系來打包我們的資源; 去掉不用的資源

  (2)無法判斷在代碼里面加載的資源,是哪些?-->所有的resources目錄下的資源,都會被打包

  進去,你在代碼里面就能加載到它了。

  如果資源不在代碼里面加載,一般不要放到resources目錄下 ,如果放到了,這個資源不管你有沒有用,都會被打包進去;

2: 資源的url不需要加assets/resources這個部分,路徑不需要加這個前綴;

3:cc.loader.loadRes(url, onComplete回掉函數(shù));

4: loadResArray([], type類型, progressCallback, completeCallback);

5: loadResDir (url [type ] [progressCallback ] [completeCallback ]) 加載一個路徑下的資源;

????cc.loader.loadResDir( // 加載resources目錄下的所有資源

????????"/", function(){});

6: getRes(url, [type]); 獲取資源id;


遠程資源加載


1: cc.loader.load(url, 結(jié)束回掉函數(shù));

2: cc.loader.load({url: “”, type: “”}, 結(jié)束回掉函數(shù));

3: 遠程加載任意類型文件;

?

資源卸載

1:每個場景有個自動釋放資源

  勾選上這個場景的資源會自動釋放,不勾選上這個場景的資源不釋放

2: 代碼加載的資源,默認是不會受場景這個選項的影響的,除非設(shè)置

  cc.loader.setAutoRelease(url, brealse),在場景自動釋放的時候,代碼資源也會釋放

3: 手動釋放資源

  loadRes/ releaseRes

  load / release

  releaseAsset (資源對象的object);

cc.Class({

? ? extends: cc.Component,

? ? properties: {

? ? ? ? // foo: {

? ? ? ? // ? ?default: null, ? ? ?// The default value will be used only when the component attaching

? ? ? ? // ? ? ? ? ? ? ? ? ? ? ? ? ? to a node for the first time

? ? ? ? // ? ?url: cc.Texture2D, ?// optional, default is typeof default

? ? ? ? // ? ?serializable: true, // optional, default is true

? ? ? ? // ? ?visible: true, ? ? ?// optional, default is true

? ? ? ? // ? ?displayName: 'Foo', // optional

? ? ? ? // ? ?readonly: false, ? ?// optional, default is false

? ? ? ? // },

? ? ? ? // ...

? ? ? ? audio: {

? ? ? ? ? ? type: cc.AudioSource,

? ? ? ? ? ? default: null,

? ? ? ? },

? ? ? ? sprite: {

? ? ? ? ? ? type: cc.Sprite,

? ? ? ? ? ? default: null,

? ? ? ? },

? ? },

? ? // use this for initialization

? ? onLoad: function () {

? ? },

? ? start: function() {

? ? ? ? // this.local_load();

? ? ? ? this.remote_load();

? ? },

? ? remote_load: function() {

? ? ? ? // 加載遠程的圖片 release 卸載資源

? ? ? ? cc.loader.load("http://127.0.0.1:6080/splash.png", function(err, ret) {

? ? ? ? ? ? if(err) {

? ? ? ? ? ? ? ? console.log(err);

? ? ? ? ? ? ? ? return;

? ? ? ? ? ? }

? ? ? ? ? ? // ret is cc.Texture2D這樣對象

? ? ? ? ? ? this.sprite.spriteFrame.setTexture(ret);

? ? ? ? ? ? this.sprite.node.setContentSize(ret.getContentSize()); // 使用這個圖片的大小

? ? ? ? ? ? // end?

? ? ? ? }.bind(this));

? ? ? ? // end

? ? ? ? // 從服務(wù)器加載mp3來進行播放, type,就是url不帶類型,我們就使用url + type的模式來制定類型

? ? ? ? cc.loader.load({url: "http://127.0.0.1:6080/bg.mp3", type: "mp3"}, function(err, ret) {

? ? ? ? ? ? if (err) {

? ? ? ? ? ? ? ? console.log(err);

? ? ? ? ? ? ? ? return;

? ? ? ? ? ? }

? ? ? ? ? ? this.audio.clip = ret;

? ? ? ? ? ? this.audio.play();

? ? ? ? }.bind(this));

? ? ? ? // end?

? ? ? ? // 從服務(wù)器加載json文件

? ? ? ? cc.loader.load({url: "http://127.0.0.1:6080/project.json", type: "json"}, function(err, ret) {

? ? ? ? ? ? if (err) {

? ? ? ? ? ? ? ? console.log(err);

? ? ? ? ? ? ? ? return;

? ? ? ? ? ? }


? ? ? ? ? ? console.log(ret);

? ? ? ? }.bind(this));

? ? ? ? // end?

? ? ? ? //?

? ? ? ? cc.loader.load({url: "http://127.0.0.1:6080/test.mydata", type: "mydata"}, function(err, ret) {

? ? ? ? ? ? if (err) {

? ? ? ? ? ? ? ? console.log(err);

? ? ? ? ? ? ? ? return;

? ? ? ? ? ? }


? ? ? ? ? ? console.log(ret);

? ? ? ? }.bind(this));

? ? ? ? // end?

? ? },

? ? local_load: function() {

? ? ? ? // 本地加載聲音

? ? ? ? // 注意:這里不需要后綴名, assets/resources/這個也不需要 ?releaseRes卸載資源

? ? ? ? cc.loader.loadRes("bg", function(err, ret){

? ? ? ? ? ? if(err) {

? ? ? ? ? ? ? ? console.log(err);

? ? ? ? ? ? ? ? return;

? ? ? ? ? ? }

? ? ? ? ? ? console.log(ret); // audio clip

? ? ? ? ? ? this.audio.clip = ret;

? ? ? ? ? ? this.audio.play();

? ? ? ? }.bind(this));

? ? ? ? // end?

? ? ? ? // 本地加載圖片

? ? ? ? cc.loader.loadRes("img/disk", cc.SpriteFrame, function(err, ret) {

? ? ? ? ? ? if (err) {

? ? ? ? ? ? ? ? console.log(err);

? ? ? ? ? ? ? ? return;

? ? ? ? ? ? }

? ? ? ? ? ? // spriteFrame對象

? ? ? ? ? ? // 例如我們的游戲,在進入下一個場景之前,有一個資源加載場景,那么到了新的場景后,我們就能夠找到這個加載好的資源

? ? ? ? ? ? // cc.loader.getRes();

? ? ? ? ? ? this.sprite.spriteFrame = cc.loader.getRes("img/disk", cc.SpriteFrame);

? ? ? ? ? ? // end?

? ? ? ? }.bind(this));

? ? ? ? // end?

? ? },

? ? // called every frame, uncomment this function to activate update callback

? ? // update: function (dt) {

? ? // },

});

我也創(chuàng)建了個cocos creator的學(xué)習(xí)交流群歡迎大家一起來學(xué)習(xí)點擊鏈接加入群聊【cocos/unity交流群】

最后編輯于
?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請聯(lián)系作者
【社區(qū)內(nèi)容提示】社區(qū)部分內(nèi)容疑似由AI輔助生成,瀏覽時請結(jié)合常識與多方信息審慎甄別。
平臺聲明:文章內(nèi)容(如有圖片或視頻亦包括在內(nèi))由作者上傳并發(fā)布,文章內(nèi)容僅代表作者本人觀點,簡書系信息發(fā)布平臺,僅提供信息存儲服務(wù)。

相關(guān)閱讀更多精彩內(nèi)容

友情鏈接更多精彩內(nèi)容