| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596 |
- 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("<color=yellow>弹窗逻辑被触发</color>");
- _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: 放你流程继续的代码
- }
- }
|