static void ConvertSprite() { //EditorUtility.DisplayDialog("MyTool", "Do It in C# !", "OK", ""); //Object selobj = Selection.activeObject; string selectionPath = AssetDatabase.GetAssetPath(Selection.activeObject); if (!selectionPath.EndsWith(".plist")) { EditorUtility.DisplayDialog("Error", "Please select a plist file!", "OK", ""); return; } Debug.LogWarning("#PLisToSprites start:" + selectionPath); string fileContent = string.Empty; using (FileStream file = new FileStream(selectionPath, FileMode.Open)) { byte[] str = new byte[(int)file.Length]; file.Read(str, 0, str.Length); fileContent = GetUTF8String(str); Debug.Log(fileContent); file.Close(); file.Dispose(); } //去掉<!DOCTYPE>,不然异常 int delStart = fileContent.IndexOf("<!DOCTYPE"); int delEnd = fileContent.IndexOf("\n", delStart); fileContent = fileContent.Remove(delStart, delEnd - delStart); Debug.Log(fileContent); //解析文件 PList plist = new PList(); plist.LoadText(fileContent);//Load(selectionPath); TPAtlas at = new TPAtlas(); at.LoadX(plist); //重写meta string texPath = Path.GetDirectoryName(selectionPath) + "/" + at.realTextureFileName; Texture2D selTex = AssetDatabase.LoadAssetAtPath(texPath, typeof(Texture2D)) as Texture2D; Debug.Log("texture:" + texPath); Debug.Log("write texture:" + selTex.name + " size:" + selTex.texelSize); TextureImporter textureImporter = AssetImporter.GetAtPath(texPath) as TextureImporter; if (textureImporter.textureType != TextureImporterType.Sprite && textureImporter.textureType != TextureImporterType.Advanced) { EditorUtility.DisplayDialog("Error", "Texture'type must be sprite or Advanced!", "OK", ""); return; } if (textureImporter.spriteImportMode != SpriteImportMode.Multiple) { EditorUtility.DisplayDialog("Error", "spriteImportMode must be Multiple!", "OK", ""); return; } SpriteMetaData[] sheetMetas = new SpriteMetaData[at.sheets.Count]; for (int i = 0; i < at.sheets.Count; i++) { var frameData = at.sheets[i]; sheetMetas[i].alignment = 0; sheetMetas[i].border = new Vector4(0, 0, 0, 0); sheetMetas[i].name = frameData.name; sheetMetas[i].pivot = new Vector2(0.5f, 0.5f); sheetMetas[i].rect = new Rect(frameData.frame.x, at.size.y - frameData.frame.y - frameData.frame.height, frameData.frame.width, frameData.frame.height);//这里原点在左下角,y相反 //Debug.Log("do sprite:" + frameData.name); } //textureImporter.spriteImportMode = SpriteImportMode.Multiple; textureImporter.spritesheet = sheetMetas; //save textureImporter.textureType = TextureImporterType.Sprite; //bug? textureImporter.spriteImportMode = SpriteImportMode.Multiple; //不加这两句会导致无法保存meta AssetDatabase.ImportAsset(texPath, ImportAssetOptions.ForceUpdate); Debug.LogWarning("#PLisToSprites end:" + texPath); }
static void OutputSpriteWithPlist() { //getdic Object selobj = Selection.activeObject; string selectionPath = AssetDatabase.GetAssetPath(Selection.activeObject); if (!selectionPath.EndsWith(".plist") && !selectionPath.EndsWith(".txt")) { EditorUtility.DisplayDialog("Error", "Please select a plist file!", "OK", ""); return; } Debug.LogWarning("#PLisToSprites start:" + selectionPath); string fileContent = string.Empty; using (FileStream file = new FileStream(selectionPath, FileMode.Open)) { byte[] str = new byte[(int)file.Length]; file.Read(str, 0, str.Length); fileContent = GetUTF8String(str); Debug.Log(fileContent); file.Close(); file.Dispose(); } //去掉<!DOCTYPE>,不然异常 int delStart = fileContent.IndexOf("<!DOCTYPE"); if (delStart >= 0) { int delEnd = fileContent.IndexOf("\n", delStart); fileContent = fileContent.Remove(delStart, delEnd - delStart); } Debug.Log(fileContent); //解析文件 PList plist = new PList(); plist.LoadText(fileContent);//Load(selectionPath); TPAtlas at = new TPAtlas(); at.LoadX(plist); Dictionary <string, bool> rotatoDic = new Dictionary <string, bool>(); foreach (var item in at.sheets) { if (item.rotated) { rotatoDic[item.name] = true; } } //output string texPath = Path.GetDirectoryName(selectionPath) + "/" + at.realTextureFileName; Texture2D selTex = AssetDatabase.LoadAssetAtPath <Texture2D>(texPath); string rootPath = Path.GetDirectoryName(selectionPath) + "/" + Selection.activeObject.name; TextureImporter textureImporter = AssetImporter.GetAtPath(texPath) as TextureImporter; int i = 0; //EditorUtility.DisplayProgressBar("unpack sprites", "start", 0); foreach (var spmeta in textureImporter.spritesheet) { i++; //if (i > 10) //{ // break; //} bool rotate = false; if (rotatoDic.ContainsKey(spmeta.name)) { rotate = true; Debug.LogWarning("conv ret:" + spmeta.name); } EditorUtility.DisplayProgressBar("unpack sprites", spmeta.name, (float)i / (float)textureImporter.spritesheet.Length); string path = rootPath + "/" + spmeta.name; string selectionExt = System.IO.Path.GetExtension(spmeta.name); if (selectionExt != ".png" && selectionExt != ".PNG") { path += ".png"; } string subDir = Path.GetDirectoryName(path); if (!Directory.Exists(subDir)) { Directory.CreateDirectory(subDir); } Debug.Log("output :" + path); SavePriteToPNG_Meta(selTex, spmeta, path, rotate); } EditorUtility.ClearProgressBar(); AssetDatabase.Refresh(); }