diff --git a/NewHorizons/Utility/ImageUtilities.cs b/NewHorizons/Utility/ImageUtilities.cs index 6fc800d8..5edc882f 100644 --- a/NewHorizons/Utility/ImageUtilities.cs +++ b/NewHorizons/Utility/ImageUtilities.cs @@ -8,6 +8,44 @@ namespace NewHorizons.Utility { static class ImageUtilities { + private static Dictionary _loadedTextures = new Dictionary(); + private static List _generatedTextures = new List(); + + public static Texture2D GetTexture(IModBehaviour mod, string filename) + { + // Copied from OWML but without the print statement lol + var path = mod.ModHelper.Manifest.ModFolderPath + filename; + if (_loadedTextures.ContainsKey(path)) + { + Logger.Log($"Already loaded image at path: {path}"); + return _loadedTextures[path]; + } + Logger.Log($"Loading image at path: {path}"); + var data = File.ReadAllBytes(path); + var texture = new Texture2D(2, 2); + texture.name = Path.GetFileNameWithoutExtension(path); + texture.LoadImage(data); + _loadedTextures.Add(path, texture); + return texture; + } + + public static void ClearCache() + { + foreach (var texture in _loadedTextures.Values) + { + if (texture == null) continue; + UnityEngine.Object.Destroy(texture); + } + _loadedTextures.Clear(); + + foreach(var texture in _generatedTextures) + { + if (texture == null) continue; + UnityEngine.Object.Destroy(texture); + } + _generatedTextures.Clear(); + } + public static Texture2D MakeOutline(Texture2D texture, Color color, int thickness) { var outline = new Texture2D(texture.width, texture.height, TextureFormat.ARGB32, false); @@ -32,6 +70,8 @@ namespace NewHorizons.Utility outline.SetPixels(outlinePixels); outline.Apply(); + _generatedTextures.Add(outline); + return outline; } @@ -67,6 +107,9 @@ namespace NewHorizons.Utility newImage.name = image.name + "Tinted"; newImage.SetPixels(pixels); newImage.Apply(); + + _generatedTextures.Add(newImage); + return newImage; } @@ -84,15 +127,10 @@ namespace NewHorizons.Utility newImage.name = image.name + "LerpedGrayscale"; newImage.SetPixels(pixels); newImage.Apply(); - return newImage; - } - public static Texture2D LoadImage(string filepath) - { - Texture2D tex = new Texture2D(2, 2); - tex.name = Path.GetFileNameWithoutExtension(filepath); - tex.LoadRawTextureData(File.ReadAllBytes(filepath)); - return tex; + _generatedTextures.Add(newImage); + + return newImage; } public static Texture2D ClearTexture(int width, int height) @@ -107,6 +145,9 @@ namespace NewHorizons.Utility } tex.SetPixels(fillPixels); tex.Apply(); + + _generatedTextures.Add(tex); + return tex; } @@ -124,39 +165,12 @@ namespace NewHorizons.Utility } tex.SetPixels(fillPixels); tex.Apply(); + + _generatedTextures.Add(tex); + return tex; } - private static Dictionary _loadedTextures = new Dictionary(); - - public static Texture2D GetTexture(IModBehaviour mod, string filename) - { - // Copied from OWML but without the print statement lol - var path = mod.ModHelper.Manifest.ModFolderPath + filename; - if (_loadedTextures.ContainsKey(path)) - { - Logger.Log($"Already loaded image at path: {path}"); - return _loadedTextures[path]; - } - Logger.Log($"Loading image at path: {path}"); - var data = File.ReadAllBytes(path); - var texture = new Texture2D(2, 2); - texture.name = Path.GetFileNameWithoutExtension(path); - texture.LoadImage(data); - _loadedTextures.Add(path, texture); - return texture; - } - - public static void ClearCache() - { - foreach(var texture in _loadedTextures.Values) - { - if (texture == null) continue; - UnityEngine.Object.Destroy(texture); - } - _loadedTextures.Clear(); - } - public static Color GetAverageColor(Texture2D src) { var pixels = src.GetPixels32();