com.soviby.unity.ui.ugui-to.../Assets/Editor/Helper/CommandHelper.cs

55 lines
2.1 KiB
C#
Raw Normal View History

2024-10-08 15:19:05 +00:00

using System.Collections.Generic;
using UnityEngine;
using System.IO;
using Newtonsoft.Json;
namespace UguiToolkit.Editor
{
public static class CommandHelper
{
public static RotScaleJsonData CalcRotScale(in string srcImgDirPath, in string targetImgDirPath, Dictionary<string, List<rotScaleInfoItem>> rotScaleInfos)
{
var rotScaleInfoFilePath = Path.GetFullPath(EditorConst.RotScaleInfoFilePath);
var rotScaleInfoToolFilePath = Path.GetFullPath(EditorConst.RotScaleInfoToolFilePath);
if (File.Exists(rotScaleInfoFilePath)) File.Delete(rotScaleInfoFilePath);
RunCmd(string.Format("{0} -src {1} -target {2} -distance_difference 0.06 -output_path {3}",
rotScaleInfoToolFilePath,
Path.GetFullPath(srcImgDirPath),
Path.GetFullPath(targetImgDirPath),
Path.GetFullPath(rotScaleInfoFilePath)));
if (!File.Exists(rotScaleInfoFilePath))
{
Debug.LogError($"[E] 文件{rotScaleInfoFilePath} 未能正确获得");
return null;
}
using (StreamReader reader = File.OpenText(rotScaleInfoFilePath))
{
var jsonData = reader.ReadToEnd();
RotScaleJsonData rotScaleJsonData = JsonConvert.DeserializeObject<RotScaleJsonData>(jsonData);
return rotScaleJsonData;
}
}
// 执行 cmd 命令
public static void RunCmd(in string cmd)
{
var p = new System.Diagnostics.Process();
p.StartInfo.FileName = "cmd.exe";
p.StartInfo.Arguments = "/c " + cmd; // 使用 /c 参数执行命令并关闭 cmd 窗口
p.StartInfo.UseShellExecute = false;
p.StartInfo.RedirectStandardOutput = true;
p.StartInfo.RedirectStandardError = true;
p.Start();
var output = p.StandardOutput.ReadToEnd();
var error = p.StandardError.ReadToEnd();
p.WaitForExit();
Debug.Log("cmd output : " + output);
Debug.Log("cmd error : " + error);
}
}
}