Unity實(shí)現(xiàn)切割圖集工具
本文實(shí)例為大家分享了Unity實(shí)現(xiàn)切割圖集工具的具體代碼,供大家參考,具體內(nèi)容如下
操作步驟
先將腳本拖入Editor
1.選中要切割的圖片,texture type 選為default,并勾選Advanced下的read/Write Enabled

2.texture type改為sprite(2D and UI),Sprite mode 選為Multiple,apply一下

3.Sprite Editor 先選其他的切一下,在選第一個(gè)切一下,切割成小圖,apply

4.選中圖集右鍵,imageslicer,process to Sprites

5.等待切割完成后就可以在同級(jí)目錄的同名文件夾下使用了

使用時(shí)要把小圖Type改為sprite(2D and UI),也可以更改名字
腳本如下
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#實(shí)現(xiàn)判斷當(dāng)前操作用戶管理角色的方法
這篇文章主要介紹了C#實(shí)現(xiàn)判斷當(dāng)前操作用戶管理角色的方法,涉及C#針對(duì)系統(tǒng)用戶判斷的相關(guān)技巧,具有一定參考借鑒價(jià)值,需要的朋友可以參考下2015-08-08
c# AcceptEx與完成端口(IOCP)結(jié)合的示例
這篇文章主要介紹了c# AcceptEx與完成端口(IOCP)結(jié)合的示例,幫助大家更好的理解和學(xué)習(xí)使用c#,感興趣的朋友可以了解下2021-03-03
C#實(shí)現(xiàn)多線程的幾種方式常用場(chǎng)景分析
多線程是C#中一個(gè)重要的概念,多線程指的是在同一進(jìn)程中同時(shí)運(yùn)行多個(gè)線程的機(jī)制,多線程適用于需要提高系統(tǒng)并發(fā)性、吞吐量和響應(yīng)速度的場(chǎng)景,可以充分利用多核處理器和系統(tǒng)資源,提高應(yīng)用程序的性能和效率,這篇文章主要介紹了C#實(shí)現(xiàn)多線程的幾種方式,需要的朋友可以參考下2024-05-05
Unity Shader實(shí)現(xiàn)描邊OutLine效果
這篇文章主要為大家詳細(xì)介紹了Unity Shader實(shí)現(xiàn)描邊OutLine效果,文中示例代碼介紹的非常詳細(xì),具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2020-01-01
C#中Arraylist的sort函數(shù)用法實(shí)例分析
這篇文章主要介紹了C#中Arraylist的sort函數(shù)用法,較為詳細(xì)的分析了ArrayList的sort函數(shù)的功能、定義及具體使用技巧,具有一定參考借鑒價(jià)值,需要的朋友可以參考下2015-10-10

