Cocos creator3.3版本 讀取二進制文件

 loadFile()
     {
         //31006war
        resources.load("datas/31006war",(err,content)=>{
        var xhr = new XMLHttpRequest();
          xhr.responseType = "arraybuffer"; 
          xhr.onreadystatechange = function () {
              if (xhr.readyState == 4) {
                  var status = xhr.status;
                  if (status >= 200 && status < 300) {
                    console.log(xhr.response);
                    if(xhr.response instanceof ArrayBuffer)
                    {
                      let  data = new Uint8Array(xhr.response);
                      let decodeInfo =  proto.LogWarDetailList.decode(data);
                      console.log(" LogWarDetailList.createTime===" +  decodeInfo.createTime + "id == " + decodeInfo.id );
                      let troopListLen = decodeInfo.enemyTroopList.length;
                      for(var i = 0; i < troopListLen; i ++)
                      {
                          var troop = decodeInfo.enemyTroopList[i];
                          console.log("troop.fightId=" + troop.fightId);
                          console.log("troop.img=" + troop.img);
                          console.log("troop.name=" + troop.name);
                      }
                    }
                  } else {
                    console.log(status);
                  }
              }
          };
          xhr.open("GET", content.nativeUrl, true);
          // xhr.setRequestHeader("Content-Type", "application/json");
          xhr.send(null);
        });
      
     }

搞了好久,不明白為啥加載要嵌套兩層,反正是目的實現(xiàn)了!

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

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

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