当前位置:网站首页>unity 自定义小工具之“导出图集图片”
unity 自定义小工具之“导出图集图片”
2022-07-21 05:16:00 【一梭键盘任平生】
public class TestSaveSprite
{
[MenuItem("Tools/导出精灵")]
static void SaveSprite()
{
string resourcesPath1 = "Assets/ResourcesOut/ui/atlas";
foreach (Object obj in Selection.objects)
{
string selectionPath = AssetDatabase.GetAssetPath(obj);
if (selectionPath.StartsWith(resourcesPath1))
{
string selectionExt = System.IO.Path.GetExtension(selectionPath);
if (!selectionExt.Equals(".prefab"))
{
continue;
}
UIAtlas Atlas = UnityEditor.AssetDatabase.LoadAssetAtPath<UIAtlas>(selectionPath);
string outPath = Application.dataPath + "/outSprite/" + Atlas.name;
ExportTexturePNGFromAtlas(outPath, Atlas);
AssetDatabase.Refresh();
}
}
Debug.Log("SaveSprite Finished");
}
static void ExportTexturePNGFromAtlas(string folderPath, UIAtlas atlas)
{
List<UISpriteData> exitSpritesList = atlas.spriteList;
Texture2D atlasTexture = NGUIEditorTools.ImportTexture(atlas.texture, true, false, !atlas.premultipliedAlpha);
int oldwith = atlasTexture.width;
int oldHeight = atlasTexture.height;
Color32[] oldPixels = null;
foreach (var es in exitSpritesList)
{
//限制es.x在0 ~ oldwith之间
int xmin = Mathf.Clamp(es.x, 0, oldwith);
//限制es.y在0 ~ oldHeight之间
int ymin = Mathf.Clamp(es.y, 0, oldHeight);
//限制es.width在0 ~ oldwith 之间
int newWidth = Mathf.Clamp(es.width, 0, oldwith);
//限制es.width在0 ~ oldHeight 之间
int newHeight = Mathf.Clamp(es.height, 0, oldHeight);
if (newWidth == 0 || newHeight == 0) continue;
//获取32位像素颜色列表
if (oldPixels == null) oldPixels = atlasTexture.GetPixels32();
Color32[] newPixels = new Color32[newWidth * newHeight];
for (int y = 0; y < newHeight; ++y)
{
for (int x = 0; x < newWidth; ++x)
{
int newIndex = (newHeight - 1 - y) * newWidth + x;
int oldIndex = (oldHeight - 1 - (ymin + y)) * oldwith + (xmin + x);
newPixels[newIndex] = oldPixels[oldIndex];
}
}
Texture2D t = new Texture2D(newWidth, newHeight);
t.SetPixels32(newPixels);
t.Apply();
byte[] bytes = t.EncodeToPNG();
Texture2D.DestroyImmediate(t);
t = null;
if (!Directory.Exists(folderPath))
{
Directory.CreateDirectory(folderPath);
}
using (FileStream fs = new FileStream(folderPath + "/" + es.name + ".png", FileMode.CreateNew))
{
BinaryWriter writer = new BinaryWriter(fs);
writer.Write(bytes);
}
}
}
}
边栏推荐
- 2022acm summer training weekly report (II)
- 2022acm summer training weekly report (I)
- 第三周ACM训练报告
- Comprehensive: realtek/ Ruiyu wireless product map and market composition
- tmux学习记录
- Merge sort
- 力扣记录:动态规划5子序列问题(3)回文——647 回文子串,516 最长回文子序列
- 小实操:实现Myarray
- Programmation créative / groupe primaire (4e - 6e année) - graphisme créatif
- 2022ACM夏季集训周报(三)
猜你喜欢
844. Compare strings with backspace
004: print characters
Why is Le Xin in a hurry to release esp32-c3 when there are few words
MySQL安装
数据库初学笔记
Quick sort
328. Parity linked list
Which of these five special Bluetooth cores suits your application best
Realize 2D map 3D character 45 degree angle RPG game effect notes in cocos creator 3.2 (camera setting scheme)
Property analysis of matter protocol (II) multiple fabric and permission control
随机推荐
organize a team
2022ACM夏季集训周报(一)
328. Parity linked list
What is the difference between embedded hardware and software in embedded development?
How to complete the design of RGB lamp Bluetooth mesh module from 0 to 1
What opportunities and challenges does the development of instrument control panel face?
299. 猜数字游戏
Open source cocos creator 3 X game framework oops framework
Upgrade every day
AtCoder Beginner Contest 218 题解
取石子
P/np/np complete /np hard
P2814 家谱(字符串并查集)
第九周ACM训练报告
Characteristics and differences between PCB and integrated circuit
小实操:实现Myarray
力扣记录:单调栈——739 每日温度,496 下一个更大元素I,503 下一个更大元素II,42 接雨水,84 柱状图中最大的矩形
P1111 修复公路(并查集)
开源 Cocos Creator 3.x 游戏框架 oops-framework
2021 popularization group summary