图集拆分后可以将子图单独保存成png图片,使用如下代码,
var sprites = Resources.LoadAll<Sprite>("main");
Debug.Log("拆分的图片如下:");
var path = "D:/Splite/";
if (!Directory.Exists(path))
{
Directory.CreateDirectory(path);
}
int counter = 0;
foreach (var sprite in sprites)
{
Debug.Log(sprite.name);
string fname = path + sprite.name + ".png";
if (sprite.name.Contains("/"))
{
var index = sprite.name.LastIndexOf("/");
var pt = path + sprite.name.Substring(0, index);
if(!Directory.Exists(pt))
{
Directory.CreateDirectory(pt);
Debug.Log($"创建目录{pt}");
}
fname = path + sprite.name + ".png";
}
byte[] bytes = ToPNG(sprite);
File.WriteAllBytes(fname, bytes);
++counter;
if (counter > 100)
{
break;
}
}
Debug.Log($"保存到目录{path},
unity sprite保存成PNG图片
最新推荐文章于 2024-06-06 17:17:13 发布
![](https://img-home.csdnimg.cn/images/20240711042549.png)