现在的项目使用Tiled软件编辑游戏地图,并通过第三方插件Tiled2Unity将地图文件解析到Unity,插件自动转换为相应的碰撞信息。
但是地图不可能只有一个,也就是说会编很多很多地图文件,但是如果因为疏忽想修改地图文件的内容,比如图层信息、参数或者碰撞等,就要把所有的地图文件全部修改一遍,一个个导入到unity特别的费时间,那么有没有一个工具批量导入到unity呢?这时候我想到了命令行工具,Tiled2Unity也支持命令行工具。
先看看最终效果:
1、先将调用命令行封装成一个函数,command则是文件名,如svn则会在环境变量PATH中的所有路径下查找名称为svn的文件文件进行执行,同样也可以写成完整路径如:F:\AA\bb.exe,argument则是参数
public static void ProcessCommand(string command,string argument){
System.Diagnostics.ProcessStartInfo info = new System.Diagnostics.ProcessStartInfo(command);
info.Arguments = argument;
info.CreateNoWindow = false;
info.ErrorDialog = true;
info.UseShellExecute = true;
if(info.UseShellExecute){
info.RedirectStandardOutput = false;
info.RedirectStandardError = false;
info.RedirectStandardInput = false;
} else{
info.RedirectStandardOutput = true;
info.RedirectStandardError = true;
info.RedirectStandardInput = true;
info.StandardOutputEncoding = System.Text.UTF8Encoding.UTF8;
info.StandardErrorEncoding = System.Text.UTF8Encoding.UTF8;
}
System.Diagnostics.Process process = System.Diagnostics.Process.Start(info);
if(!info.UseShellExecute){
Debug.Log(process.StandardOutput);
Debug.Log(process.StandardError);
}
process.WaitForExit();
process.Close();
}
2、调用Tiled2Unity软件,并传入参数,则自动打开Tiled2Unity软件进行导出,并发送到unity
ReimportMap("Tile文件TMX路径");//换成TMX路径
static void ReimportMap(string path){
string exportPath = Application.dataPath + "/Tiled2Unity";
string arg = "-a -s=0.01 " + path + " " + exportPath;
ProcessCommand ("Tiled2Unity安装路径/Tiled2Unity.exe", arg);//换成Tiled2Unity的安装路径
AssetDatabase.Refresh ();
}