douhushen3241 2018-03-18 01:58
浏览 43
已采纳

bytes下载在加载时不会更新

In my script im expecting the text bytesDownloadedText to be updated with how many bytes have been downloaded so far but it only runs once and stays on 0. How do I fix this

private IEnumerator DownloadFile(){
        WWW w = new WWW (PATH_TO_DOWNLOAD);

        bytesDownloadedText.text = w.bytesDownloaded.ToString();

        yield return w;

        if (w.error != null) {
            Debug.LogError ("Error: " + w.error);
        } else {
            scriptText = w.text;
            filesDownloaded = true;
            Debug.Log (scriptText);
        }
    }

Edit: New code + Additional debugging information

private IEnumerator DownloadFile(){
        WWW w = new WWW (PATH_TO_DOWNLOAD);

        while(!w.isDone){
            bytesDownloadedText.text = w.bytesDownloaded.ToString ();
            Debug.Log ("Bytes Downloaded: " + w.bytesDownloaded);

            yield return null;
        }

        Debug.Log ("Exiting while loop");

        if (w.error != null) {
            Debug.LogError ("Error: " + w.error);
        } else {
            //bytesDownloadedText.text = w.bytesDownloaded.ToString ();
            scriptText = w.text;
            filesDownloaded = true;
            Debug.Log (scriptText);
        }
    }
  1. Filesize: 337 bytes
  2. Download Path: https://deathcrow.altervista.org/websharp/files.php

3.Code where DownloadFile is called

private void Start(){
        StartCoroutine (DownloadFile ());
    }
  1. The while loop is exiting
  2. w.text returns the test script from the server(which is code)

using UnityEngine;

public class TestScript: MonoBehaviour{
    public void Update(){
        if (Input.GetMouseButtonDown (0)) {
            RaycastHit hit;
            if (Physics.Raycast (Camera.main.ScreenPointToRay(Input.mousePosition),out hit)){
                if (hit.transform.tag == "Destroy") {
                    Destroy (hit.transform.root.gameObject);
                }
            }
        }
    }
}

Edit: Online code

<?
    $scriptText = "";

    $file_handle = fopen("TestScript.cs","r");
    while(!feof($file_handle)){
        $line = fgets($file_handle);
        $scriptText = $scriptText . $line;
    }
    fclose($file_handle);

    $size = strlen($scriptText);

    header("Content-length: ".$size);

    echo $scriptText;
?>
</div>
  • 写回答

1条回答 默认 最新

  • drzbc6003 2018-03-18 02:31
    关注

    Do not yield WWW if you want to use the bytesDownloaded property as that will pause your code until WWW returns which makes it impossible to read how much data has been downloaded.

    You have to put WWW.bytesDownloaded in a loop then use WWW.isDone to detect when WWW is done and then exit the loop. Inside that loop, you can use WWW.bytesDownloaded to display the downloaded data. Finally, you must wait for a frame after each loop so that other scripts can execute or Unity will freeze until the download is done.

    This is what that code should look like:

    private IEnumerator DownloadFile()
    {
        WWW w = new WWW(PATH_TO_DOWNLOAD);
    
        while (!w.isDone)
        {
            yield return null;
            bytesDownloadedText.text = w.bytesDownloaded.ToString();
            Debug.Log("Bytes Downloaded: " + w.bytesDownloaded);
        }
    
        if (w.error != null)
        {
            Debug.LogError("Error: " + w.error);
        }
        else
        {
            scriptText = w.text;
            filesDownloaded = true;
            Debug.Log(scriptText);
        }
    }
    

    Note: There are some instances where the bytesDownloaded property returns 0. This has nothing to do with Unity. This happens mostly when your server is not sending the Content-Length header.

    Example of sending the Content-Length header from the server(php):

    <?php
      //String to send
      $data = "Test message to send";
      //Get size
      $size= strlen($data);
      //Set Content-length header
      header("Content-length: ".$size);
      //Finally, you can send the data
      echo $data;
    ?>
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 基于卷积神经网络的声纹识别
  • ¥15 Python中的request,如何使用ssr节点,通过代理requests网页。本人在泰国,需要用大陆ip才能玩网页游戏,合法合规。
  • ¥100 为什么这个恒流源电路不能恒流?
  • ¥15 有偿求跨组件数据流路径图
  • ¥15 写一个方法checkPerson,入参实体类Person,出参布尔值
  • ¥15 我想咨询一下路面纹理三维点云数据处理的一些问题,上传的坐标文件里是怎么对无序点进行编号的,以及xy坐标在处理的时候是进行整体模型分片处理的吗
  • ¥15 CSAPPattacklab
  • ¥15 一直显示正在等待HID—ISP
  • ¥15 Python turtle 画图
  • ¥15 stm32开发clion时遇到的编译问题