【实用工具】提取其他游戏资源时图片被打包成图集后分割方法【转】
代码1:
using UnityEngine;
using UnityEditor;
using System.Collections;
public class SpriteSheetSelect
{
public static Texture2D imageCopy;
[MenuItem("Assets/Sprite Sheet Packer/Select to Sprites")]
static void SpriteImageSelect()
{
imageCopy = Selection.activeObject as Texture2D;
}
}
代码2:
using UnityEngine;
using UnityEditor;
using System.Collections.Generic;
using System.IO;
public static class SpriteSheetPackerImport
{
[MenuItem("Assets/Sprite Sheet Packer/Process to Sprites")]
static void ProcessToSprite()
{
Texture2D image = Selection.activeObject as Texture2D;//获取选择的对象
Texture2D imageCopy = SpriteSheetSelect.imageCopy;
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);//创建文件夹
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, imageCopy.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();
//AssetDatabase.CreateAsset(myimage, rootPath + "/" + image.name + "/" + metaData.name + ".PNG");
File.WriteAllBytes(rootPath + "/" + image.name + "/" + metaData.name + ".PNG", pngData);
}
}
}
使用方法:
1:需要切割的图片复制多一张,副本图片TextureType选择Advanced,把下图的勾打上
然后在把TextureType设置成Cursor
2:选择另外一张图片,TextureType设置成Sprite-》Sprite Mode设置成Multiple-》点击Sprite Editor按钮
点击Sprite Editor按钮后出现下图
点击左上角的Slice在点击弹出窗口的Slice,会自动分割图片,白框框住的就是后面切割的图片,如有某个不正确的请手动调整
3:关闭并Apply应用,在资源哪点击TextureType设置成Cursor的图片
4:点击
5:在资源选择
6:点击第四步图中的Process to Sprites
好了,在同目录下找文件吧