大尺寸的原图
裁剪后的图片
- 新建指定的文件夹存放裁切后的图片
- 批量选中要裁切的图片
- 在Inspector里面勾选Read/Write Enabel
- 右键——裁切
Editor下的代码
[MenuItem("Assets/裁剪图片")]
public static void CutTexture()
{
Object[] m_objects = Selection.GetFiltered(typeof(Object), SelectionMode.DeepAssets);
for (int i = 0; i < m_objects.Length; i++)
{
// 获取路径
//string path_tex = AssetDatabase.GetAssetPath(m_objects[i]);
if (m_objects[i].GetType() == typeof(Texture2D))
{
Texture2D texture = m_objects[i] as Texture2D;
//Rect 左起位置,下起位置,图片宽度,图片高度
Rect rect = new Rect(321, 765, 468, 750);
int startX = (int)rect.x;
int startY = (int)rect.y;
//int endX = startX + (int)rect.width;
//int endY = startY + (int)rect.height;
Color[] pixels = texture.GetPixels(startX, startY, (int)rect.width, (int)rect.height);
Texture2D clippedImage = new Texture2D((int)rect.width, (int)rect.height);
// 指定位置更改为指定像素
//for (int j = 0; j < pixels.Length; j++)
//{
// colorItem item = new colorItem();
// if (pixels[j] != Color.clear)
// {
// item.pos.x = j % texture.width;
// item.pos.y = j % texture.height;
// item.color = pixels[j];
// Debug.Log("pos: " + j + "/" + item.pos);
// }
//}
clippedImage.SetPixels(pixels);
clippedImage.Apply();
byte[] pngData = clippedImage.EncodeToPNG();
// 指定的存放位置
File.WriteAllBytes(Application.dataPath + "/ImgRes/" + i.ToString() + ".png", pngData);
}
}
AssetDatabase.Refresh();
}