IEnumerator Downloading() { while (pendingList.Count > 0) { WebItemRequest request = pendingList[0]; pendingList.RemoveAt(0); WebItem item; if (cache.TryGetValue(request.url, out item) && item.Item != null) { item.AddRef(); if (request.onComplete != null) { request.onComplete(true, item); } item.Release(); } else { while (downloadingList.Count >= 4) { yield return(null); } StartCoroutine(WorkerCoroutine(request)); } yield return(null); } }
public void RequestWebAsset(string url, int version, System.Type type, System.Action <bool, WebItem> onfinish = null, System.Action <float> onprogress = null) { for (int i = 0; i < pendingList.Count; i++) { if (pendingList[i].url.Equals(url) && pendingList[i].version == version) { pendingList[i].onComplete += onfinish; pendingList[i].onProgress += onprogress; return; } } for (int i = 0; i < downloadingList.Count; i++) { if (downloadingList[i].url.Equals(url) && downloadingList[i].version == version) { downloadingList[i].onComplete += onfinish; downloadingList[i].onProgress += onprogress; return; } } WebItemRequest request = new WebItemRequest(url, version, type, onfinish, onprogress); bool needrestart = pendingList.Count == 0; pendingList.Add(request); if (needrestart) { StartDownload(); } #if UNITY_EDITOR // for (int i = 0; i < pendingList.Count; i++) { // Debug.Log(pendingList[i].url); // } #endif }
public void RequestWebAsset(string url, int version, System.Type type, System.Action<bool, WebItem> onfinish = null, System.Action<float> onprogress = null) { for (int i = 0; i < pendingList.Count; i++) { if(pendingList[i].url.Equals(url) && pendingList[i].version == version) { pendingList[i].onComplete += onfinish; pendingList[i].onProgress += onprogress; return; } } for (int i = 0; i < downloadingList.Count; i++) { if(downloadingList[i].url.Equals(url)&& downloadingList[i].version == version) { downloadingList[i].onComplete += onfinish; downloadingList[i].onProgress += onprogress; return; } } WebItemRequest request = new WebItemRequest(url, version, type, onfinish, onprogress); bool needrestart = pendingList.Count == 0; pendingList.Add(request); if(needrestart) { StartDownload(); } #if UNITY_EDITOR // for (int i = 0; i < pendingList.Count; i++) { // Debug.Log(pendingList[i].url); // } #endif }
IEnumerator WorkerCoroutine(WebItemRequest request) { string filepath = GetCachePath(request.url, request.version); bool exists = System.IO.File.Exists(filepath); string wwwpath = request.url; if(exists) { System.Uri uri = new System.Uri(filepath); wwwpath = uri.AbsoluteUri; } using(WWW www = new WWW(System.Uri.EscapeUriString(wwwpath))) { downloadingList.Add(request); float timeout = Time.realtimeSinceStartup; while(!www.isDone) { if(request.onProgress != null) request.onProgress(www.progress); yield return null; if(Time.realtimeSinceStartup - timeout > 1f && www.progress == 0) { downloadingList.Remove(request); if(request.onComplete != null) request.onComplete(false, null); yield break; } } downloadingList.Remove(request); if(www.error != null) { Debug.LogWarning("cannot open url: " + wwwpath + " " + www.error); if(request.onComplete != null) request.onComplete(false, null); yield break; } WebItem item = new WebItem(); item.url = request.url; item.type = request.type; item.version = request.version; if(item.type == typeof(Texture)) { item.Item = www.textureNonReadable; } else if(item.type == typeof(AssetBundle)) { item.Item = www.assetBundle; } else if(item.type == typeof(AudioClip)) { item.Item = www.audioClip; } cache[item.url] = item; item.AddRef(); if(request.onComplete != null) request.onComplete(true, item); item.Release(); //check need write to disk if(!exists) { //clear old version data var parentDir = System.IO.Directory.GetParent(filepath); var files = parentDir.GetFiles(); foreach(var f in files) { f.Delete(); } //write new version data using (var fs = System.IO.File.OpenWrite(filepath)) { Debug.Log(filepath); fs.Write(www.bytes, 0, www.bytes.Length); fs.Flush(); fs.Close(); } } } }
IEnumerator WorkerCoroutine(WebItemRequest request) { string filepath = GetCachePath(request.url, request.version); bool exists = System.IO.File.Exists(filepath); string wwwpath = request.url; if (exists) { System.Uri uri = new System.Uri(filepath); wwwpath = uri.AbsoluteUri; } using (WWW www = new WWW(System.Uri.EscapeUriString(wwwpath))) { downloadingList.Add(request); float timeout = Time.realtimeSinceStartup; while (!www.isDone) { if (request.onProgress != null) { request.onProgress(www.progress); } yield return(null); if (Time.realtimeSinceStartup - timeout > 1f && www.progress == 0) { downloadingList.Remove(request); if (request.onComplete != null) { request.onComplete(false, null); } yield break; } } downloadingList.Remove(request); if (www.error != null) { Debug.LogWarning("cannot open url: " + wwwpath + " " + www.error); if (request.onComplete != null) { request.onComplete(false, null); } yield break; } WebItem item = new WebItem(); item.url = request.url; item.type = request.type; item.version = request.version; if (item.type == typeof(Texture)) { item.Item = www.textureNonReadable; } else if (item.type == typeof(AssetBundle)) { item.Item = www.assetBundle; } else if (item.type == typeof(AudioClip)) { item.Item = www.audioClip; } cache[item.url] = item; item.AddRef(); if (request.onComplete != null) { request.onComplete(true, item); } item.Release(); //check need write to disk if (!exists) { //clear old version data var parentDir = System.IO.Directory.GetParent(filepath); var files = parentDir.GetFiles(); foreach (var f in files) { f.Delete(); } //write new version data using (var fs = System.IO.File.OpenWrite(filepath)) { Debug.Log(filepath); fs.Write(www.bytes, 0, www.bytes.Length); fs.Flush(); fs.Close(); } } } }