Sprite Editor 图集切片精灵

切图需求

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

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

切图效果

imageimage

image

步骤

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

image

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

imageimage

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

image

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

imageimage

插件代码

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 == 0)
                {
                    continue;
                }

                // 得到导出路径
                string loadPath = selectionPath.Remove(selectionPath.Length - selectionExt.Length);
                loadPath = loadPath.Substring(resourcesPath.Length);

                // 加载此文件下的所有资源
                Sprite[] sprites = Resources.LoadAll<Sprite>(loadPath);
                if (sprites.Length > 0)
                {
                    // 创建导出文件夹
                    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

image

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

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:259)
TestSaveSprite.SaveSprite () (at Assets/Editor/TestSaveSprite.cs:39)

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

image

 

说明

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

Unity的Sprite Multiple功能是用来处理多个片在一个图集中的情况的。在使用这个功能之前,你需要准备一个包含多个小的大图集。然后,你可以通过以下步骤来拆分这个图集。 首先,打开Unity编辑器,并将大图集导入到项目中。将图集拖拽到场景中的一个空的游戏对象上,创建一个Sprite Renderer组件。然后,将Sprite Mode设置为Multiple,这样unity就会把图集视为一个多精灵。 接下来,点击Sprite Editor按钮,这会打开Sprite Editor窗口。在窗口中,你会看到图集中所有小的预览。你可以使用鼠标选择并拖拽来选择一个小,并在预览面板中预览选择的小。 在Sprite Editor窗口的左侧,你可以调整小的位置和大小,以确保它们正确地对应着图集中的位置。你还可以裁剪小来确保只显示你想要的部分。你可以使用切分工具来裁剪小,也可以使用设置工具来调整其精确位置和大小。 一旦你完成了所有的拆分和调整,点击Apply按钮来应用更改。此时,unity会自动创建一个Sprite Asset,它会将图集中的每个小作为一个独立的精灵。 现在,你可以在Unity中使用这些独立的精灵了。你可以将它们分别用于不同的游戏对象,也可以使用Sprite Renderer组件的Sprite属性来动态更换精灵。 总的来说,Unity的Sprite Multiple功能非常方便,它使得管理和使用图集中的多个小变得简单而高效。无论是制作2D游戏还是创建用户界面,都可以通过这个功能来提高工作效率。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值