public bool GetTexture(string url, string savePath, RawImage ownerUI, Dictionary <RawImage, string> uiToUrlMap, bool resetUI, Action loadDoneAction) { if (m_TextureCache != null) { if (m_TextureCache.GetTexture(url, savePath, ownerUI, uiToUrlMap, resetUI, loadDoneAction)) { return(true); } } var loadTask = new TextureDownloadTask(); loadTask.downloadUrl = url; loadTask.ownerUI = ownerUI; loadTask.savePath = savePath; loadTask.uiToUrlMap = uiToUrlMap; loadTask.resetUI = resetUI; loadTask.loadDoneAction = loadDoneAction; m_WaitDownloadTasks.Enqueue(loadTask); if (m_CurrentIdleTask > 0) { LaunchNextLoadTask(); } return(true); }
private void LaunchNextLoadTask() { if (m_CurrentIdleTask > 0 && m_WaitDownloadTasks.Count > 0) { var tryTimes = m_WaitDownloadTasks.Count; TextureDownloadTask task = new TextureDownloadTask(); bool needLaunchTask = false; while (tryTimes > 0) { --tryTimes; task = m_WaitDownloadTasks.Dequeue(); if (m_DownloadingTask.Contains(task.downloadUrl)) { m_WaitDownloadTasks.Enqueue(task); } else { needLaunchTask = true; break; } } if (!needLaunchTask) { return; } if (m_TextureCache != null) { if (m_TextureCache.GetTexture(task.downloadUrl, task.savePath, task.ownerUI, task.uiToUrlMap, task.resetUI, task.loadDoneAction)) { LaunchNextLoadTask(); return; } } --m_CurrentIdleTask; m_DownloadingTask.Add(task.downloadUrl); ActionShcheduler.Instance.RunAsync(() => { var webClient = new WebClient(); byte[] data; try { data = webClient.DownloadData(task.downloadUrl); var contentType = webClient.ResponseHeaders["Content-Type"]; if (string.IsNullOrEmpty(contentType) || !contentType.StartsWith("image")) { data = null; } } catch (Exception e) { Debug.LogError("load image from htpp error:" + e); data = null; } ActionShcheduler.Instance.QueueOnMainThread(() => { try { if (data != null) { CacheTextureToDisk(task.downloadUrl, task.savePath, data); } if (data != null && task.ownerUI != null && task.uiToUrlMap.ContainsKey(task.ownerUI) && task.uiToUrlMap[task.ownerUI] == task.downloadUrl) { Texture2D texture = new Texture2D(2, 2, TextureFormat.RGB24, false); texture.LoadImage(data); texture.Apply(false); task.ownerUI.texture = texture; task.uiToUrlMap.Remove(task.ownerUI); CacheTexture(task.downloadUrl, texture); if (task.loadDoneAction != null) { task.loadDoneAction(); } } } finally { ++m_CurrentIdleTask; m_DownloadingTask.Remove(task.downloadUrl); LaunchNextLoadTask(); } }); }); } }