切图需求

假设有一张大的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

Sprite Editor 图集切片精灵的更多相关文章

  1. Unity 导出切片精灵

    http://blog.csdn.net/akof1314/article/details/38845933 设有一张png/tga图集,导入到Unity,放置目录"Assets/Resou ...

  2. Sprite Editor

    [Sprite Editor] 在Unity3D中,一个图片可以有多种类型(如下图).对于2D游戏开发,最常用的类型就是Sprite. 下图是Sprite Texture的属性,Packing Tag ...

  3. 15、Cocos2dx 3.0游戏开发找小三之Sprite:每一个精灵都是上辈子折翼的天使

    重开发人员的劳动成果,转载的时候请务必注明出处:http://blog.csdn.net/haomengzhu/article/details/30475395 Sprite  Sprite 能够说是 ...

  4. NGUI使用图集的精灵换图片

    创建了一个sprite,选择的是某一个图集下的sprite,现在我想点击它来换图片.前提是你的sprite已经加了UIButton 代码如下: public void OnClick() { if ( ...

  5. [置顶] Unity2d引入新功能SpriteAtlas,Sprite新的图集方式

    孙广东  2017.8.3 http://blog.csdn.NET/u010019717 在Unity 2017.1.0f3中引入了 SpriteAtlas 官方文档:  https://docs. ...

  6. cocos2d-x 3.1.1 学习笔记[2]Sprite 精灵

    Sprite应该是用到最多的一个类吧.无法想像一个游戏没有精灵将怎样进行愉快的玩耍. Sprite继承于Node 和 TextureProtocol. Sprite是一个2d的图像. Sprite能够 ...

  7. unity sprite怎么获取切割后的图

    学习了一段时间的unity,对里面的组件有一个大致的了解,但是具体操作来说还不是很熟悉,今天看了一片关于unity sprite怎么获取切割后的图的文章,感觉还不错. 假设有一张png/tga图集,导 ...

  8. NGUI中Button与原生2D精灵的混合使用

    一些废话 每一篇的首段都是这个“一些废话”,原因是我太能逼逼了,不逼逼一些废话我就觉得难受.这是我第四篇关于Unity的博文,前两篇还是去年写的,“从一点儿不会开始”系列,类似教程和学习笔记的博文,这 ...

  9. unity3d插件Daikon Forge GUI 中文教程3-基础控件Button和Sprite的使用

    2.2添加一个按钮Button 来看看特有的属性:Button Properties Data 显示的文本 Behavior 中的几个: Aoto Size 选中时就是按钮的背景会根据Data中的文本 ...

随机推荐

  1. handler机制的原理(转)

      Handler概述   andriod提供了Handler 和 Looper 来满足线程间的通信.Handler先进先出原则.Looper类用来管理特定线程内对象之间的消息交换(MessageEx ...

  2. mybatis hellworld

    用maven来进行搭建项目的~~   1. 搭建环境 pom.xml <project xmlns="http://maven.apache.org/POM/4.0.0" x ...

  3. 初学Node(六)搭建一个简单的服务器

    搭建一个简单的服务器 通过下面的代码可以搭建一个简单的服务器: var http = require("http"); http.createServer(function(req ...

  4. ORA-00257归档日志写满的解决方法

    背景: 在前一篇博客中我们提到了如何启动或关闭oracle的归档(ARCHIVELOG)模式,在我成功设定数据库为归档模式以后, 第二天再次尝试连接数据库,报错:ORA-00257.在网上找到了一圈资 ...

  5. eclipse一直卡住,出现 “android sdk content loader 0%” 卡住的错误分析及解决方法

    分析:这种问题之前没有遇到过,也不知道什么原因,直接去网上查询,打开www.stackoverflow.com,输入要查询问题的关键词,我们输入 “android sdk content loader ...

  6. Android获取焦点所在控件

    View vFocus=getWindow().getDecorView().findFocus(); if(vFocus instanceof EditText) { ((EditText) vFo ...

  7. 【读书笔记】iOS-开发技巧-三种收起键盘的方法

    - (void)viewDidLoad { [super viewDidLoad]; // Do any additional setup after loading the view, typica ...

  8. 【原/转】iOS中非常强大的过滤器:NSPredicate

    在APPLE的官方Demo:UICatalog中实现UISearchBar模糊搜索功能是这么做的: - (void)viewDidLoad { [super viewDidLoad]; self.al ...

  9. 【原】xcode5&IOS7及以下版本免证书真机调试记录

    搞了有一段IOS开发了,之前一直在企业做,近阶段主要在公司做C++服务端开发,打算在空闲实现搞搞个人开发,为自己赚钱,IDP还没申请下来,所以先用此方法在越狱设备上先做一下app的免证书真机调试,先记 ...

  10. iOS:交换Button中图片与文字的左右位置

    titleEdgeInsets属性和 imageEdgeInsets属性只是在画这个button出来的时候用来调整image和label位置的属性,并不影响button本身的大小.它们只是image和 ...