Search Unity

  1. Improved Prefab workflow (includes Nested Prefabs!), 2D isometric Tilemap and more! Get the 2018.3 Beta now.
    Dismiss Notice
  2. The Unity Pro & Visual Studio Professional Bundle gives you the tools you need to develop faster & collaborate more efficiently. Learn more.
    Dismiss Notice
  3. Improve your Unity skills with a certified instructor in a private, interactive classroom. Watch the overview now.
    Dismiss Notice
  4. Want to see the most recent patch releases? Take a peek at the patch release page.
    Dismiss Notice

progress bar for downloading a file webclient .downloadfile

Discussion in 'Scripting' started by ftwbondy, Feb 24, 2017.

  1. ftwbondy

    ftwbondy

    Joined:
    Feb 24, 2017
    Posts:
    4
    so i have a piece of my script that is set to download a file from a url and then save it to the asset folder and it works great but i want to show the progress of its download as a variable so that i can then display the progress so that you have an idea on how its going, i can not find a way to get this to work/

    this is my working code

    Code (CSharp):
    1.     private static void download()
    2.     {
    3.         WebClient client = new WebClient();
    4.         client.DownloadFile("http://download.net/1.jpg",Application.dataPath + "/Download/image.jpg");
    5.     }
    an help would be greatly appreciated.
     
  2. andymads

    andymads

    Joined:
    Jun 16, 2011
    Posts:
    1,434
    I've never used WebClient but if you use WWW then you can poll the progress (0..1) and use this value for a progress bar.
     
  3. ftwbondy

    ftwbondy

    Joined:
    Feb 24, 2017
    Posts:
    4
    hmmmm interesting could you show me an example how this could be done with WWW with a progress bar?
     
  4. andymads

    andymads

    Joined:
    Jun 16, 2011
    Posts:
    1,434
    Have you used WWW before? Do you know what a Coroutine is?
     
  5. ftwbondy

    ftwbondy

    Joined:
    Feb 24, 2017
    Posts:
    4
    yes i have used it before but i am still new to c# scripting with unity, i do have a www above this that is checking a txt file on a web server for a version checking.
     
  6. andymads

    andymads

    Joined:
    Jun 16, 2011
    Posts:
    1,434
    Code (CSharp):
    1. var www = new WWW(url);
    2. while(!www.isDone)
    3. {
    4.     // todo: query www.progress which will be 0..1 and use the value
    5.     yield return null;
    6. }
     
  7. essbe

    essbe

    Joined:
    May 11, 2017
    Posts:
    1
    @andymads except this doesn't work. Even if you run it in a Coroutine it locks the main thread and you can't access the progress property.
     
  8. andymads

    andymads

    Joined:
    Jun 16, 2011
    Posts:
    1,434
    It works fine in a coroutine.
     
  9. ftwbondy

    ftwbondy

    Joined:
    Feb 24, 2017
    Posts:
    4
    I ended up using another way using WWW and IEnumerator here is my working code also for the web location of the files you need to have the setup.exe and a txt file with the version all located in the same folder.


    Code (CSharp):
    1. using UnityEngine;
    2. using System.Collections;
    3. using UnityEngine.UI;
    4. using System.Net;
    5. using System.IO;
    6.  
    7. public class VCheck : MonoBehaviour {
    8.  
    9.     public int m_GameV;
    10.     public Text m_downloadProgress;
    11.     public int m_CheckV;
    12.     private string m_ServerV;
    13.     private bool m_downloadFinished = false;
    14.  
    15.     // Use this for initialization
    16.     void Start ()
    17.     {
    18.         //runs check V on game start
    19.         StartCoroutine(CheckV());
    20.         m_downloadProgress.enabled = false;
    21.     }
    22.  
    23.     // Update is called once per frame
    24.     void Update ()
    25.     {
    26.         //starts setup.exe when download is finished
    27.         if (m_downloadFinished == true)
    28.         {
    29.             //start update
    30.             System.Diagnostics.Process.Start(System.Environment.ExpandEnvironmentVariables("%USERPROFILE%") + "/setup.exe");
    31.             m_downloadFinished = false;
    32.  
    33.             Application.Quit();
    34.  
    35.             // quit in editor only used in unity editor
    36.             #if UNITY_EDITOR
    37.             UnityEditor.EditorApplication.isPlaying = false;
    38.             #endif
    39.         }
    40.     }
    41.     //start download update file
    42.     private IEnumerator urlDownload ()
    43.     {
    44.         //creats connection to server
    45.         WWW downloadFile = new WWW("http://DownLoadLocationOfSeup.exe.com");
    46.  
    47.         //shows download progress
    48.         while (!downloadFile.isDone)
    49.         {
    50.             m_downloadProgress.enabled = true;
    51.             m_downloadProgress.text = (string.Format("New Version Available Downloading {0:P1}", downloadFile.progress));
    52.             yield return null;
    53.             if (downloadFile.isDone)
    54.             {
    55.                 m_downloadFinished = true;
    56.                 m_downloadProgress.text = ("New Version Available Downloading 100 %");
    57.             }
    58.         }
    59.         //save location
    60.         string savePath = System.Environment.ExpandEnvironmentVariables("%USERPROFILE%") + "/setup.exe";
    61.         //write file to local disk
    62.         byte[] bytes = downloadFile.bytes;
    63.         File.WriteAllBytes(savePath, bytes);
    64.     }
    65.  
    66.     //check V with server
    67.     private IEnumerator CheckV()
    68.     {
    69.         WWW hs_get = new WWW("http://VersionCheck.txt");
    70.         yield return hs_get;
    71.         if (hs_get.error != null)
    72.         {
    73.             print("no internet connection" + hs_get.error);
    74.             m_CheckV = m_GameV;
    75.         }
    76.         else
    77.         {
    78.             //if game V is less then CHeck download new v
    79.             m_ServerV = hs_get.text;
    80.             m_CheckV = int.Parse(m_ServerV);
    81.             if (m_CheckV > m_GameV)
    82.             {
    83.                 StartCoroutine(urlDownload());
    84.             }
    85.         }
    86.  
    87.     }
    88. }
    89.  
     
  10. igor_rst

    igor_rst

    Joined:
    Apr 20, 2017
    Posts:
    12
    Code (CSharp):
    1.  
    2.                 WebClient www = new WebClient ();
    3.                 Uri uri = new Uri (you_url);
    4.                 www.Encoding = Encoding.UTF8;
    5.                 www.DownloadProgressChanged += new DownloadProgressChangedEventHandler ((sender, data) => {
    6.                     Debug.Log("here you can check progress");
    7.                 });
    8.                 www.DownloadDataCompleted += new DownloadDataCompletedEventHandler ((sender, data) => {
    9.                     Debug.Log("here you can check complete");
    10.                     www.Dispose ();
    11.                 });
    12.                 www.DownloadDataAsync (uri);
    13.             }
     
  11. GuardFromUA

    GuardFromUA

    Joined:
    Jan 11, 2017
    Posts:
    8
    @igor_rst How you are doing it in other thread in Unity?
     
  12. atulyextel26

    atulyextel26

    Joined:
    Jun 14, 2018
    Posts:
    2
    simple bro use client.DownloadFileAsycn function