Unity 工具 之 图集根据切割为一张张单张图片的简单封装(Unity 自带 SpritePackage 打包图集的性能优化)
目录
二、Unity性能优化之图集打包 - 使用Unity自带的打包图集功能
一、简单介绍
Unity 工具类,自己整理的一些游戏开发可能用到的模块,单独独立使用,方便游戏开发。
本节介绍,如何不修改原始脚本模板的情况下,添加自己需要的命名空间Namespace的方法,这里简单说明,如果你有更好的方法,欢迎留言交流。
二、实现原理
1、Selection.activeObject 获取当前选中的图片集合
2、AssetImporter.spritesheet 取得该图集的分割小块图信息
3、Texture2D.SetPixel/ Texture2D.GetPixel 读写图片信息
4、File.WriteAllBytes 进行图片保存
三、注意事项
1、图片勾选读写权限
2、图片转为 sprite 精灵 Multiple,并有进行对应的分割
四、实现步骤
1、打开Unity,新建空工程
2、在工程中新建一个 Editor 文件夹,添加图片切割脚本,实现对选择的图片进行切割为单张图片的代码
3、导入一张图片,勾选读写权限
4、把图片类型,设置为 Sprite,Mode 为 Multiple
4、点击对应图片的 Sprite Editor,进行图片分割处理,可根据需要自行分割
5、 若果上面一步需要 2D Sprite 工具,若没有,可以 在 Package Manager 中下载安装
6、这时选中图片,右键 ImageSlicer - Process to Sprites,开始切割为单张图
7、切割后的结果
五、关键代码
using UnityEngine;
using System.Collections;
using UnityEditor;
using System.IO;
using System.Collections.Generic;
namespace XANEditor
{
public static class ImageSlicerTool
{
[MenuItem("Assets/ImageSlicer/Process to Sprites")]
static void ProcessToSprite()
{
Texture2D image = Selection.activeObject as Texture2D;//获取选择的对象
string rootPath = Path.GetDirectoryName(AssetDatabase.GetAssetPath(image));//获取路径名称
string fileExtension = Path.GetExtension(AssetDatabase.GetAssetPath(image));//获取路径名称后缀名(扩展名)
string path = rootPath + "/" + image.name + fileExtension;//图片路径名称
TextureImporter texImp = AssetImporter.GetAtPath(path) as TextureImporter;//获取图片入口
if (texImp.spritesheet.Length<=0)
{
Debug.LogError("没有精灵图集,无法切割,请把图片转为 sprite - Multiple ,并且适当分割下精灵图");
return;
}
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, 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(rootPath + "/" + image.name + "/" + metaData.name + fileExtension, pngData);
// 刷新资源窗口界面
AssetDatabase.Refresh();
}
}
}
}
附加
一、一些错误整理
1、没有读写权限的报错
UnityException: Texture 'Test' is not readable, the texture memory can not be accessed from scripts. You can make the texture readable in the Texture Import Settings.
2、选中不是图片的报错
ArgumentException: Invalid path
二、Unity性能优化之图集打包 - 使用Unity自带的打包图集功能
基于Unity3D的图集打包
Unity3D的UGUI图集打包采取自动打包的方式,旨在让开发者彻底模糊图集的概念,无须关注图集是如何打包的,从而让开发者将更多的精力放在逻辑开发上。步骤如下:
1. 首先创建3个Image组件分别显示3张不同的图片,此时还没打包图集,可以看出Batches为3
2. 设置图集打包开关,Edit\rightarrowProject Settings\rightarrowEditor,如下图
3. 为每张图片设置图集名
4. 点击Window\rightarrow2D\rightarrowSpritePacker ,即可看到打包好的图集
5. 再次运行,Batches变为1
打包后的图集Unity3D自动帮我们存至Libary/AtlasCache目录下,开发者无须对其进行特别处理。
TexturePacker
TexturePacker是通用的图集打包工具,支持多种平台的图集打包。由于具体用法网上介绍很多,上手不难,这里不再赘述。
官网:https://www.codeandweb.com/texturepacker
参考文献:
http://www.xuanyusong.com/archives/3304
https://blog.csdn.net/weixin_39706943/article/details/80522010