ISTISTEST Mon - Sat 09:00-20:00 +91-884-9999-647 Mon - Sat 22:30-09:30 +91-884-9999-647
mj@inventive9.com
Expertise
IT Services
Mission
Create Innovative Products
Trusted By
Global Clients
Get A Quote

Download file using WebClient Unity

The main objective of this post is to help you download files from server so that you can update assets and add new ones to your game without having it updated.

1. Create script.

Create a script by right clicking in Project Window.

2. Open and edit the script.

3. Add System.Net.

using System.Net;

4. Create DownloadZipFile Method.

  • 4.1 Create WebClient object
  • 4.2 Add delegates
  • Now method will look like..

public void DownloadZipFile (string zipURL, string filePath, string fileName)
{
WebClient client = new WebClient();

client.DownloadProgressChanged += Client_DownloadProgressChanged;
client.DownloadFileCompleted += SuccessResponse;

client.DownloadFileAsync(new System.Uri(zipURL), filePath + fileName);
}

5. Create assigned local methods

void Client_DownloadProgressChanged (object sender, DownloadProgressChangedEventArgs e)
{
if (e.ProgressPercentage >= 100)
{
// Download Complete
Debug.Log("Download Complete");
}
else
{
// Download In Progress
Debug.Log("Download Progress = " + e.ProgressPercentage + "%");
// You can use this method to show download progress
}
}

void SuccessResponse (object sender, System.ComponentModel.AsyncCompletedEventArgs e)
{
if (e.Error == null)
{
Debug.Log("End of Download");
}
else
Debug.Log("SuccessResponse ---- Error --- \n " + e.Error);
}

6. Call DownloadZipFile method.

You need to pass your zip file url, file path and file name where you want to save this.

7. And done you will get your file downloaded.

I hope you will find this blogpost very useful while downloading additional data to your game with zipfile.

Let me know in the comment section below if you have any questions regarding this post or any other topic of Unity, I will try to reply ASAP.

Leave a Reply