1
\$\begingroup\$

I Have image with a sprite and i want to change this image sprite to Texture so that I can assign it to the material And i don't want to keep multiple copies for each image as a sprite and texture.

Is there any way to change sprite to texture at run time?

\$\endgroup\$
1
\$\begingroup\$
// assume "sprite" is your Sprite object
 var croppedTexture = new Texture2D( (int)sprite.rect.width, (int)sprite.rect.height );
 var pixels = sprite.texture.GetPixels(  (int)sprite.textureRect.x, 
                                         (int)sprite.textureRect.y, 
                                         (int)sprite.textureRect.width, 
                                         (int)sprite.textureRect.height );
 croppedTexture.SetPixels( pixels );
 croppedTexture.Apply();

this should work for you. you can follow this thread for more info: https://answers.unity.com/questions/651984/convert-sprite-image-to-texture.html

\$\endgroup\$
  • \$\begingroup\$ Thanks. I didn't need to crop or anything. but from thread you share i got my answer which was "Image.sprite.texture". \$\endgroup\$ – Swati Sep 25 at 6:36
0
\$\begingroup\$

For just converting Image's sprite To Texture use this:

Image.sprite.texture

And this will do the Job.

Thanks @virtouso for sharing the thread.

\$\endgroup\$

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.