在Unity中打开外部程序可以使用System.Diagnostics.Process
类。
1、编写控制脚本:
using System.Collections;
using System.Collections.Generic;
using System.Diagnostics;
using UnityEngine;
public class OpenExternalProgram : MonoBehaviour
{
public string pathToProgram= @"E:\Project\test.exe"; //外部应用路径
// Start is called before the first frame update
void Start()
{
}
// Update is called once per frame
void Update()
{
if(Input.GetKeyDown(KeyCode.A))
{
OpenProgram();
}
}
public void OpenProgram()
{
Process.Start(pathToProgram); //打开外部程序
}
}
2、以上脚本定义了一个字符串变量pathToProgram
,用于存储外部程序的路径。OpenProgram
方法使用System.Diagnostics.Process
类中的Process.Start()
函数打开指定路径的外部程序。
3、把脚本放到场景中,在需要的时候调用OpenProgram
方法即可打开外部程序。
另外假如需要在Unity程序关闭外部程序,可以通过S