切图需求

假设有一张大的UI的图集,我们想把它里面的小图一张一张地切割出来,如果有plist文件,请查阅我的另一篇文章《还原TexturePacker plist 文件 切开各小图片

今天我们使用 Unity4.3或更高版本自带的 Sprite Editor 来导出切片精灵

切图效果

步骤

1、准备一张大的图集,导入Unity的Asset/Resources/XXX/文件夹下(注意:图集文件一定要放在Resources文件下)

2、该图集默认是Texture,我们需要把它的Type修改成Sprite

3、接着修改Sprite Mode为Multiple,应用。然后点击 Sprite Editor

4、在打开的Sprite Edirot 窗口中把图集切成多个Sprite

插件代码

5、编写TestExportSprite.cs,放在Editor目录下

using UnityEngine;
using UnityEditor; public class TestExportSprite
{ [MenuItem("Assets/导出选中图片为单独png")]
static void ExportSelSprite()
{
string resourcesPath = "Assets/Resources/";
foreach (Object obj in Selection.objects)
{
string selectionPath = AssetDatabase.GetAssetPath(obj); // 必须最上级是"Assets/Resources/"
if (selectionPath.StartsWith(resourcesPath))
{
string selectionExt = System.IO.Path.GetExtension(selectionPath);
if (selectionExt.Length == )
{
continue;
} // 得到导出路径
string loadPath = selectionPath.Remove(selectionPath.Length - selectionExt.Length);
loadPath = loadPath.Substring(resourcesPath.Length); // 加载此文件下的所有资源
Sprite[] sprites = Resources.LoadAll<Sprite>(loadPath);
if (sprites.Length > )
{
// 创建导出文件夹
string outPath = Application.dataPath + "/outSprite/" + loadPath;
System.IO.Directory.CreateDirectory(outPath); foreach (Sprite sprite in sprites)
{
// 创建单独的纹理
Texture2D tex = new Texture2D((int)sprite.rect.width, (int)sprite.rect.height, sprite.texture.format, false);
tex.SetPixels(sprite.texture.GetPixels((int)sprite.rect.xMin, (int)sprite.rect.yMin,
(int)sprite.rect.width, (int)sprite.rect.height));
tex.Apply(); // 写入成PNG文件
System.IO.File.WriteAllBytes(outPath + "/" + sprite.name + ".png", tex.EncodeToPNG());
}
Debug.Log(string.Format("Export {0} to {1}",loadPath,outPath));
}
}
}
Debug.Log("Export All Sprites Finished");
}
}

5、使用Sprite Editor把图集切割成Sprite之后,修改图集的属性为Advanced,并勾选 Read/Write Enabled 和Transpare

否则当你导出切片时会报错

UnityException: Texture 'UIAtlas' is not readable, the texture memory can not be accessed from scripts. You can make the texture readable in the Texture Import Settings.
UnityEngine.Texture2D.GetPixels (Int32 x, Int32 y, Int32 blockWidth, Int32 blockHeight) (at C:/BuildAgent/work/aeedb04a1292f85a/artifacts/EditorGenerated/TextureBindings.cs:)
TestSaveSprite.SaveSprite () (at Assets/Editor/TestSaveSprite.cs:)

5、在Resources目录下选中UIAtlas.psd,右键,选择“导出选中图片为单独png

说明

部分内容参考自:http://blog.csdn.net/akof1314/article/details/38845933

最新文章

  1. Web 设计新趋势: 使用 SVG 代替 Web Icon Font
  2. 安装ipvsadm时出现下面所示错误,MARK
  3. Bolt 动画
  4. 《algorithm puzzles》——谜题
  5. OC基础 类的三大特性
  6. 黄聪:Microsoft Enterprise Library 5.0 系列教程(四) Logging Application Block
  7. mybatis 入门进阶之 mapper
  8. 从url下载图片--java与python实现方式比较
  9. JavaScript 插件的书页翻转效果
  10. Python学习-使用opencv-python提取手掌和手心及部分掌纹
  11. ubuntu16+zabbix3.4+grafana环境搭建记录
  12. MVC设计模式的简单理解
  13. C#委托与事件总结
  14. windows 64位下 Octave 不能画图的解决办法
  15. hdu 5726 tetrahedron 立体几何
  16. 用Python操作Named pipe命名管道,实用做法——os.read 或 os.write
  17. css设置背景模糊
  18. 轻松应对IDC机房带宽突然暴涨问题
  19. Turkey HSD检验法/W法
  20. CXF生成调用webservice的客户端

热门文章

  1. 总结一下SQL的全局变量
  2. HTML5中的SVG
  3. js中的排序
  4. .NET破解之百度网盘批量转存工具
  5. OC中几种集合的遍历方法(数组遍历,字典遍历,集合遍历)
  6. CollectionView 简用
  7. setSupportActionBar(toolbar)导致程序崩溃闪退
  8. 朝花夕拾-android 一个注册新用户时,多步填写用户资料的框架
  9. MySQL学习总结(摘抄)
  10. 安装MySQL,在./configure时出现错误:error: No curses/termcap library found的解决办法