- Home /
Load Texture2D converted into Sprites; How can i add them to a GameObject sucessfully?
I'm loading a lot of PNG's generated by the computer in a folder, after that i'm loading that PNG's like Texture2D at runtime through a function, until here, there seems to be no problem.
The problem is: How can i convert this Texture2D to Sprites in order to use them in a GameObject?
Here is the code:
public static Sprite Texture2DToSprites(this Texture2D txture2d)
{
try
{
Sprite sprite = Sprite.Create(txture2d, new Rect(0, 0, txture2d.width, txture2d.height), new Vector2(0.5f, 0.5f));
Debug.Log("Texture2D converted in Sprites Sucessfully!!");
return sprite;
}
catch (System.Exception ex)
{
Debug.Log("PrepareTexture2DToSprites Error: " + ex.ToString());
return null;
}
}
It would also be cool if you show me a way to test than the PNG's being loaded are loaded in a correct way, however that's not the main focus of the question, but it would help too. ;)
Thanks for th attention.
Answer by Obsdark · Oct 09, 2017 at 01:02 PM
nvm, i fix it, the problem was the name of the Sprite won't save itself, fixing that everything works like a charm, so this converter helps a lot and also do the work, however admins, feel free to delete the question if you think is duplicated or if necesary.
Thanks a lot
Your answer
Follow this Question
Related Questions
Changing the alpha channel of a sprite using the new Unity sprite system 1 Answer
How to double spirte/gameobject/prefab and control the result on those items? 0 Answers
How do i change a sprite when another gameobject with the same prefab is colliding / is near 1 Answer
How do i change a sprite when another gameobject with the same prefab is colliding / is near 1 Answer
Left Click While Holding Right Click 1 Answer