Clean up generated textures

This commit is contained in:
Nick 2022-05-01 12:54:52 -04:00
parent 58f12976ef
commit 931403566b

View File

@ -8,6 +8,44 @@ namespace NewHorizons.Utility
{ {
static class ImageUtilities static class ImageUtilities
{ {
private static Dictionary<string, Texture2D> _loadedTextures = new Dictionary<string, Texture2D>();
private static List<Texture2D> _generatedTextures = new List<Texture2D>();
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) public static Texture2D MakeOutline(Texture2D texture, Color color, int thickness)
{ {
var outline = new Texture2D(texture.width, texture.height, TextureFormat.ARGB32, false); var outline = new Texture2D(texture.width, texture.height, TextureFormat.ARGB32, false);
@ -32,6 +70,8 @@ namespace NewHorizons.Utility
outline.SetPixels(outlinePixels); outline.SetPixels(outlinePixels);
outline.Apply(); outline.Apply();
_generatedTextures.Add(outline);
return outline; return outline;
} }
@ -67,6 +107,9 @@ namespace NewHorizons.Utility
newImage.name = image.name + "Tinted"; newImage.name = image.name + "Tinted";
newImage.SetPixels(pixels); newImage.SetPixels(pixels);
newImage.Apply(); newImage.Apply();
_generatedTextures.Add(newImage);
return newImage; return newImage;
} }
@ -84,15 +127,10 @@ namespace NewHorizons.Utility
newImage.name = image.name + "LerpedGrayscale"; newImage.name = image.name + "LerpedGrayscale";
newImage.SetPixels(pixels); newImage.SetPixels(pixels);
newImage.Apply(); newImage.Apply();
return newImage;
}
public static Texture2D LoadImage(string filepath) _generatedTextures.Add(newImage);
{
Texture2D tex = new Texture2D(2, 2); return newImage;
tex.name = Path.GetFileNameWithoutExtension(filepath);
tex.LoadRawTextureData(File.ReadAllBytes(filepath));
return tex;
} }
public static Texture2D ClearTexture(int width, int height) public static Texture2D ClearTexture(int width, int height)
@ -107,6 +145,9 @@ namespace NewHorizons.Utility
} }
tex.SetPixels(fillPixels); tex.SetPixels(fillPixels);
tex.Apply(); tex.Apply();
_generatedTextures.Add(tex);
return tex; return tex;
} }
@ -124,39 +165,12 @@ namespace NewHorizons.Utility
} }
tex.SetPixels(fillPixels); tex.SetPixels(fillPixels);
tex.Apply(); tex.Apply();
_generatedTextures.Add(tex);
return tex; return tex;
} }
private static Dictionary<string, Texture2D> _loadedTextures = new Dictionary<string, Texture2D>();
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) public static Color GetAverageColor(Texture2D src)
{ {
var pixels = src.GetPixels32(); var pixels = src.GetPixels32();