EncodeToPNG doesn't create alpha channel?

or does it? i’m confused.

using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEditor;
using System.IO;

public class SavePNG : EditorWindow
{
    private Texture2D texture;

    [MenuItem("Window/SavePNG")]
    static void Init()
    {
        SavePNG window = (SavePNG)EditorWindow.GetWindow(typeof(SavePNG));
        window.Show();
    }
    void OnGUI()
    {
        if(GUILayout.Button("Create texture"))
        {
            texture = new Texture2D(2048,2048, TextureFormat.RGBA32, false);
            Color32 c = new Color(1,0,0,1);
            Color32[] colors = texture.GetPixels32();
            for(int i=0; i<colors.Length; i++)
            {
                colors[i] = c;
                colors[i].a = (byte)(1f*i/colors.Length);
            }
            texture.SetPixels32(colors);
            texture.Apply();
        }

        EditorGUILayout.ObjectField("texture", texture, typeof(Texture2D));

        if(GUILayout.Button("Save texture"))
        {
            byte[] bytes = texture.EncodeToPNG();
            File.WriteAllBytes(Application.dataPath + "/Texture.png", bytes);
            AssetDatabase.Refresh(ImportAssetOptions.ForceUncompressedImport);
            Debug.Log(texture.format);

        }
    }
}

in Unity it shows RGBA32 format and has a Alpha channel.
If i open it up in Photoshop, its totally blank transparent without Alpha Channel?

anyone knows what’s going on?

1 Like

(byte)(256f * i / colors.Length);

thats just to give some variation in the alpha, doesn’t matter if it’s there. Have the same problem without any colors changes.

Odd.

When I made that change, I got an alpha channel. I don’t know anything about photoshop but the alpha was clearly visible in the Unity editor. What version of Unity are you using? Whatever the answer, your solution might just be upgrading to 2017.2…

colors[i].a = (byte)(1f*i/colors.Length);

Color32 is 0-255, not 0.0-1.0. You’re setting the alpha for every pixel to 0.

–Eric

2 Likes

@Eric5h5 is right. You should listen to him. :wink:

hahaha yes i forgot thanks, i was wondering why they stayed black :stuck_out_tongue:
but that still doesn’t solve my problem :frowning:

Right…yet it does work on v2017.2, like I said in my original reply.

Keep in mind that when you open a PNG with an alpha channel in photoshop it does NOT appear transparent anywhere.

You have to look over in the alpha channel (channels tab) to see it, but the window remains fully opaque. It’s apparently by design but I find it maddening when I’m using Photoshop.

Try opening the alpha PNG with the Gimp image program and you will see the alpha “holes.”

EDIT: correction! Apparently the latest Photoshop does show transparencies properly. I know that older version do not. Not sure how much older…

this is what i get when saving a png in unity
in Unity it shows correct:


the alpha channel is there:

this is how i get it in PS:

it does seem to work correct in unity thou…

1 Like

bump…
Every PNG i save is messed up :frowning:
made a flowmap painter to paint on rivers inside unity and if i open it in unity i get the same result as my above post, desperately need a solution…

What I don’t get is that your exact code, with that one line fixed, generates a .PNG with transparency. So either you aren’t running the fixed version of the code, you aren’t measuring the transparency correctly, or there is something different about your environment than everyone else’s.

Which of those possibilities would you like help further investigating?

To me it looks fine, what do you expect to get exactly? Photoshop displays PNGs only the way you see above, it does not support seperated alpha channels for pngs. If you need the alpha chanel seperated then you have to open the map in a different programm or save something else as png.
You could store the RGB map and the Alpha in two different JPGs with Quality setting of 100. Then import to Photoshop, put the mask in a seperate channel and then save as a TGA.

Unity should add a EncodeToTGA(), that would be neat.
EDIT: Oh look, maybe try this?

3 Likes

yes i changed to the jpg method, didn’t know PS doesn’t support alpha channel in png :eyes:
and i vote YES on the EncodeToTGA() :slight_smile:
EDIT: Oh wow thanks for the link!!!

I think the thing you’re missing is that alpha is used as transparency in Photoshop. That’s what it’s supposed to look like. Anywhere the alpha channel is black is transparent, and anywhere it’s white is opaque. That’s how transparency usually works. If you want to ignore the alpha channel in Photoshop I think there’s a command somewhere to just turn it off but at the moment I don’t remember where.

1 Like

Cue a close-in camera view of @McDev02 saying “There’s a script for that…” :slight_smile:

Thanks! Good to have around. TGA is my preferred format because it’s super-simple.

I tested the script now and it works fine. But I thought the Alpha handling isn’t good so I added a boolean “storeAlpha” to the Method and replaced “_texture2D.format == TextureFormat.ARGB32” with that boolean.
Whatever people say, TGA is still the best format for textures.
BTW: If you save PNGs in Photoshop and transparency is 0 then color information is lost. So only use PNGs if alpha is transparency. For UI sprites it is the preferrred format, but not for textures.

1 Like

the problem was that there isn’t a alpha channel present in PS…? no idea why not btw.

Yes there is. That image you posted is showing transparency. See those grey and white squares in the background? Those are not actually part of the image, that is Photoshop’s “transparent background”. It’s trying to show you that most of the image is transparent and the only parts that aren’t transparent are where you see colors. If you copy and paste that image on top of a different image in Photoshop, it only pastes the non-transparent parts. You won’t see the grey and white squares anymore. Transparency and alpha channel are the same thing in Photoshop. You said you’re making a Flow Map and they use alpha channels for something else, but that’s a very edge case. MOST of the time, with images, alpha means transparency.

I think he means there’s no alpha channel in the Channels window.

1 Like