Unity ShoeBox Extract Sprites 到 Unity 格式
为一张透明的图片进行切割
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; } } |
郑重声明:本站内容如果来自互联网及其他传播媒体,其版权均属原媒体及文章作者所有。转载目的在于传递更多信息及用于网络分享,并不代表本站赞同其观点和对其真实性负责,也不构成任何其他建议。