Author Topic: Textures offset when copying individual sprites from UIAtlas to a new texture  (Read 2084 times)

Biggix

  • Newbie
  • *
  • Thank You
  • -Given: 2
  • -Receive: 0
  • Posts: 33
    • View Profile
Hi,

I have a bunch of textures within a UIAtlas. I need to "extract" those textures from the atlas (in runtime) and have each one applied to a UITexture for a specific purpose. Textures are RGBA32.

However when I do the copying, the resulting textures are strangely offset by some non-constant Y value (around 512 in some cases). It's clear that something goes wrong around the GetPixels phase.

This is how I do it:
  1.  
  2. List<UISpriteData> spriteList = sourceAtlas.spriteList;
  3. spriteTextures = new Texture2D[spriteList.Count];
  4.        
  5. Texture2D src_tex2d = Instantiate(sourceTex) as Texture2D;
  6.  
  7.                 for (int i=0; i<spriteList.Count; i++) {
  8.                        
  9.                         UISpriteData data = spriteList[i];
  10.                        
  11.                         Color[] pix = src_tex2d.GetPixels(data.x, data.y, data.width, data.height);
  12.                         spriteTextures[i] = new Texture2D(data.width, data.height);
  13.                        
  14.                         spriteTextures[i].SetPixels(pix);
  15.                         spriteTextures[i].Apply();
  16. }
  17.  
  18.  


Please help!!

Biggix

  • Newbie
  • *
  • Thank You
  • -Given: 2
  • -Receive: 0
  • Posts: 33
    • View Profile
Update. I have tried with the following code. But the result is the same: the sprites are mysteriously shifted by different Y value! And I have checked the data.x and data.y properties using debug, they're just fine. :(  >:(

  1. Texture2D src_tex2d = Instantiate(sourceTex) as Texture2D;//(sourceAtlas.texture as Texture2D);
  2. Color32[] srcTexture = src_tex2d.GetPixels32();
  3.  
  4. for (int i=0; i<spriteList.Count; i++) {
  5.            
  6.             UISpriteData data = spriteList[i];;
  7.        
  8.             Color32[] newPixels = new Color32[data.width * data.height];
  9.            
  10.             for (int y = 0; y < data.height; ++y)
  11.             {
  12.                 for (int x = 0; x < data.width; ++x)
  13.                 {
  14.                     int newIndex = y * data.width + x;
  15.                     int oldIndex = (data.y + y) * src_tex2d.width + (data.x + x);
  16.                     newPixels[newIndex] = srcTexture[oldIndex];
  17.                 }
  18.             }
  19.            
  20.             spriteTextures[i] = new Texture2D(data.width, data.height);
  21.    spriteTextures[i].SetPixels32(newPixels);
  22.             spriteTextures[i].Apply();
  23.  
  24. }
  25.    

Biggix

  • Newbie
  • *
  • Thank You
  • -Given: 2
  • -Receive: 0
  • Posts: 33
    • View Profile
Solved it! In case it would help anyone, this is the culprit line

  1. Color[] pix = src_tex2d.GetPixels(data.x, sourceAtlas.texture.height - data.y - data.height, data.width, data.height);
  2.                

But - why!? ArenMook, any comments?

ArenMook

  • Administrator
  • Hero Member
  • *****
  • Thank You
  • -Given: 337
  • -Receive: 1171
  • Posts: 22,128
  • Toronto, Canada
    • View Profile
IIRC, because texture data is specified from bottom-left, while UVs are from top-left.