RunExeAndWait.cs 2.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596
  1. using UnityEngine;
  2. using UnityEngine.UI;
  3. using System.Collections;
  4. using System.Diagnostics;
  5. using System;
  6. public class RunExeAndWait : MonoBehaviour
  7. {
  8. private Action _finishCallback; // 用来保存驱动器传进来的回调
  9. [Header("UI 弹窗")]
  10. public GameObject confirmPanel; // Canvas 上的弹窗 Panel
  11. public Button btnConfirm; // 弹窗上的“确定”按钮
  12. public Button btnCancel; // 弹窗上的“取消”按钮
  13. [Header("EXE 配置")]
  14. string exePath = System.IO.Path.Combine(Application.streamingAssetsPath, "BIU", "EquipmentWorkingPrinciple.exe");
  15. private void Start()
  16. {
  17. //ShowConfirmAndStartExe();
  18. }
  19. // ---------------------------
  20. // 外部流程调用
  21. public void ShowConfirmAndStartExe(Action finishCallback)
  22. {
  23. UnityEngine.Debug.LogError("<color=yellow>弹窗逻辑被触发</color>");
  24. _finishCallback = finishCallback;
  25. // 1. 弹窗出现,流程暂停
  26. confirmPanel.SetActive(true);
  27. // 2. 按钮事件绑定
  28. btnConfirm.onClick.RemoveAllListeners();
  29. btnCancel.onClick.RemoveAllListeners();
  30. btnConfirm.onClick.AddListener(OnClickConfirm);
  31. btnCancel.onClick.AddListener(OnClickCancel);
  32. }
  33. // ---------------------------
  34. // 点击“确定”
  35. private void OnClickConfirm()
  36. {
  37. // 启动 exe 并等待完成
  38. StartCoroutine(StartExeAndWaitCoroutine());
  39. confirmPanel.SetActive(false); // 关闭弹窗
  40. }
  41. // 点击“取消”
  42. public void OnClickCancel()
  43. {
  44. confirmPanel.SetActive(false); // 关闭弹窗
  45. _finishCallback?.Invoke(); // 用户取消,直接继续流程
  46. // 流程继续下一步
  47. NextStep();
  48. }
  49. // ---------------------------
  50. private IEnumerator StartExeAndWaitCoroutine()
  51. {
  52. if (!System.IO.File.Exists(exePath))
  53. {
  54. UnityEngine.Debug.LogError("EXE 文件不存在: " + exePath);
  55. yield break;
  56. }
  57. ProcessStartInfo info = new ProcessStartInfo();
  58. info.FileName = exePath;
  59. info.UseShellExecute = false;
  60. info.CreateNoWindow = false;
  61. Process p = Process.Start(info);
  62. // 等待 exe 关闭,不堵主线程
  63. while (!p.HasExited)
  64. {
  65. yield return null;
  66. }
  67. _finishCallback?.Invoke(); // EXE 结束,回调驱动继续流程
  68. }
  69. // ---------------------------
  70. // 流程下一步逻辑
  71. private void NextStep()
  72. {
  73. UnityEngine.Debug.Log("流程继续下一步");
  74. // TODO: 放你流程继续的代码
  75. }
  76. }