Search Unity

GetPixels returns an array of "empty" colors.

Discussion in 'Scripting' started by Redden44, May 13, 2019.

  1. Redden44

    Redden44

    Joined:
    Nov 15, 2014
    Posts:
    159
    Hello, I need to get the pixels from a sprite I created at runtime with "Sprite.Create" but "GetPixels" returns an array of RGBA(1.0, 1.0, 1.0, 0.0) colors, which turns into an empty sprite. The base sprite is fine and I checked the sprite's texture and it's readable, any idea how to fix it please?

    Here's the code:
    Code (CSharp):
    1. private void BuildSprite_1()
    2.     {
    3.         Sprite sprite = SpriteManager.Instance.GetSprite("LoamySoil");
    4.         Debug.Log(string.Format("Width {0}  Height {1}  isReadable {2}", sprite.texture.width, sprite.texture.height, sprite.texture.isReadable));
    5.  
    6.         int tileResolution_x = (int)sprite.rect.width;// sprite.texture.width; //64;
    7.         int tileResolution_y = (int)sprite.rect.height;// sprite.texture.height; //32;
    8.        
    9.         int textureWidth = tileResolution_x;// sprite.texture.width;
    10.         int textureHeight = tileResolution_y;// sprite.texture.height;
    11.         Texture2D texture = new Texture2D(textureWidth, textureHeight);
    12.        
    13.         Color[] tilePixels = sprite.texture.GetPixels(0, 0, tileResolution_x, tileResolution_y);
    14.         foreach (Color c in tilePixels)
    15.         {
    16.             Debug.Log(c);
    17.         }
    18.         texture.SetPixels(0, 0, tileResolution_x, tileResolution_y, tilePixels);
    19.  
    20.         texture.wrapMode = TextureWrapMode.Clamp;
    21.         texture.filterMode = FilterMode.Point;
    22.         texture.Apply();
    23.  
    24.         uint extrude = 1;
    25.         SpriteMeshType spriteMeshType = SpriteMeshType.Tight;
    26.  
    27.         int rect_x = 0;
    28.         int rect_y = 0;
    29.         int width = textureWidth;
    30.         int height = textureHeight;
    31.         Rect spriteRect = new Rect(rect_x, rect_y, width, height);
    32.         float pivotPointX = 0.0f;
    33.         float pivotPointY = 0.0f;
    34.         Vector2 pivotPoint = new Vector2(pivotPointX / width, pivotPointY / height);
    35.         int pixelsPerUnit = 32;
    36.  
    37.         Sprite newSprite = Sprite.Create(texture, spriteRect, pivotPoint, pixelsPerUnit, extrude, spriteMeshType);
    38.  
    39.         GameObject go = new GameObject("NewSprite");
    40.         SpriteRenderer spriteRen = go.AddComponent<SpriteRenderer>();
    41.         spriteRen.sprite = newSprite;
    42.     }
     
  2. csofranz

    csofranz

    Joined:
    Apr 29, 2017
    Posts:
    1,556
    Your Alpha (the fourth value in RGBA) is Zero, meaning Zero opacit = full transfparency. Try changing it to 1.0
     
  3. Redden44

    Redden44

    Joined:
    Nov 15, 2014
    Posts:
    159
    I know that, that is the problem..GetPixels should returns an array of colors (the sprite is green), instead the array has got only black transparent colors.
     
  4. csofranz

    csofranz

    Joined:
    Apr 29, 2017
    Posts:
    1,556
    To nitpick, if what you write is true, it has white transparent pixels - not that you and I could tell the differece:) - can you try the simpler Create() call without pixelsPerUnit? I know C# should convert the int to float, but I've seen stranger things go wrong, and here we simply eliminate more thaings that can go wrong.
     
  5. Redden44

    Redden44

    Joined:
    Nov 15, 2014
    Posts:
    159
    Yes sorry, white, I was thinking about Color32 (1-255) instead of Color (0-1).

    Anyway I solved the problem, I was using (0, 0) to set the starting point for GetPixels because I thought that the sprite.rect and its texture had the same size, which is not true..the sprite.rect is 64x32 while the texture is 1024x512..I have no idea why it's like that..is that the minimum size for a texture when you use Sprite.Create?
    So I changed the origin from (0, 0) to (int)sprite.rect.min.x and (int)sprite.rect.min.y and now it's working.

    Still I'm really curious to know why sprite.rect and sprite.texture have completely different sizes.