2025-05-18 01:04:31 +08:00

144 lines
3.7 KiB
C#

using System.IO;
using UnityEditor;
using UnityEngine;
using UnityEngine.UI;
public static class ExportSpritesAsFile
{
/// <summary>
/// 注意,使用此编辑器拓展需要先选中图集文件
/// </summary>
[MenuItem("Tools/图集元素转文件工具")]
static void ProcessToSprite()
{
Texture2D image = Selection.activeObject as Texture2D;//获取选择的对象
string path = AssetDatabase.GetAssetPath(image);//图片路径名称
string rootPath = Path.GetDirectoryName(path);//获取路径名称
var sfn = image.name;
if (sfn.StartsWith("SpriteAtlasTexture"))
{
var fi = sfn.IndexOf('-') + 1;
var si = sfn.IndexOf('-', fi);
var spn = sfn.Substring(fi, si - fi);
Debug.Log(spn);
}
TextureImporter texImp = AssetImporter.GetAtPath(path) as TextureImporter;//获取图片入口
if (!Directory.Exists(Path.Combine(rootPath, image.name)))
{
AssetDatabase.CreateFolder(rootPath, image.name);//创建文件夹
}
foreach (SpriteMetaData metaData in texImp.spritesheet)//遍历小图集
{
Texture2D myimage = new Texture2D((int)metaData.rect.width, (int)metaData.rect.height);
//for (int y = (int)metaData.rect.y; y < metaData.rect.y + metaData.rect.height; y++)//Y轴像素
//{
// for (int x = (int)metaData.rect.x; x < metaData.rect.x + metaData.rect.width; x++)
// myimage.SetPixel(x - (int)metaData.rect.x, y - (int)metaData.rect.y, image.GetPixel(x, y));
//}
myimage.SetPixels(image.GetPixels((int)metaData.rect.x, (int)metaData.rect.y, (int)metaData.rect.width, (int)metaData.rect.height));
if (myimage.format != TextureFormat.ARGB32 && myimage.format != TextureFormat.RGB24)
{
Texture2D newTexture = new Texture2D(myimage.width, myimage.height);
newTexture.SetPixels(myimage.GetPixels(0), 0);
myimage = newTexture;
}
var pngData = myimage.EncodeToPNG();
File.WriteAllBytes(rootPath + "/" + image.name + "/" + metaData.name + ".PNG", pngData);
// 刷新资源窗口界面
AssetDatabase.Refresh();
}
}
[MenuItem("Tools/图集元素检查工具")]
static void CheckSprite()
{
var sgo = Selection.activeGameObject;
if (sgo)
{
var images = sgo.GetComponentsInChildren<Image>();
foreach (var image in images)
{
if (image.sprite)
{
var sp = AssetDatabase.GetAssetPath(image.sprite);
var sfn = Path.GetFileName(sp);
if (sfn.StartsWith("SpriteAtlasTexture"))
{
Debug.Log(image.name);
}
//Debug.Log(sp);
}
}
var srs = sgo.GetComponentsInChildren<SpriteRenderer>();
foreach (var sr in srs)
{
if (sr.sprite)
{
var sp = AssetDatabase.GetAssetPath(sr.sprite);
var sfn = Path.GetFileName(sp);
if (sfn.StartsWith("SpriteAtlasTexture"))
{
Debug.Log(sr.name);
}
//Debug.Log(sp);
}
}
}
}
[MenuItem("Tools/图集元素替换工具")]
static void ReplaceSprite()
{
var sgo = Selection.activeGameObject;
if (sgo)
{
var images = sgo.GetComponentsInChildren<Image>();
foreach (var image in images)
{
if (image.sprite)
{
var sp = AssetDatabase.GetAssetPath(image.sprite);
var sfn = Path.GetFileName(sp);
if (sfn.StartsWith("SpriteAtlasTexture"))
{
var fi = sfn.IndexOf('-') + 1;
var si = sfn.IndexOf('-', fi);
var spn = sfn.Substring(fi, si - fi);
Debug.Log(spn + " " + image.sprite.name);
var newsp = sp.Substring(0, sp.Length - 4) + "/" + image.sprite.name + ".png";
var news = AssetDatabase.LoadAssetAtPath<Sprite>(newsp);
Debug.Log(news != null ? news.name : "empty");
if (news)
{
image.sprite = news;
}
//Debug.Log(newsp);
}
//Debug.Log(sp);
}
}
}
}
}