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í)有所幫助,也希望大家多多支持腳本之家。
C#創(chuàng)建數(shù)據(jù)庫(kù)及附加數(shù)據(jù)庫(kù)的操作方法
Unity shader實(shí)現(xiàn)移動(dòng)端模擬深度水效果

