// Helper for GetImageFullsize IEnumerable LoadImage(string path, Texture2D dest, bool runForeground = false) { // Temporarily increase the reference count during loading to prevent texture destruction if // ReturnImageFullSize is called during load. m_FullSizeReferences++; var reader = new ThreadedImageReader(path, -1, App.PlatformConfig.ReferenceImagesMaxDimension); while (!reader.Finished) { if (!runForeground) { yield return(null); } } RawImage result = null; try { result = reader.Result; if (result != null && dest != null) { int resizeLimit = App.PlatformConfig.ReferenceImagesResizeDimension; if (result.ColorWidth > resizeLimit || result.ColorHeight > resizeLimit) { // Resize the image to the resize limit before saving it to the dest texture. var tempTexture = new Texture2D( result.ColorWidth, result.ColorHeight, TextureFormat.RGBA32, true); tempTexture.SetPixels32(result.ColorData); tempTexture.Apply(); DownsizeTexture(tempTexture, ref dest, resizeLimit); Object.Destroy(tempTexture); } else { // Save the the image to the dest texture. dest.Resize(result.ColorWidth, result.ColorHeight, TextureFormat.RGBA32, true); dest.SetPixels32(result.ColorData); dest.Apply(); } // Cache the texture. ImageCache.SaveImageCache(dest, path); } } catch (FutureFailed e) { ImageLoadError imageLoad = e.InnerException as ImageLoadError; if (imageLoad != null) { ControllerConsoleScript.m_Instance.AddNewLine(imageLoad.Message, true); } } finally { // Reduce the reference count again. This ensures the image gets properly released if // ReturnImageFullSize is called before loading finished. ReleaseImageFullsize(); } }
// Like RequestLoadCoroutine, but allowed to use main thread CPU time IEnumerator <Timeslice> RequestLoadCoroutineMainThread() { // On main thread! Can decode images using WWW class. This is about 10x faster using (WWW loader = new WWW(PathToWwwUrl(m_Path))) { while (!loader.isDone) { yield return(null); } if (string.IsNullOrEmpty(loader.error)) { // Passing in a texture with mipmapCount > 1 is how you ask for mips // from WWW.LoadImageIntoTexture Texture2D inTex = new Texture2D(2, 2, TextureFormat.RGBA32, true); loader.LoadImageIntoTexture(inTex); DownsizeTexture(inTex, ref m_Icon, ReferenceImageCatalog.MAX_ICON_TEX_DIMENSION); m_Icon.wrapMode = TextureWrapMode.Clamp; m_ImageAspect = (float)inTex.width / inTex.height; ImageCache.SaveIconCache(m_Icon, FilePath, m_ImageAspect); yield return(null); // Create the full size image cache as well. int resizeLimit = App.PlatformConfig.ReferenceImagesResizeDimension; if (inTex.width > resizeLimit || inTex.height > resizeLimit) { Texture2D resizedTex = new Texture2D(2, 2, TextureFormat.RGBA32, true); DownsizeTexture(inTex, ref resizedTex, resizeLimit); ImageCache.SaveImageCache(resizedTex, m_Path); Object.Destroy(resizedTex); } else { ImageCache.SaveImageCache(inTex, m_Path); } Object.Destroy(inTex); m_State = ImageState.Ready; yield break; } } // OK, take the slower path instead. foreach (var ret in RequestLoadCoroutine()) { yield return(ret); } }