using UnityEditor; using UnityEngine; using System.Diagnostics; using System.IO; using System.Text; namespace Editor { public static class GitPullEditor { static bool isRunning = false; // 菜单项 [MenuItem("Tools/Git/拉取最新版本", false, 100)] public static void PullLatest() { if (isRunning) { EditorUtility.DisplayDialog("Git 拉取", "正在拉取中,请稍候...", "确定"); return; } isRunning = true; var projectRoot = Directory.GetParent(Application.dataPath).FullName; var psi = new ProcessStartInfo { FileName = "git", Arguments = "pull", WorkingDirectory = projectRoot, RedirectStandardOutput = true, RedirectStandardError = true, UseShellExecute = false, CreateNoWindow = true, StandardOutputEncoding = Encoding.UTF8, StandardErrorEncoding = Encoding.UTF8 }; var proc = new Process { StartInfo = psi, EnableRaisingEvents = true }; var output = new StringBuilder(); proc.OutputDataReceived += (s, e) => { if (!string.IsNullOrEmpty(e.Data)) { output.AppendLine(e.Data); UnityEngine.Debug.Log("[git] " + e.Data); } }; proc.ErrorDataReceived += (s, e) => { if (!string.IsNullOrEmpty(e.Data)) { output.AppendLine(e.Data); UnityEngine.Debug.LogError("[git] " + e.Data); } }; try { proc.Start(); proc.BeginOutputReadLine(); proc.BeginErrorReadLine(); // 等待完成(超时 10 分钟) proc.WaitForExit(600000); isRunning = false; if (proc.ExitCode == 0) { EditorUtility.DisplayDialog("Git 拉取", "拉取完成。请查看控制台获取详细信息。", "确定"); } else { EditorUtility.DisplayDialog("Git 拉取失败", $"ExitCode: {proc.ExitCode}\n请查看控制台输出。", "确定"); } } catch (System.Exception ex) { isRunning = false; UnityEngine.Debug.LogException(ex); EditorUtility.DisplayDialog("Git 拉取异常", ex.Message, "确定"); } } // 菜单验证,拉取中禁用该菜单项 [MenuItem("Tools/Git/拉取最新版本", true)] public static bool ValidatePullLatest() { return !isRunning; } } }