为一张透明的图片进行切割
可以使用 ShoeBox 的
Extract Sprites 功能,将图片拖动到 Extract Sprites 功能图标上,就会自动切割好图片,如下图所示:
点击
Settings 可以进行详细设置,这里将 Clusters Merge Sub Items 设为 false,不合并子项,将 File Name 改成 slice##.png,点击
Apply 即可。然后按
Save 就可以导出切割好的图片。如下图所示:
生成包括一个文本文档,记录每张切割的子图坐标。在 Unity 的 Editor 目录下,添加如下脚本:
C# Code
1
2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 |
using System.Collections.Generic;
using System.Reflection; using UnityEditor; using UnityEngine; using System.IO; /// <summary> /// 处理ShoeBox切割的图片到Sprites /// </summary> public class ShoeBoxExtractSpritesImport { [MenuItem( "Assets/ShoeBox/Process to Sprites")] static void ProcessExtractSpritesToSprites() { TextAsset txt = Selection.activeObject as TextAsset; if (txt == null) { Debug.LogError( "Must be .txt file!"); return; } string assetPath = AssetDatabase.GetAssetPath(txt); if (Path.GetExtension(assetPath) != ".txt") { Debug.LogError( "Must be .txt file!"); return; } string rootPath = Path.GetDirectoryName(assetPath); string path = rootPath + "/" + Path.GetFileNameWithoutExtension(assetPath); TextureImporter texImp = AssetImporter.GetAtPath(path) as TextureImporter; if (texImp == null) { return; } object[] args = new object[ 2] { 0, 0 }; MethodInfo mi = typeof(TextureImporter).GetMethod( "GetWidthAndHeight", BindingFlags.NonPublic | BindingFlags.Instance); mi.Invoke(texImp, args); var height = ( int)args[ 1]; List<SpriteMetaData> sprites = GetSpriteMetaDatas(rootPath, txt.text, height); texImp.spritesheet = sprites.ToArray(); texImp.textureType = TextureImporterType.Sprite; texImp.spriteImportMode = SpriteImportMode.Multiple; AssetDatabase.DeleteAsset(assetPath); AssetDatabase.ImportAsset(path); } static List<SpriteMetaData> GetSpriteMetaDatas( string path, string text, int totalHeight) { List<SpriteMetaData> sprites = new List<SpriteMetaData>(); var sliceName = "slice"; var arrayTxt = text.Split( ','); var pngIndex = 0; foreach (var txt in arrayTxt) { var arrayPoint = txt.Split( ' '); int x = int.Parse(arrayPoint[arrayPoint.Length - 2]); int y = int.Parse(arrayPoint[arrayPoint.Length - 1].TrimEnd( '.')); pngIndex++; var pngName = sliceName + pngIndex.ToString().PadLeft( 2, '0') + ".png"; var pngPath = path + "/" + pngName; TextureImporter importer = AssetImporter.GetAtPath(pngPath) as TextureImporter; if (importer != null) { object[] args = new object[ 2] { 0, 0 }; MethodInfo mi = typeof(TextureImporter).GetMethod( "GetWidthAndHeight", BindingFlags.NonPublic | BindingFlags.Instance); mi.Invoke(importer, args); var width = ( int)args[ 0]; var height = ( int)args[ 1]; SpriteMetaData smd = new SpriteMetaData(); smd.rect = new Rect(x, totalHeight - y - height, width, height); smd.alignment = 0; smd.name = pngName.Remove(pngName.Length - 4); smd.pivot = new Vector2( 0.5f, 0.5f); sprites.Add(smd); AssetDatabase.DeleteAsset(pngPath); } } return sprites; } } |
在 Project 视图里,选中这个文本文档,右键选择功能 ShoeBox→Process to Sprites,即可自动完成导入成 Unity 的 Sprites 格式,如下图所示:
Unity 的 Sprite Editor 也自带切割图片的功能,但是当遇到比较复杂的图集时,切割的比较混乱,用 ShoeBox 正确率比较大一点。
ShoeBox 地址:
http://renderhjs.net/shoebox/