unity中sprite转成texture2d,写一个工具类
时间: 2024-05-06 15:19:22 浏览: 231
可以使用以下代码将Sprite转换成Texture2D:
```
public static Texture2D SpriteToTexture2D(Sprite sprite)
{
Texture2D texture = new Texture2D((int)sprite.rect.width, (int)sprite.rect.height, TextureFormat.RGBA32, false);
texture.filterMode = FilterMode.Point;
Color[] spriteColors = sprite.texture.GetPixels((int)sprite.textureRect.x,
(int)sprite.textureRect.y,
(int)sprite.textureRect.width,
(int)sprite.textureRect.height);
texture.SetPixels(spriteColors);
texture.Apply();
return texture;
}
```
将上述代码放到一个静态类中即可实现一个工具类,用法类似于以下示例:
```
Sprite sprite = Resources.Load<Sprite>("YourSpriteName");
Texture2D texture = SpriteToTexture2D(sprite);
```
阅读全文