Unity编辑器—图片裁剪工具

6 篇文章 0 订阅
大尺寸的原图

在这里插入图片描述

裁剪后的图片

在这里插入图片描述

  1. 新建指定的文件夹存放裁切后的图片
  2. 批量选中要裁切的图片
  3. 在Inspector里面勾选Read/Write Enabel
  4. 右键——裁切
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();
 }
  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 1
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值