unity把切好的圖集匯出
阿新 • • 發佈:2018-11-30
以前的部落格有介紹怎麼切割圖片
https://blog.csdn.net/qq_42661974/article/details/84258719
首先建立一個指令碼 放在Editor資料夾中
using UnityEngine; using UnityEditor; public class TestSaveSprite { [MenuItem("Tools/匯出精靈")] static void SaveSprite() { 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; } // 從路徑"Assets/Resources/UI/testUI.png"得到路徑"UI/testUI" 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("SaveSprite to " + outPath); } } } Debug.Log("SaveSprite Finished"); } }
指令碼寫好後會發現unity工具欄出現一個tools工具
要匯出的圖集必須在Resources資料夾下
選擇圖集後點擊匯出精靈
匯出成功後會列印路徑,然後按照路徑找出匯出成功的資源
匯出成功