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