- Home /
Convert Texture2d to sprite
Hi I am having a png file downloaded from server. I have to create a sprite at run time out of texture is there any easy method to do this
public SpriteRenderer bg;
void change()
{
Texture2D sprites = Resources.Load<Texture2D>("lev1/"+name[i]) ;
Rect rec = new Rect(0, 0, sprites.width, sprites.height);
Sprite.Create(sprites,rec,new Vector2(0,0),1);
bg.sprite = Sprite.Create(sprites,rec,new Vector2(0,0),.01f);}
I used this code but the sprite that is formed is very large than expected As i change this texture to sprite from the inspector and then drag and drop this sprite set my camera according to it and and then change the back sprite to texture and use the I code to convert the texture to sprite I get different result ...
Answer by blueteak · Apr 02, 2015 at 01:40 PM
You have the right implementation, use Texture2D.Resize to get it smaller.
Answer by NinjaISV · Sep 11, 2015 at 03:44 PM
This article will probably solve your issue: http://answers.unity3d.com/questions/650552/convert-a-texture2d-to-sprite.html
But I use Rect rec = new Rect(0, 0, texture.width, texture.height);
dust.GetComponent ().sprite=Sprite.Create(texture,rec,new Vector2(0.5f,0.5f),100);
But result is image became small than before sprite.
Someone know how to solve.
Thanks for the help!
Your answer
Follow this Question
Related Questions
Can I create a sprite at runtime? 3 Answers
Using Texture2D created at runtime to create a Sprite decreases Sprite quality. (Android) 1 Answer
Convert texture 2d to sprite and save 0 Answers
Change SpriteRenderers Texture2D in runtime 1 Answer
White Edges on Sprites Loaded from PNG at Run-time 0 Answers