当前位置:   article > 正文

Unity 启动exe 并且监听exe的关闭_unity程序退出监听

unity程序退出监听

将需要启动的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);

        }

    }

}

  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 19
  • 20
  • 21
  • 22
  • 23
  • 24
  • 25
  • 26
  • 27
  • 28
  • 29
  • 30
  • 31
  • 32
  • 33
  • 34
  • 35
  • 36
  • 37
  • 38
  • 39
  • 40
  • 41
  • 42
  • 43
  • 44
  • 45
  • 46
  • 47
  • 48
  • 49
  • 50
  • 51
  • 52
  • 53
  • 54
  • 55
  • 56
  • 57
  • 58
  • 59
  • 60
  • 61
  • 62
  • 63
  • 64
  • 65
  • 66
  • 67
  • 68
  • 69
  • 70
  • 71
  • 72
  • 73
  • 74
  • 75
  • 76
  • 77
  • 78
  • 79
  • 80
  • 81
  • 82
  • 83
  • 84
  • 85
  • 86
  • 87
  • 88
  • 89
  • 90
  • 91
  • 92
  • 93
  • 94
  • 95
  • 96
  • 97
  • 98
  • 99
  • 100
  • 101
  • 102
  • 103
  • 104
  • 105
  • 106
  • 107
  • 108
  • 109
  • 110
声明:本文内容由网友自发贡献,不代表【wpsshop博客】立场,版权归原作者所有,本站不承担相应法律责任。如您发现有侵权的内容,请联系我们。转载请注明出处:https://www.wpsshop.cn/w/知新_RL/article/detail/108123
推荐阅读
相关标签
  

闽ICP备14008679号