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/
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.
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.
using UnityEngine;
using System.Collections;
using UnityEngine.UI;
using System.Net;
using System.IO;
public class VCheck : MonoBehaviour {
public int m_GameV;
public Text m_downloadProgress;
public int m_CheckV;
private string m_ServerV;
private bool m_downloadFinished = false;
// Use this for initialization
void Start ()
{
//runs check V on game start
StartCoroutine(CheckV());
m_downloadProgress.enabled = false;
}
// Update is called once per frame
void Update ()
{
//starts setup.exe when download is finished
if (m_downloadFinished == true)
{
//start update
System.Diagnostics.Process.Start(System.Environment.ExpandEnvironmentVariables("%USERPROFILE%") + "/setup.exe");
m_downloadFinished = false;
Application.Quit();
// quit in editor only used in unity editor
#if UNITY_EDITOR
UnityEditor.EditorApplication.isPlaying = false;
#endif
}
}
//start download update file
private IEnumerator urlDownload ()
{
//creats connection to server
WWW downloadFile = new WWW("http://DownLoadLocationOfSeup.exe.com");
//shows download progress
while (!downloadFile.isDone)
{
m_downloadProgress.enabled = true;
m_downloadProgress.text = (string.Format("New Version Available Downloading {0:P1}", downloadFile.progress));
yield return null;
if (downloadFile.isDone)
{
m_downloadFinished = true;
m_downloadProgress.text = ("New Version Available Downloading 100 %");
}
}
//save location
string savePath = System.Environment.ExpandEnvironmentVariables("%USERPROFILE%") + "/setup.exe";
//write file to local disk
byte[] bytes = downloadFile.bytes;
File.WriteAllBytes(savePath, bytes);
}
//check V with server
private IEnumerator CheckV()
{
WWW hs_get = new WWW("http://VersionCheck.txt");
yield return hs_get;
if (hs_get.error != null)
{
print("no internet connection" + hs_get.error);
m_CheckV = m_GameV;
}
else
{
//if game V is less then CHeck download new v
m_ServerV = hs_get.text;
m_CheckV = int.Parse(m_ServerV);
if (m_CheckV > m_GameV)
{
StartCoroutine(urlDownload());
}
}
}
}
WebClient www = new WebClient ();
Uri uri = new Uri (you_url);
www.Encoding = Encoding.UTF8;
www.DownloadProgressChanged += new DownloadProgressChangedEventHandler ((sender, data) => {
Debug.Log("here you can check progress");
});
www.DownloadDataCompleted += new DownloadDataCompletedEventHandler ((sender, data) => {
Debug.Log("here you can check complete");
www.Dispose ();
});
www.DownloadDataAsync (uri);
}