Skip to content

Instantly share code, notes, and snippets.

What would you like to do?
Pre-multiply alpha channel when importing Unity textures
using System;
using JetBrains.Annotations;
using UnityEditor;
using UnityEngine;
namespace UnityProject.Editor {
public sealed class TextureAlphaPremultiplier : AssetPostprocessor {
public void OnPreprocessTexture() {
if (ShouldPremultiplyAlpha())
((TextureImporter) assetImporter).alphaIsTransparency = false;
public void OnPostprocessTexture([NotNull] Texture2D texture) {
if (!ShouldPremultiplyAlpha())
int width = texture.width;
int height = texture.height;
for (int x = 0; x < width; ++x) {
for (int y = 0; y < height; ++y) {
Color color = texture.GetPixel(x, y);
texture.SetPixel(x, y, Premultiply(color));
Debug.Log("Automatically premultiplied alpha for " + assetPath);
private static Color Premultiply(Color color) {
float a = color.a;
return new Color(color.r * a, color.g * a, color.b * a, a);
private bool ShouldPremultiplyAlpha() {
return assetPath.EndsWith(".png", StringComparison.OrdinalIgnoreCase);
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
You can’t perform that action at this time.