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?

// 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.height );
 croppedTexture.SetPixels( pixels );

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

  • \$\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 '19 at 6:36

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


And this will do the Job.

Thanks @virtouso for sharing the thread.


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.