IEnumerable <Timeslice> RequestLoadCoroutine() { var reader = new ThreadedImageReader(m_Path, ReferenceImageCatalog.MAX_ICON_TEX_DIMENSION, App.PlatformConfig.ReferenceImagesMaxDimension); while (!reader.Finished) { yield return(null); } RawImage result = null; try { result = reader.Result; } catch (FutureFailed e) { ImageLoadError imageLoad = e.InnerException as ImageLoadError; if (imageLoad != null) { ControllerConsoleScript.m_Instance.AddNewLine(imageLoad.Message, true); } } if (result != null) { while (ReferenceImageCatalog.m_Instance.TexturesCreatedThisFrame >= ReferenceImageCatalog.TEXTURE_CREATIONS_PER_FRAME) { yield return(null); } if (m_Icon == null) { m_Icon = new Texture2D(result.ColorWidth, result.ColorHeight, TextureFormat.RGBA32, true); } else { m_Icon.Resize(result.ColorWidth, result.ColorHeight, TextureFormat.RGBA32, true); } m_ImageAspect = result.ColorAspect; m_Icon.wrapMode = TextureWrapMode.Clamp; m_Icon.SetPixels32(result.ColorData); m_Icon.Apply(); ReferenceImageCatalog.m_Instance.TexturesCreatedThisFrame++; reader = null; yield return(null); } else { // Problem reading the file? m_State = ImageState.Error; reader = null; yield break; } m_State = ImageState.Ready; ImageCache.SaveIconCache(m_Icon, FilePath, m_ImageAspect); }
// 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(); } }