Search Unity

SetPixels not working on transparent pixels

Discussion in '2D' started by TBbadmofo, Sep 17, 2017.

  1. TBbadmofo

    TBbadmofo

    Joined:
    Apr 1, 2014
    Posts:
    14
    Using Unity 5.6.3p2

    I have a Texture2D that has some dynamic shapes colored into it and the background is transparent.
    I call GetPixels() to get a section of pixels to modify, and basically just set all of their colors to Color.black;
    Then I call SetPixels() on the textures and then use Apply().

    The pixels that had color and were not transparent will change to black, but the background part that is transparent, I cannot make it any color it just stays transparent.

    Any ideas what I'm doing wrong?
     
  2. eXonius

    eXonius

    Joined:
    Feb 2, 2016
    Posts:
    205
    Do you set the alpha value?
     
  3. TBbadmofo

    TBbadmofo

    Joined:
    Apr 1, 2014
    Posts:
    14
    Yes and all colors in Color, such as Color.black, use the alpha value.
    Also if I set the alpha to 0 then the visible pixels do turn transparent.
     
  4. eXonius

    eXonius

    Joined:
    Feb 2, 2016
    Posts:
    205
    Don't know then. Maybe show some code.
     
  5. TBbadmofo

    TBbadmofo

    Joined:
    Apr 1, 2014
    Posts:
    14
    The blue part is the transparent area.

    Code (CSharp):
    1. Color[] pixels = texture.GetPixels(left, bottom, right, top);
    2.  
    3. for(int i=0; i<pixels.length; i++)
    4. {
    5.     pixels[i] = Color.black;
    6. }
    7.  
    8. texture.SetPixels(left, bottom, right, top, pixels)
    9. texture.Apply()

     
  6. TBbadmofo

    TBbadmofo

    Joined:
    Apr 1, 2014
    Posts:
    14
    Figured it out.
    This can be caused by the Sprite Renderer's Draw Mode being set to Simple.
     
    Salyangoz and eXonius like this.