IEnumerator DownloadImage(Image_info info) { string filePath = getImgUrl(info); string url = idToUrl(info.key) + "@" + getSizeString(info.state); bool isOk = false; int times = 0; while (!isOk) { WWW www = new WWW(url); yield return(www); if (www.error == null) { info.tex.LoadImage(www.bytes); info.isError = "OK"; info.site = "网络"; info.fun(info.tex); listRemoveImg(info); isOk = true; try { File.WriteAllBytes(filePath, info.tex.EncodeToJPG()); } catch (System.Exception e) { string s = e.ToString(); } } else { Debug.LogError("ImageManager2 网络请求图片出错:" + url + www.error.ToString()); info.isError = "Error"; if (times == 0) { listRemoveImg(info); } times++; if (times >= 10) { isOk = true; info.fun(null); } } if (!isOk) { yield return(new WaitForSeconds(3f)); } } }
IEnumerator LoadLocalImage(Image_info info) { string filePath = getWWWImgUrl(info); WWW www = new WWW(filePath); yield return(www); if (www.error == null) { info.tex.LoadImage(www.bytes); listRemoveImg(info); info.isError = "OK"; info.site = "本地"; info.fun(info.tex); } else { //TODO 加载网络数据 StartCoroutine(DownloadImage(info)); } }
private void loadLocalImage(Image_info info) { string filePath = Application.persistentDataPath + "/Resources/" + info.key; if (System.IO.File.Exists(filePath)) { try { Texture2D test = (Texture2D)Resources.Load(info.key, typeof(Texture2D)); info.tex = test; info.fun(info.tex); } catch (System.Exception e) { Debug.Log("loadLocalImage 读取本地文件出错:" + e.ToString()); } } else { StartCoroutine(DownloadImage(info)); } }