Unity 启动exe 并且监听exe的关闭

将需要启动的exe放置再streamingAssets文件夹下

using ActivationCode;
using System;
using System.Collections;
using System.Collections.Generic;
using System.Diagnostics;
using System.IO;
using System.Threading.Tasks;
using UnityEngine;

public class test : MonoBehaviour
{
```private Process myProcess;
    string fileName = Path.Combine(Application.streamingAssetsPath, "LaoFu/ElderlyYaChuang.exe");

    /*这里有一个特殊的要求,就是不允许直接打开streamingAssets下的exe,而是必须通过我这盒子才能打开
     做法是破环原exe里的某个核心文件,当通过我这个盒子打开的时候复原,等exe关闭了又删除复原的文件,本例
     是将level0文件改名为first,启动时复制一份恢复名字level0*/
    string firstFile = Path.Combine(Application.streamingAssetsPath, "LaoFu/ElderlyYaChuang_Data/first");
     string levelFile = Path.Combine(Application.streamingAssetsPath, "LaoFu/ElderlyYaChuang_Data/level0");
    // Start is called before the first frame update
    void Start()
    {
        if (File.Exists(firstFile))
        {
            UnityEngine.Debug.Log("asdfasdfasdf");
            File.Copy(firstFile, levelFile);
            OpenExe();
        }
    }

    private TaskCompletionSource<bool> eventHandled;


    private async void OpenExe()
    {
        await PrintDoc(fileName, "");
    }
    // Print a file with any known extension.
    public async Task PrintDoc(string fileName, string message)
    {
        eventHandled = new TaskCompletionSource<bool>();
       using (myProcess = new Process())
        {

            try
            {
                // Start a process to print a file and raise an event when done.
                myProcess.StartInfo.FileName = fileName;
                myProcess.StartInfo.UseShellExecute = false;
                myProcess.StartInfo.CreateNoWindow = true;
                myProcess.StartInfo.WindowStyle = ProcessWindowStyle.Hidden;
                myProcess.StartInfo.Arguments = message;
               myProcess.EnableRaisingEvents = true;
                myProcess.Start();
                myProcess.Exited += new EventHandler(myProcess_Exited);
                //WindMgr.Instance.OnClickMinisize();
            }
            catch (Exception ex)
            {
                UnityEngine.Debug.LogError($"An error occurred trying to print \"{fileName}\":\n{ex.Message}");
                return;
            }
            //myProcess.WaitForExit();
            // Wait for Exited event, but not more than 30 seconds.
            await Task.WhenAny(eventHandled.Task).ContinueWith(_ =>
            {
                //WindMgr.Instance.OnClickBacksize();
                //WindMgr.Instance.OnClickMaxsize();
                UnityEngine.Debug.Log("退出");
                // myProcess.Kill();
                myProcess.Close();
            });

        }
    }
     private void Update()
    {
        if (Input.GetKeyDown(KeyCode.Q))
        {
            myProcess.Kill();
        }
    }

    private void myProcess_Exited(object sender, System.EventArgs e)
    {
        UnityEngine.Debug.Log("退出");
        Application.Quit();
#if UNITY_EDITOR
        // ... UNITY调试时候才编译
        UnityEditor.EditorApplication.isPlaying = false;
#endif
    }
     private void OnApplicationQuit()
    {
        if (myProcess != null)
        {
            myProcess.Close();
        }


        if (File.Exists(levelFile))
        {
            File.Delete(levelFile);

        }

    }

}

  • 0
    点赞
  • 3
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值