Tasharen Entertainment Forum

Support => NGUI 3 Support => Topic started by: 19xdxd on March 22, 2014, 06:31:32 AM

Title: Convert UISprite to Texture2D
Post by: 19xdxd 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?
Title: Re: Convert UISprite to Texture2D
Post by: ArenMook 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.