日韩性视频-久久久蜜桃-www中文字幕-在线中文字幕av-亚洲欧美一区二区三区四区-撸久久-香蕉视频一区-久久无码精品丰满人妻-国产高潮av-激情福利社-日韩av网址大全-国产精品久久999-日本五十路在线-性欧美在线-久久99精品波多结衣一区-男女午夜免费视频-黑人极品ⅴideos精品欧美棵-人人妻人人澡人人爽精品欧美一区-日韩一区在线看-欧美a级在线免费观看

歡迎訪問 生活随笔!

生活随笔

當前位置: 首頁 > 编程资源 > 编程问答 >内容正文

编程问答

Unity切割Sprite图集转换为多张图片

發布時間:2024/1/18 编程问答 45 豆豆
生活随笔 收集整理的這篇文章主要介紹了 Unity切割Sprite图集转换为多张图片 小編覺得挺不錯的,現在分享給大家,幫大家做個參考.

?為了可以使用Unity自帶的精靈切割,要將紋理類型改成"Sprite","Sprite Mode"改成"Multiple","Format"改成"Truecolor",點擊"Apply"按鈕進行應用。

接下來,因為要對圖片進行讀寫操作,要更改圖片的屬性才能進行,否則會提示如下:
UnityException: Texture 'testUI' is not readable, the texture memory can not be accessed from scripts. You can make the texture readable in the Texture Import Settings.
將圖片紋理類型更改為"Advanced",將"Read/Write Enabled"屬性進行打勾,如下圖所示:

?

下面代碼:

[MenuItem("Tools/export select sprite", false, 51)]public static void exportSprite(){Texture2D image = Selection.activeObject as Texture2D;//獲取對象if (image != null){string tDirName = AssetDatabase.GetAssetPath(image).Replace(image.name + ".png", image.name + "") + @"/";tDirName = tDirName.Replace(@"Assets/", @"TexturePacker/"); //輸出路徑string path = Path.GetDirectoryName(AssetDatabase.GetAssetPath(image)) + "/" + image.name + ".png";//圖片路徑名稱if (Directory.Exists(tDirName) == false){Directory.CreateDirectory(tDirName);}TextureImporter texImp = AssetImporter.GetAtPath(path) as TextureImporter;foreach (SpriteMetaData metaData in texImp.spritesheet) //遍歷小圖集{Texture2D myimage = new Texture2D((int)metaData.rect.width, (int)metaData.rect.height);for (int y = (int)metaData.rect.y; y < metaData.rect.y + metaData.rect.height; y++)//Y軸像素{for (int x = (int)metaData.rect.x; x < metaData.rect.x + metaData.rect.width; x++)myimage.SetPixel(x - (int)metaData.rect.x, y - (int)metaData.rect.y, image.GetPixel(x, y));}//轉換紋理到EncodeToPNG兼容格式if (myimage.format != TextureFormat.ARGB32 && myimage.format != TextureFormat.RGB24){Texture2D newTexture = new Texture2D(myimage.width, myimage.height);newTexture.SetPixels(myimage.GetPixels(0), 0);myimage = newTexture;}var pngData = myimage.EncodeToPNG();File.WriteAllBytes(tDirName + "/" + metaData.name + ".png", pngData);}Debug.LogError("export " + image.name + " sprite finished");}}

?

?

總結

以上是生活随笔為你收集整理的Unity切割Sprite图集转换为多张图片的全部內容,希望文章能夠幫你解決所遇到的問題。

如果覺得生活随笔網站內容還不錯,歡迎將生活随笔推薦給好友。