Unity 图片分割将spirte保存在本地
生活随笔
收集整理的這篇文章主要介紹了
Unity 图片分割将spirte保存在本地
小編覺得挺不錯的,現(xiàn)在分享給大家,幫大家做個參考.
如果你拿到的是一張整圖,你想分割之后使用NGUI sprite來使用!? 下面就能解決的需求.
步驟:
1. 使用Unity自帶的spirte進行分割圖片
2. 使用代碼把分割出來的2DSpirte轉(zhuǎn)換成本地PNG圖片,再導入Unity使用atlas紋理O(∩_∩)O~
注意事項:
1.? 圖片切換成Advanced類型 Read/Write Enabled勾上,不然會拋出異常
直接上代碼:
[MenuItem("Tools/導出精靈")]static void SaveSprite() {//每一張貼圖類型Advanced下 Read/Write Enabled打上勾才能進行文件讀取string resourcesPath = @"Assets/Resources/";foreach (Object obj in Selection.objects){string selectionPath = AssetDatabase.GetAssetPath(obj);// 必須最上級是"Assets/Resources/"if (selectionPath.StartsWith(resourcesPath)){//獲取文件后罪名.pngstring 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 [] spriteList = Resources.LoadAll<Sprite>(loadPath);if(spriteList.Length > 0){//創(chuàng)建導出文件夾string outPath = Application.dataPath + "/outSprite/" + loadPath;System.IO.Directory.CreateDirectory(outPath);foreach (var sprite in spriteList){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("保存圖片完畢!" + outPath);}}}}原文:Unity 圖片分割將spirte保存在本地 - 盤子臉 - 博客園
總結(jié)
以上是生活随笔為你收集整理的Unity 图片分割将spirte保存在本地的全部內(nèi)容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: K2 BPM PaaS平台签约福耀玻璃:
- 下一篇: Xposed之开发Hook插件