'How to change the Texture Type of a .png image to Sprite through a script in Unity?
I am using Unity and I have a lot of images in the Resource folder. I am saving these images using my nodejs and MySQL server.
What I need to do is that when reading those images, I change the Texture Type to "Sprite" through a script (by default is set to "Default"), since it is necessary that they have a Texture Type "Sprite" to be able to put the image in a canvas.
I know that it can be done using the Unity inspector but I need to do it with a script, since I don't know which images are going to be in what quantity.
Greetings. inspector configuration on an image
Solution 1:[1]
As far as I understand you want to create a sprite from texture. So you can use Sprite.Create function here I add a function from which you can convert texture to sprite.
public static Sprite TextureToSprite(Texture2D texture) => Sprite.Create(texture, new Rect(0f, 0f, texture.width, texture.height), new Vector2(0.5f, 0.5f), 50f, 0, SpriteMeshType.FullRect);
you can call like this
var sprite = TextureToSprite(yourTexture);
now you can use this sprite in the canvas image component. NOTE :- You can change Sprite.Create functions parameters according to your need.
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
| Solution | Source |
|---|---|
| Solution 1 | Jaimin |
