2017-08-31 4 views
3

ペーストビン(生)からテキストをtexfileにダウンロードします。 私はIEnumeratorを作成しましたが、何とか空のテキストファイルを作成するだけです。c#webClient.DownloadFileはダウンロードされません。空のテキストファイルが作成されます。

public IEnumerator DownloadTextFile() 
{ 
    WebClient version = new WebClient(); 
    yield return version; 
    version.DownloadFileAsync(new Uri(myLink), "version.txt"); 
} 

public IEnumerator DownloadTextFile() 
{ 
    WebClient version = new WebClient(); 
    yield return version; 
    version.DownloadFile(myLink , "version.txt"); 
} 

ありがとうございます。

答えて

4

WebクライアントがそのようUnity3Dの中から使用するように設計されていない、yield return version;は、ダウンロードするファイルを待ちません。

あなたができることは、WWWクラスを使用して、そのようにダウンロードを実行することです。 WWWはUnityの一部であり、Unityが動作する仕方で動作するように設計されています。

public IEnumerator DownloadTextFile() 
{ 
    WWW version = new WWW(myLink); 
    yield return version; 
    File.WriteAllBytes("version.txt", version.bytes); 

    //Or if you just wanted the text in your game instead of a file 
    someTextObject.text = version.text; 
} 

StartCoroutine(DownloadTextFile())

+0

このすばらしい非常に有用な回答をありがとう。それはとてもうまくいった。 – Chloe

+0

ダウンロードの進行状況を確認する方法はありますか? – Chloe

+0

はい、WWWのドキュメントを見ると、['progress'](https://docs.unity3d.com/ScriptReference/WWW-progress.html)フィールドが表示されます。進歩がどれだけ遠いかを見るためにそれを読んでください。 –

1

WebClientをusingステートメントの中に入れて、ダウンロード完了を確実に処理できるようにしてください。非同期バージョンの場合は、ビジー状態での保釈を行わないようにする必要があります。

 //Method 1 
     using (var version = new WebClient()) 
      version.DownloadFile("https://pastebin.com/raw/c1GrSCKR", @"c:\temp\version.txt"); 

     // Method 2 (better if you are working within Task based async system) 
     //using (var version = new WebClient()) 
     // await version.DownloadFileTaskAsync("https://pastebin.com/raw/c1GrSCKR", @"c:\temp\version.txt"); 

     // Method 3 - you can't dispose till is completed/you can also register to get notified when download is done. 
     using (var version = new WebClient()) 
     { 
      //version.DownloadFileCompleted += (object sender, AsyncCompletedEventArgs e) => 
      //{ 

      //}; 
      version.DownloadFileAsync(new Uri("https://pastebin.com/raw/c1GrSCKR"), @"c:\temp\version.txt"); 

      while (version.IsBusy) { } 
     } 
+1

これはUnity3dなので、「while(version.IsBusy){}」は大きなノーノであり、ゲームをロックします。あなたは 'while(version.IsBusy){yield returns 0; } '代わりに、1つのフレームを待ち、ビジーであれば再びチェックします。 –

+0

Scottさんに電話してください。私はUnity部分を完全に無視します。あなたの投稿は上にスポットです。 –

3

を呼び出すことによってDownloadTextFileを起動してくださいスコット・チェンバレンのsolution適切あるとWWW APIは、バックグラウンドでのスレッド問題を処理するのではユニティでこれを行うには方法をお勧めします。コルーチンを使用してダウンロードしてから、スコットのようなFile.WriteAllXXXのいずれかの関数を手動で保存するだけです。

質問は具体的にはWebClientについて質問しており、大きなデータのようにWebClientを使用している場合がありますので、この回答を追加しています。

問題はあなたがWebClientを生み出していることです。あなたはあなたのようにコルーチンにWebClientを与える必要はありません。別のスレッドで呼び出されるDownloadFileCompletedイベントに登録するだけです。そのDownloadFileCompletedコールバック関数でユニティ機能を使用するためには、ここで

は完全な例( UnityThreadが必要とされている)..です thisポストから UnityThreadスクリプトを使用して UnityThread.executeInUpdate機能の助けを借りてユニティ機能を実行する必要があります。

public Text text; 
int fileID = 0; 

void Awake() 
{ 
    UnityThread.initUnityThread(); 
} 

void Start() 
{ 
    string url = "http://www.sample-videos.com/text/Sample-text-file-10kb.txt"; 
    string savePath = Path.Combine(Application.dataPath, "file.txt"); 

    downloadFile(url, savePath); 
} 

void downloadFile(string fileUrl, string savePath) 
{ 
    WebClient webClient = new WebClient(); 
    webClient.DownloadFileCompleted += new AsyncCompletedEventHandler(DoSomethingOnFinish); 
    webClient.QueryString.Add("fileName", fileID.ToString()); 
    Uri uri = new Uri(fileUrl); 
    webClient.DownloadFileAsync(uri, savePath); 
    fileID++; 
} 

//THIS FUNCTION IS CALLED IN ANOTHER THREAD BY WebClient when download is complete 
void DoSomethingOnFinish(object sender, AsyncCompletedEventArgs e) 
{ 
    string myFileNameID = ((System.Net.WebClient)(sender)).QueryString["fileName"]; 
    Debug.Log("Done downloading file: " + myFileNameID); 

    //Ssafety use Unity's API in another Thread with the help of UnityThread 
    UnityThread.executeInUpdate(() => 
    { 
     text.text = "Done downloading file: " + myFileNameID; 
    }); 
} 
関連する問題