public void TestDeletingObsoleteCaches() { // Clear out obsolete caches before testing it with a new asset. ImageCache.DeleteObsoleteCaches(); // Create a temporary image. string imageFile = "Assets/Editor/Tests/TestData/TiltBrushLogo.jpg"; var tempFileName = Path.GetTempFileName(); File.Copy(imageFile, tempFileName, true); // Create a cache from the temporary image. Texture2D texture = new Texture2D(2, 2); texture.LoadImage(File.ReadAllBytes(tempFileName)); ImageCache.SaveIconCache(texture, tempFileName, 1.0f); int cacheCountWithTempImage = Directory.GetDirectories(ImageCache.CacheBaseDirectory).Length; // Delete the temporary image and rely on DeleteObsoleteCache() do delete the cache. File.Delete(tempFileName); ImageCache.DeleteObsoleteCaches(); int cacheCountWithoutTempImage = Directory.GetDirectories(ImageCache.CacheBaseDirectory).Length; // Check counts. Assert.AreEqual(cacheCountWithTempImage - 1, cacheCountWithoutTempImage); }
public void TestCachingRoundtrip() { // Get the texture. string imageFile = "Assets/Editor/Tests/TestData/TiltBrushLogo.jpg"; Texture2D texture = AssetDatabase.LoadAssetAtPath <Texture2D>(imageFile); float aspectRatio = 0.75f; // Cache the texture. ImageCache.SaveIconCache(texture, imageFile, aspectRatio); try { // Retreive the cache. float reconstructedAspectRatio; Texture2D reconstructedTexture = ImageCache.LoadIconCache(imageFile, out reconstructedAspectRatio); Assert.NotNull(reconstructedTexture); // Compare the textures. CompareTextures(texture, reconstructedTexture); Assert.AreEqual(aspectRatio, reconstructedAspectRatio); } finally { // Clean up the test cache. string cacheDirectory = ImageCache.CacheDirectory(imageFile); Directory.Delete(cacheDirectory, true); } }
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); }
// 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); } }