Unity与外部exe交互

个人插眼学习:
1.打开关闭exe程序

using UnityEngine;
using System.Collections;
using System.Diagnostics;
using System.Collections.Generic;
using System;
 
public class CallApplication : MonoBehaviour
{
 
    // Use this for initialization
    private static string outputPath = @"C:\Program Files (x86)\Google\Chrome\Application\chrome.exe";
    private int x = 0, y = 0;
    private bool isOn = false;
    private int minute = 0;
 
    // Use this for initialization
    void Start()
    {
        //ListAllAppliction();
        UnityEngine.Debug.Log("当前应用:" + Process.GetCurrentProcess().ProcessName + " 进程ID: " + Process.GetCurrentProcess().Id);
    }
    void OnGUI()
    {
        if (GUI.Button(new Rect(10, 10, 200, 50), "打开外部应用"))
        {
            if (CheckProcess("chrome"))
                return;
            else
                StartProcess(outputPath);
        }
        if (GUI.Button(new Rect(10, 60, 200, 50), "杀死应用进程"))
        {
            KillProcess("chrome");
        }
        if (GUI.Button(new Rect(10, 110, 200, 50), "开启定时关闭"))
        {
            minute = System.DateTime.Now.Minute + 1;
            isOn = true;
        }
        if (isOn)
        {
            GUI.contentColor = Color.red;
            GUI.Label(new Rect(10, 160, 160, 30), "\t倒计时:" + (60 - System.DateTime.Now.Second));
            if (System.DateTime.Now.Minute == minute)
            {
                UnityEngine.Debug.Log("自动关闭应用....");
                KillProcess("chrome");
                isOn = false;
            }
        }
    }
    /// 
    /// 开启应用
    /// 
    /// 
    void StartProcess(string ApplicationPath)
    {
        UnityEngine.Debug.Log("打开本地应用");
        Process foo = new Process();
        foo.StartInfo.FileName = ApplicationPath;
        foo.Start();
    }
 
    /// 
    /// 检查应用是否正在运行
    /// 
    bool CheckProcess(string processName)
    {
        bool isRunning = false;
        Process[] processes = Process.GetProcesses();
        int i = 0;
        foreach (Process process in processes)
        {
            try
            {
                i++;
                if (!process.HasExited)
                {
                    if (process.ProcessName.Contains(processName))
                    {
                        UnityEngine.Debug.Log(processName + "正在运行");
                        isRunning = true;
                        continue;
                    }
                    else if (!process.ProcessName.Contains(processName) && i > processes.Length)
                    {
                        UnityEngine.Debug.Log(processName + "没有运行");
                        isRunning = false;
                    }
                }
            }
            catch (Exception ep)
            {
            }
        }
        return isRunning;
    }
    /// 
    /// 列出已开启的应用
    /// 
    void ListAllAppliction()
    {
        Process[] processes = Process.GetProcesses();
        int i = 0;
        foreach (Process process in processes)
        {
            try
            {
                if (!process.HasExited)
                {
                    UnityEngine.Debug.Log("应用ID:" + process.Id + "应用名:" + process.ProcessName);
                }
            }
            catch (Exception ep)
            {
            }
        }
    }
    /// 
    /// 杀死进程
    /// 
    /// 应用程序名
    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)
            {
                //UnityEngine.Debug.Log("Holy batman we've got an exception!");
            }
        }
    }
 
}

转自这里
2.unity打开外部的exe程序并隐藏窗口

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Diagnostics;
using System.Drawing;
using System.IO;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
 
namespace 打开外部记事本并写入一段话
{
    public partial class Form1 : Form
    {
        Process pro;    
        public Form1()
        {
            InitializeComponent();
        }
        /// 
        /// 写入按钮的点击事件
        /// 
        private void btnWrite_Click(object sender, EventArgs e)
        {
            //要写入的文本
            string strWrite = "要追加写入的文本 \r\n";         //\r\n是换行
            File.AppendAllText("要打开并写入的记事本.txt", strWrite);  //第一个参数是文件的路径,第二个参数是要追加写入的文本
 
            pro = new Process();
            pro.StartInfo.WindowStyle=ProcessWindowStyle.Normal;  //打开的窗口为正常大小显示
            pro.StartInfo.FileName = "要打开并写入的记事本.txt";  //该记事本在bin目录下,需要提前建好
            //打开记事本
            pro.Start();   
 
        }
        /// 
        /// 关闭按钮的点击事件,用来关闭记事本的窗口并释放资源
        /// 
        private void btnCloseTxtWindow_Click(object sender, EventArgs e)
        {
            pro.CloseMainWindow();
            pro.Close();
            pro.Dispose();
        }
    }
}

转自这里

你可能感兴趣的:(C#,Unity)