最近在做一个项目需要 Unity控制打开、关闭 第三方给的exe软件
首先新建脚本命名 :OpenOtherExe,将脚本托给MainCamera(当然挂载给谁看心情)
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using System.Diagnostics;//需要引入命名空间
public class OpenOtherExe : MonoBehaviour {
string exePath = "D:\\Test\\Debug\\TeacherMechine.exe";
// string str = Application.dataPath + "/Resources/MyFile/Debug/TeacherMechine.exe";
void OnGUI()
{
if (GUI.Button(new Rect(100, 100, 150, 50), "Start TeacherMechine"))
{
//打开第三软件
Process.Start(exePath);
}
if (GUI.Button(new Rect(100, 200, 150, 50), "Stop TeacherMechine"))
{
KillProcess("TeacherMechine");//括号里面的是exe的文件名
}
}
//关闭第三方软件
void KillProcess(string processName)
{
Process[] processes = Process.GetProcesses();
foreach (Process process in processes)
{
try
{
if (!process.HasExited)
{
if (process.ProcessName == processName)
{
process.Kill();
UnityEngine.Debug.Log("已关闭进程");
}
}
}
catch (System.InvalidOperationException ex)
{
UnityEngine.Debug.Log(ex);
}
}
}
/// <summary>
/// 该程序关闭会自动结束自己打开的进程
/// </summary>
private void OnDisable()
{
KillProcess("TeacherMechine");
}
}
————————————————
版权声明:本文为优快云博主「奋斗的菇凉」的原创文章,遵循CC 4.0 BY-SA版权协议,转载请附上原文出处链接及本声明。
原文链接:https://blog.youkuaiyun.com/qq_40229737/article/details/95229070
第一种路径:"D:\\Test\\Debug\\TeacherMechine.exe";(第三方exe的文件路径)
路径给定死了,但是可以在Unity没有打包出来的情况下进行运行测试。
第二种路径: string str = Application.dataPath + "/Resources/MyFile/Debug/TeacherMechine.exe";
Unity打完包出来后,在Resources下面新建文件夹,起名为MyFile,将对应相应的外部EXE工程复制到此路径下,将路径标好,对应相应的EXE。
第二种路径,在于随便哪个盘,便可引用外部EXE,关键在于Application.dataPath。
原文链接:https://blog.youkuaiyun.com/qq_40229737/article/details/95229070