unity TexturePacker自动打图集编辑器
目前仅支持windows,手边没有mac没法做。实现方式是用C#调用Cmd用命令行进行打图集,如果选择的输出目录是unity内部则自动创建材质球和AtlasPrefab。并且自动设置图片格式。目前编辑器功能比较少够我在我的项目里面用了,代码也比较简单可自行添加,如有不懂可以留言交流。
使用前需要安装TexturePacker,并且需要添加到环境变量,可以在TexturePacker File >Install command line client中查看。
参数介绍
XmlPath:本工具会保存上一次用的配置,记录到本地的一个xml里路径可以在代码里面改。
CmdPath:填写你的cmd路径即可,环境变量里有的话可以直接写cmd.exe。
ResourcesPath:存放需要打成图集的碎图文件夹。
OutPutPath:图集的输出路径,如果是unity内部路径则可以直接产生NGUI用的材质球和Atlas Prefab。
TexturePacker Config:TexturePacker里的几个设置项
Unity Import Config:图片导入到Unity里面的几个设置项
Labels:图片在Unity里面的标签可以为空
OutputName:输出的名字不能为空必须配置
SaveConfig:保存当前的配置到xml
编辑器中列出的几个设置项是我在手动打图集过程中,需要修改的。
贴出运行cmd的代码吧
private void RunTexturePacker()
{
string command = "/k " + _resourcesRootPath +
"&TexturePacker " + _data.ResourcesPath +
" --format unity --opt " + _data.TextureFormat_TP.ToString()+
" --size-constraints " +_data.SizeConstraints_TP+
roatation + ditherType +
" --max-width " + _data.MaxSize_Width_TP+
" --max-height " + _data.MaxSize_Heitht_TP+
" --sheet \""+ _data.OutPutPath+"/"+_outputName+".png\" " +
"--data \""+_data.OutPutPath+"/"+_outputName+".txt\""+
"&exit";
processCommand (_data.CmdPath, command);
}
private static void processCommand(string cmdPath, string argument)
{
ProcessStartInfo start = new ProcessStartInfo(cmdPath);
start.Arguments = argument;
start.CreateNoWindow = true; //是否显示cmd窗口
start.ErrorDialog = true;
start.UseShellExecute = false; //是否使用shell
if(start.UseShellExecute)
{
start.RedirectStandardOutput = false;
start.RedirectStandardError = false;
start.RedirectStandardInput = false;
}
else
{
start.RedirectStandardOutput = true;
start.RedirectStandardError = true;
start.RedirectStandardInput = true;
start.StandardOutputEncoding = System.Text.UTF8Encoding.Default;
start.StandardErrorEncoding = System.Text.UTF8Encoding.Default;
}
Process p = Process.Start(start);
//process.StandardInput.WriteLine(argument) //不知为何不好使
if(!start.UseShellExecute)
{
UnityEngine.Debug.Log(start.StandardOutputEncoding);
UnityEngine.Debug.Log(p.StandardOutput.ReadToEnd ());
string error = p.StandardError.ReadToEnd ();
if (string.IsNullOrEmpty (error) == false)
{
UnityEngine.Debug.LogError(error);
}
}
p.WaitForExit();
p.Close();
}
需要注意的点就是命令前面需要加一个/k 也不知道为啥,然后如果用process.StandardInput.WriteLine(argument)这种方式写入命令,则会报错不是内部或外部命令,如有知道的请提示我下,谢谢了。
下载地址 https://download.csdn.net/download/daimou123/11287082
完毕。