using UnityEngine; using UnityEngine.UI; using System.Collections; using System.Diagnostics; using System; public class RunExeAndWait : MonoBehaviour { private Action _finishCallback; // 用来保存驱动器传进来的回调 [Header("UI 弹窗")] public GameObject confirmPanel; // Canvas 上的弹窗 Panel public Button btnConfirm; // 弹窗上的“确定”按钮 public Button btnCancel; // 弹窗上的“取消”按钮 [Header("EXE 配置")] string exePath = System.IO.Path.Combine(Application.streamingAssetsPath, "BIU", "EquipmentWorkingPrinciple.exe"); private void Start() { //ShowConfirmAndStartExe(); } // --------------------------- // 外部流程调用 public void ShowConfirmAndStartExe(Action finishCallback) { UnityEngine.Debug.LogError("弹窗逻辑被触发"); _finishCallback = finishCallback; // 1. 弹窗出现,流程暂停 confirmPanel.SetActive(true); // 2. 按钮事件绑定 btnConfirm.onClick.RemoveAllListeners(); btnCancel.onClick.RemoveAllListeners(); btnConfirm.onClick.AddListener(OnClickConfirm); btnCancel.onClick.AddListener(OnClickCancel); } // --------------------------- // 点击“确定” private void OnClickConfirm() { // 启动 exe 并等待完成 StartCoroutine(StartExeAndWaitCoroutine()); confirmPanel.SetActive(false); // 关闭弹窗 } // 点击“取消” public void OnClickCancel() { confirmPanel.SetActive(false); // 关闭弹窗 _finishCallback?.Invoke(); // 用户取消,直接继续流程 // 流程继续下一步 NextStep(); } // --------------------------- private IEnumerator StartExeAndWaitCoroutine() { if (!System.IO.File.Exists(exePath)) { UnityEngine.Debug.LogError("EXE 文件不存在: " + exePath); yield break; } ProcessStartInfo info = new ProcessStartInfo(); info.FileName = exePath; info.UseShellExecute = false; info.CreateNoWindow = false; Process p = Process.Start(info); // 等待 exe 关闭,不堵主线程 while (!p.HasExited) { yield return null; } _finishCallback?.Invoke(); // EXE 结束,回调驱动继续流程 } // --------------------------- // 流程下一步逻辑 private void NextStep() { UnityEngine.Debug.Log("流程继续下一步"); // TODO: 放你流程继续的代码 } }