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);
        });
      
     }

搞了好久,不明白为啥加载要嵌套两层,反正是目的实现了!

你可能感兴趣的:(Cocos creator3.3版本 读取二进制文件)