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 树莓派与pix飞控通信
  • ¥15 自动转发微信群信息到另外一个微信群
  • ¥15 outlook无法配置成功
  • ¥30 这是哪个作者做的宝宝起名网站
  • ¥60 版本过低apk如何修改可以兼容新的安卓系统
  • ¥25 由IPR导致的DRIVER_POWER_STATE_FAILURE蓝屏
  • ¥50 有数据,怎么建立模型求影响全要素生产率的因素
  • ¥50 有数据,怎么用matlab求全要素生产率
  • ¥15 TI的insta-spin例程
  • ¥15 完成下列问题完成下列问题