Author Topic: Convert UISprite to Texture2D  (Read 3588 times)

19xdxd

  • Newbie
  • *
  • Thank You
  • -Given: 0
  • -Receive: 0
  • Posts: 1
    • View Profile
Convert UISprite to Texture2D
« on: March 22, 2014, 06:31:32 AM »
Hi,

I'm having a bit of trouble converting UISprite to Texture2D

I only want the selected sprite to be converted to the Texture2D and not the whole thing

I have done the following so far:


  1. UIAtlas atlas = objectWithSprite.GetComponent<UISprite>().atlas;
  2. UISpriteData sprite = objectWithSprite.GetComponent<UISprite>().GetAtlasSprite();
  3.  
  4. Texture2D atlasTexture = atlas.spriteMaterial.mainTexture as Texture2D;
  5. Rect outer = new Rect(sprite.x, sprite.y, sprite.width, sprite.height);
  6. Rect uv0 = NGUIMath.ConvertToTexCoords(outer, atlasTexture.width, atlasTexture.height);
  7.  
  8. UITexture tex = new UITexture();
  9. tex.uvRect = uv0;
  10.  
  11. Texture2D t = tex.mainTexture;

Can someone please tell me what I am doing wrong please?

ArenMook

  • Administrator
  • Hero Member
  • *****
  • Thank You
  • -Given: 337
  • -Receive: 1171
  • Posts: 22,128
  • Toronto, Canada
    • View Profile
Re: Convert UISprite to Texture2D
« Reply #1 on: March 22, 2014, 03:14:04 PM »
Why are you doing this? If you are trying to extract a sprite, use the "Save As" button in atlas inspector.