Unity Loaded texture during RUNTIME has white or black line

It is because of Texture's alpha is not dilated for the gap.
Details could be found HOWTO-alphamaps

So how to fix it:
  • if it's in your Unity project, just enable this texture's AlphaIsTransparency in Texture Importing Setting inspector.
public static void DilateTexture()
{
    if (Selection.activeObject == null)
    {
        if (EditorUtility.DisplayDialog("Error Selection", "Select a texture", "OK"))
        {
            return;
        }
    }

    var texture = Selection.activeObject as Texture2D;
    if (texture == null)
    {
        if (EditorUtility.DisplayDialog("Error Selection", Selection.activeObject.name + " is not a texture", "OK"))
        {
            return;
        }
    }

    var path = AssetDatabase.GetAssetPath(texture);
    var textureImporter = TextureImporter.GetAtPath(path) as TextureImporter;
    var originAlpha = textureImporter.alphaIsTransparency;
    var originReadable = textureImporter.isReadable;
    var originMax = textureImporter.maxTextureSize;

    textureImporter.alphaIsTransparency = true;
    textureImporter.isReadable = true;
    textureImporter.maxTextureSize = 4096;
    textureImporter.SaveAndReimport();

    var dilatePath = path + ".png";

    var bytes = texture.EncodeToPNG();
    var overwrite = EditorUtility.DisplayDialog("Warning!", "Overwrite original file?\n" + path, "YES", "NO");
    if (overwrite)
    {
        File.WriteAllBytes(Application.dataPath + path.Replace("Assets", ""), bytes);
    }
    else
    {
        File.WriteAllBytes(Application.dataPath + dilatePath.Replace("Assets", ""), bytes);
    }

    textureImporter.alphaIsTransparency = originAlpha;
    textureImporter.maxTextureSize = originMax;
    textureImporter.isReadable = originReadable;

    AssetDatabase.Refresh();
}
  • if it is a raw texture loaded during runtime, just found a plugin to do this. dowload Adv. Edge Padding
WRITTEN BY:    Suntabu