Unity切割圖集轉(zhuǎn)換為多張圖片
本文實(shí)例為大家分享了Unity切割圖集轉(zhuǎn)換為多張圖片的具體代碼,供大家參考,具體內(nèi)容如下
這是網(wǎng)上看來(lái)的一個(gè)工具,用于Unity中將圖集切割為多張的格式后將這些sprite改為一張張圖片,就是切割速度太慢,圖集中的圖片較多的時(shí)候還會(huì)丟失一部分圖片,有時(shí)間本人會(huì)進(jìn)一步改善再修改這篇博客。
1.首先選中要切割的圖集,texture type 選為default,并勾選Advanced下的read/Write Enabled。
2.texture type改為sprite(2D and UI),Sprite mode 選為Multiple,apply一下。
3.點(diǎn)擊Sprite Editor切割圖片。
4.選中圖集右鍵然后選擇imageslicer選擇process to Sprites。
5.等待切割完成。
腳本如下:
using UnityEngine;
using System.Collections;
using UnityEditor;
using System.IO;
using System.Collections.Generic;
/// <summary>
/// 切割
/// </summary>
public static class ImageSlicer
{
[MenuItem("Assets/ImageSlicer/Process to Sprites")]
static void ProcessToSprite()
{
Texture2D image = Selection.activeObject as Texture2D;//獲取旋轉(zhuǎn)的對(duì)象
string rootPath = Path.GetDirectoryName(AssetDatabase.GetAssetPath(image));//獲取路徑名稱
string path = rootPath + "/" + image.name + ".PNG";//圖片路徑名稱
TextureImporter texImp = AssetImporter.GetAtPath(path) as TextureImporter;//獲取圖片入口
AssetDatabase.CreateFolder(rootPath, image.name);//創(chuàng)建文件夾
foreach (SpriteMetaData metaData in texImp.spritesheet)//遍歷小圖集
{
Texture2D myimage = new Texture2D((int)metaData.rect.width, (int)metaData.rect.height);
//abc_0:(x:2.00, y:400.00, width:103.00, height:112.00)
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));
}
//轉(zhuǎn)換紋理到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();
//AssetDatabase.CreateAsset(myimage, rootPath + "/" + image.name + "/" + metaData.name + ".PNG");
File.WriteAllBytes(rootPath + "/" + image.name + "/" + metaData.name + ".PNG", pngData);
// 刷新資源窗口界面
AssetDatabase.Refresh();
}
}
}
以上就是本文的全部?jī)?nèi)容,希望對(duì)大家的學(xué)習(xí)有所幫助,也希望大家多多支持腳本之家。
相關(guān)文章
C#簡(jiǎn)單實(shí)現(xiàn)子窗體向父窗體傳值的方法
這篇文章主要介紹了C#簡(jiǎn)單實(shí)現(xiàn)子窗體向父窗體傳值的方法,以實(shí)例形式較為詳細(xì)的分析了C#窗體間傳值的實(shí)現(xiàn)技巧,具有一定參考借鑒價(jià)值,需要的朋友可以參考下2015-09-09
C#中Foreach循環(huán)遍歷的本質(zhì)與枚舉器詳解
這篇文章主要給大家介紹了關(guān)于C#中Foreach循環(huán)遍歷本質(zhì)與枚舉器的相關(guān)資料,foreach循環(huán)用于列舉出集合中所有的元素,foreach語(yǔ)句中的表達(dá)式由關(guān)鍵字in隔開(kāi)的兩個(gè)項(xiàng)組成,本文通過(guò)示例代碼介紹的非常詳細(xì),需要的朋友可以參考下2021-08-08
VS?Code里使用Debugger?for?Unity插件調(diào)試的方法(2023最新版)
Debugger for Unity是一個(gè)非正式支持的,官方推薦的,應(yīng)用最廣的,Visual Studio Code上的Unity調(diào)試插件,這篇文章主要介紹了VS?Code里使用Debugger?for?Unity插件進(jìn)行調(diào)試(2023最新版),需要的朋友可以參考下2023-02-02
C#中通過(guò)反射將枚舉元素加載到ComboBo的實(shí)現(xiàn)方法
本文主要介紹了C#中通過(guò)反射將枚舉元素加載到ComboBo的實(shí)現(xiàn)方法,文中通過(guò)示例代碼介紹的非常詳細(xì),具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2021-09-09

