midworld / unity-googledrive

Google Drive for Unity3D
Apache License 2.0
121 stars 28 forks source link

Cannot receive progress value #34

Closed jcaron144 closed 6 years ago

jcaron144 commented 6 years ago

Hello, When downloading a large file it would be nice to see the progress on the download. I have tried logging the response.progress, but that always returns 0.

Shouldn't this Log statement return a progress float?:

public IEnumerator DownloadFile(string url)
{
    #region Check the access token is expired
    var check = CheckExpiration();
    while (check.MoveNext())
    yield return null;

    if (check.Current is Exception)
    {
        yield return check.Current;
        yield break;
    }
    #endregion

    var request = new UnityWebRequest(url);
    request.headers["Authorization"] = "Bearer " + AccessToken;

    var response = new UnityWebResponse(request);
    while (!response.isDone)
        {
               Debug.Log(response.progress);
               yield return null;
         }
    yield return new AsyncSuccess(response.bytes);
}

Oops, on further investigation, I added the progress value at a previous time in an attempt to populate it -- it does not exist in the current source code. Is there any way to easily get it?

midworld commented 6 years ago

Hi, You can get the progress of downloading with some further works. Downloading is running on another thread: https://github.com/midworld/unity-googledrive/blob/7be81c0ac570a7588d65ed8cf34ab858613b6c78/Assets/GoogleDrive/UnityWebResponse.cs#L65 If Google Drive gives 'Content-Length', you can get the progress easily. Just write current downloaded byte count and the content length to public properties of UnityWebResponse. You should consider thread-safety when update these value(probably use lock). If Google Drive doesn't give 'Content-Length', it might be a 'chunked' encoded stream. In this case you couldn't know the length of the file. So you should query stats of the file to get the whole file size first. Thanks.

jcaron144 commented 6 years ago

Great, I was able to create a progress event to listen to. Thanks again!