From 1fde360b9f5799bf2e424f9eb19eb4149596f692 Mon Sep 17 00:00:00 2001 From: kaycodes13 Date: Thu, 1 Jan 2026 22:07:55 -0500 Subject: [PATCH 1/4] prevent imported textures from generating mipmaps --- UnityHelper/Util/SpriteUtil.cs | 6 ++++-- 1 file changed, 4 insertions(+), 2 deletions(-) diff --git a/UnityHelper/Util/SpriteUtil.cs b/UnityHelper/Util/SpriteUtil.cs index e9aed0d..7fad099 100644 --- a/UnityHelper/Util/SpriteUtil.cs +++ b/UnityHelper/Util/SpriteUtil.cs @@ -128,7 +128,9 @@ public static Texture2D LoadTextureFromFile(string fileName, bool makeUnreadable /// A object. public static Texture2D LoadTextureFromArray(byte[] buffer, bool makeUnreadable = false) { - Texture2D tex = new(2, 2); + // The texture format here doesn't matter, but this constructor can + // prevent textures from generating mipmaps. + Texture2D tex = new(2, 2, TextureFormat.RGBA32, mipChain: false); tex.LoadImage(buffer, makeUnreadable); return tex; } @@ -152,7 +154,7 @@ public static Texture2D GetReadableTexture(Texture2D tex) { var previous = RenderTexture.active; RenderTexture.active = temp; - var readable = new Texture2D(tex.width, tex.height); + var readable = new Texture2D(tex.width, tex.height, tex.format, mipChain: tex.mipmapCount > 1); readable.ReadPixels(new Rect(0, 0, temp.width, temp.height), 0, 0); readable.Apply(); From 44831f9366731547ffd27a40622b57261022bd5b Mon Sep 17 00:00:00 2001 From: kaycodes13 Date: Thu, 1 Jan 2026 22:14:55 -0500 Subject: [PATCH 2/4] add utilities for premultiplying texture/sprite alpha --- UnityHelper/Util/SpriteUtil.cs | 74 +++++++++++++++++++++++++--------- 1 file changed, 55 insertions(+), 19 deletions(-) diff --git a/UnityHelper/Util/SpriteUtil.cs b/UnityHelper/Util/SpriteUtil.cs index 7fad099..b7ab960 100644 --- a/UnityHelper/Util/SpriteUtil.cs +++ b/UnityHelper/Util/SpriteUtil.cs @@ -84,18 +84,32 @@ public static Sprite LoadSpriteFromArray(byte[] buffer, float pixelsPerUnit = 64 public static Sprite LoadSpriteFromArray(byte[] buffer, float pixelsPerUnit) => LoadSpriteFromArray(buffer, pixelsPerUnit, null); - #endregion - - #region Texture2D - - /// - /// Load an image from the assembly's embedded resources and return a texture. - /// - /// The assembly to load from. - /// The path to the image. - /// Whether or not to mark the texture as unreadable. - /// A object. - public static Texture2D LoadEmbeddedTexture(Assembly asm, string path, bool makeUnreadable = false) + /// + /// Converts the given 's alpha format to premultiplied. + /// + /// + /// If a sprite has a fuzzy white edge, or a white tint in areas where it's + /// partially transparent, running it through this function may fix the issue. + /// + /// The sprite to convert. + /// The same . + public static Sprite PremultiplyAlpha(this Sprite source) { + source.texture.PremultiplyAlpha(); + return source; + } + + #endregion + + #region Texture2D + + /// + /// Load an image from the assembly's embedded resources and return a texture. + /// + /// The assembly to load from. + /// The path to the image. + /// Whether or not to mark the texture as unreadable. + /// A object. + public static Texture2D LoadEmbeddedTexture(Assembly asm, string path, bool makeUnreadable = false) { byte[] buffer = GetEmbeddedImageData(asm, path); @@ -135,13 +149,35 @@ public static Texture2D LoadTextureFromArray(byte[] buffer, bool makeUnreadable return tex; } - /// - /// If the given texture is unreadable, returns a readable copy of it. - /// If the given texture is readable, returns the same texture object. - /// - /// The texture to make readable. - /// A readable object. - public static Texture2D GetReadableTexture(Texture2D tex) { + /// + /// Converts the given 's alpha format to premultiplied. + /// + /// + /// If a texture has a fuzzy white edge, or a white tint in areas where it's + /// partially transparent, running it through this function may fix the issue. + /// + /// The texture to convert. + /// The same . + public static Texture2D PremultiplyAlpha(this Texture2D source) { + Color32[] pixels = source.GetPixels32(); + + for (int i = 0; i < pixels.Length; i++) { + Color px = pixels[i]; + pixels[i] = new Color(px.r * px.a, px.g * px.a, px.b * px.a, px.a); + } + + source.SetPixels32(pixels); + source.Apply(); + return source; + } + + /// + /// If the given texture is unreadable, returns a readable copy of it. + /// If the given texture is readable, returns the same texture object. + /// + /// The texture to make readable. + /// A readable object. + public static Texture2D GetReadableTexture(Texture2D tex) { if (tex.isReadable) return tex; From c192fb0fe9a3540db3a78ced4ec6f153d0a2c0a7 Mon Sep 17 00:00:00 2001 From: kaycodes13 Date: Thu, 1 Jan 2026 22:46:56 -0500 Subject: [PATCH 3/4] fix code style, change a bunch of tabs to spaces --- UnityHelper/Util/SpriteUtil.cs | 104 +++++++++++++++++---------------- 1 file changed, 53 insertions(+), 51 deletions(-) diff --git a/UnityHelper/Util/SpriteUtil.cs b/UnityHelper/Util/SpriteUtil.cs index b7ab960..2a30235 100644 --- a/UnityHelper/Util/SpriteUtil.cs +++ b/UnityHelper/Util/SpriteUtil.cs @@ -84,32 +84,33 @@ public static Sprite LoadSpriteFromArray(byte[] buffer, float pixelsPerUnit = 64 public static Sprite LoadSpriteFromArray(byte[] buffer, float pixelsPerUnit) => LoadSpriteFromArray(buffer, pixelsPerUnit, null); - /// - /// Converts the given 's alpha format to premultiplied. - /// - /// - /// If a sprite has a fuzzy white edge, or a white tint in areas where it's - /// partially transparent, running it through this function may fix the issue. - /// - /// The sprite to convert. - /// The same . - public static Sprite PremultiplyAlpha(this Sprite source) { + /// + /// Converts the given 's alpha format to premultiplied. + /// + /// + /// If a sprite has a fuzzy white edge, or a white tint in areas where it's + /// partially transparent, running it through this function may fix the issue. + /// + /// The sprite to convert. + /// The same . + public static Sprite PremultiplyAlpha(this Sprite source) + { source.texture.PremultiplyAlpha(); - return source; - } + return source; + } - #endregion + #endregion - #region Texture2D + #region Texture2D - /// - /// Load an image from the assembly's embedded resources and return a texture. - /// - /// The assembly to load from. - /// The path to the image. - /// Whether or not to mark the texture as unreadable. - /// A object. - public static Texture2D LoadEmbeddedTexture(Assembly asm, string path, bool makeUnreadable = false) + /// + /// Load an image from the assembly's embedded resources and return a texture. + /// + /// The assembly to load from. + /// The path to the image. + /// Whether or not to mark the texture as unreadable. + /// A object. + public static Texture2D LoadEmbeddedTexture(Assembly asm, string path, bool makeUnreadable = false) { byte[] buffer = GetEmbeddedImageData(asm, path); @@ -149,35 +150,36 @@ public static Texture2D LoadTextureFromArray(byte[] buffer, bool makeUnreadable return tex; } - /// - /// Converts the given 's alpha format to premultiplied. - /// - /// - /// If a texture has a fuzzy white edge, or a white tint in areas where it's - /// partially transparent, running it through this function may fix the issue. - /// - /// The texture to convert. - /// The same . - public static Texture2D PremultiplyAlpha(this Texture2D source) { - Color32[] pixels = source.GetPixels32(); - - for (int i = 0; i < pixels.Length; i++) { - Color px = pixels[i]; - pixels[i] = new Color(px.r * px.a, px.g * px.a, px.b * px.a, px.a); - } - - source.SetPixels32(pixels); - source.Apply(); - return source; - } - - /// - /// If the given texture is unreadable, returns a readable copy of it. - /// If the given texture is readable, returns the same texture object. - /// - /// The texture to make readable. - /// A readable object. - public static Texture2D GetReadableTexture(Texture2D tex) { + /// + /// Converts the given 's alpha format to premultiplied. + /// + /// + /// If a texture has a fuzzy white edge, or a white tint in areas where it's + /// partially transparent, running it through this function may fix the issue. + /// + /// The texture to convert. + /// The same . + public static Texture2D PremultiplyAlpha(this Texture2D source) + { + Color32[] pixels = source.GetPixels32(); + + for (int i = 0; i < pixels.Length; i++) { + Color px = pixels[i]; + pixels[i] = new Color(px.r * px.a, px.g * px.a, px.b * px.a, px.a); + } + + source.SetPixels32(pixels); + source.Apply(); + return source; + } + + /// + /// If the given texture is unreadable, returns a readable copy of it. + /// If the given texture is readable, returns the same texture object. + /// + /// The texture to make readable. + /// A readable object. + public static Texture2D GetReadableTexture(Texture2D tex) { if (tex.isReadable) return tex; From ba0124c99e3c8f2026c5cd203552f60098682af6 Mon Sep 17 00:00:00 2001 From: kaycodes13 Date: Fri, 2 Jan 2026 01:05:12 -0500 Subject: [PATCH 4/4] add note about texture readability to premultiply functions --- UnityHelper/Util/SpriteUtil.cs | 6 ++++++ 1 file changed, 6 insertions(+) diff --git a/UnityHelper/Util/SpriteUtil.cs b/UnityHelper/Util/SpriteUtil.cs index 2a30235..c678f39 100644 --- a/UnityHelper/Util/SpriteUtil.cs +++ b/UnityHelper/Util/SpriteUtil.cs @@ -86,11 +86,15 @@ public static Sprite LoadSpriteFromArray(byte[] buffer, float pixelsPerUnit) /// /// Converts the given 's alpha format to premultiplied. + /// The sprite's underlying texture must be readable. /// /// /// If a sprite has a fuzzy white edge, or a white tint in areas where it's /// partially transparent, running it through this function may fix the issue. /// + /// + /// If the Sprite's underlying texture is not readable. + /// /// The sprite to convert. /// The same . public static Sprite PremultiplyAlpha(this Sprite source) @@ -152,11 +156,13 @@ public static Texture2D LoadTextureFromArray(byte[] buffer, bool makeUnreadable /// /// Converts the given 's alpha format to premultiplied. + /// The texture must be readable. /// /// /// If a texture has a fuzzy white edge, or a white tint in areas where it's /// partially transparent, running it through this function may fix the issue. /// + /// If the texture is not readable. /// The texture to convert. /// The same . public static Texture2D PremultiplyAlpha(this Texture2D source)