Search Unity

  1. Welcome to the Unity Forums! Please take the time to read our Code of Conduct to familiarize yourself with the forum rules and how to post constructively.
  2. We have updated the language to the Editor Terms based on feedback from our employees and community. Learn more.
    Dismiss Notice

Question How to LoadImage with transparency?

Discussion in 'General Graphics' started by Zylkowski_a, May 30, 2020.

  1. Zylkowski_a

    Zylkowski_a

    Joined:
    Jul 27, 2019
    Posts:
    157
    Right now I am using following code to load textures from files in my game(and I want to load them like this):
    Code (CSharp):
    1.         var bytes = File.ReadAllBytes(AbsolutePath);
    2.         var texture = new Texture2D(1, 1,TextureFormat.ARGB32,false);
    3.         texture.LoadImage(bytes);
    The thing is that this texture has transparent regions and they become black after loading texture. How can I load partially transparent texture using Texture2D.LoadImage?
     
  2. Olmi

    Olmi

    Joined:
    Nov 29, 2012
    Posts:
    1,553
    Hi @Zylkowski_a,
    If you are loading a PNG image, have you checked the loaded textures' alpha channel to see if there's some data?
     
  3. Zylkowski_a

    Zylkowski_a

    Joined:
    Jul 27, 2019
    Posts:
    157
    I don't really know how should I check it there's some data. And yes I am loading PNG btw
     
  4. Olmi

    Olmi

    Joined:
    Nov 29, 2012
    Posts:
    1,553
  5. Zylkowski_a

    Zylkowski_a

    Joined:
    Jul 27, 2019
    Posts:
    157
    @Olmi Unity say that this pixel is RGBA(0.000, 0.000, 0.000, 1.000), I tried with one that was RGBA(0.000, 0.000, 0.000, 0.000) and both of them are black ingame
     
  6. Olmi

    Olmi

    Joined:
    Nov 29, 2012
    Posts:
    1,553
    Well, if the image data is correct,it should show up. But that depends also on your shader.
    I just tried loading my test png from disk and it does show up correctly when I apply that image as a sprite.

    I used code like this:
    Code (CSharp):
    1. var bytes = File.ReadAllBytes(AbsolutePath);
    2. var texture = new Texture2D(1, 1,TextureFormat.ARGB32,false);
    3. texture.LoadImage(bytes);
    4. Sprite sprite = Sprite.Create(texture, new Rect(0,0,texture.width, texture.height), new Vector2(0,0));
    5. GetComponent<SpriteRenderer>().sprite = sprite;
    Result looks like this:
    alpha_circle.PNG

    But I'd definitely also check the image and try some other tool/plugin for saving like SuperPNG in Photoshop. I think @bgolus has suggested it too sometime in the past as a good option, if I remember correctly.