Unity开启外部EXE程序

Unity开启外部EXE

using System;
using System.Collections;
using System.Collections.Generic;
using System.Diagnostics;
using System.Runtime.InteropServices;
using System.Threading.Tasks;
using UnityEditor;
using UnityEngine;

public class Unity_OpenExe : MonoBehaviour
{
    
    



    public string[] WebCamList;
    public List<Process> CamProcessList = new List<Process>();

    //使用查找任务栏
    [DllImport("user32.dll")]
    static extern IntPtr FindWindow(string strClassName, int nptWindowName);
    //当前窗口
    [DllImport("user32.dll")] static extern IntPtr GetForegroundWindow();
    //获取窗口位置以及大小
    [DllImport("user32.dll")] static extern bool GetWindowRect(IntPtr hWnd, ref RECT lpRect);

    [StructLayout(LayoutKind.Sequential)]
    private struct RECT
    {
    
    
        public int Left; //最左坐标
        public int Top; //最上坐标
        public int Right; //最右坐标
        public int Bottom; //最下坐标
    }

    [DllImport("user32.dll")]
    static extern IntPtr SetWindowLong(IntPtr hwnd, int _nIndex, int dwNewLong);

    [DllImport("user32.dll")]
    static extern bool SetWindowPos(IntPtr hWnd, int hWndInsertAfter, int X, int Y, int cx, int cy, uint uFlags);

    [DllImport("user32.dll", SetLastError = true)]
    private static extern int GetWindowLong(IntPtr hWnd, int nIndex);

    const uint SWP_SHOWWINDOW = 0x0040;
    const int GWL_STYLE = -16;
    const int WS_BORDER = 1;
    const int WS_POPUP = 0x800000;
    Process X;
    Process Y;
    IntPtr intPtr1;

    private string ProcessName;
    List<string> exeS = new List<string>();
    private void Start()
    {
    
    
#if UNITY_EDITOR

#else
        OpenNetBox();
#endif
    }

    public void OpenNetBox()
    {
    
    
        UnityEngine.Debug.LogWarning("使用前请配置文件路径");

        if (WebCamList != null && WebCamList.Length > 0)
        {
    
    
            foreach (var item in WebCamList)
            {
    
    
                Task.Run(() =>
                {
    
    
                    try
                    {
    
    
                        string exePath = Application.streamingAssetsPath + "/" + item;
                        Process webcam = Process.Start(exePath);
                        IntPtr winIntPtr0 = FindWindow("PreviewDemo", 0);

                        if (winIntPtr0 != null)
                        {
    
    

                         //   UnityEngine.Debug.LogWarning("句柄找到");
                            // SetWindowPos(winIntPtr0, 0, 500, 800, 500, 687, 3);
                            //1280,970,600,400
                            //1280,462,600,400
                        }

                        CamProcessList.Add(webcam);
                    }
                    catch (Exception ex)
                    {
    
    

                        throw ex;
                    }


                });
            }
            //Task.Run(() =>
            //{
    
    

            //    exePath1 = Application.streamingAssetsPath + "/" + "WebCam1/PreviewDemo.exe";
            //    X = Process.Start(exePath1);
            //    IntPtr winIntPtr0 = FindWindow("PreviewDemo", 0);
            //    if (winIntPtr0 != null)
            //    {
    
    

            //        UnityEngine.Debug.LogWarning("句柄找到");
            //        SetWindowPos(winIntPtr0, 0, 500, 800, 500, 687, 3);
            //        //1280,970,600,400
            //        //1280,462,600,400
            //    }

            //});
        }

        //Task.Run(() =>
        //{
    
    

        //    exePath2 = Application.streamingAssetsPath + "/" + "WebCam2/PreviewDemo.exe";
        //    Y = Process.Start(exePath2);
        //    IntPtr winIntPtr1 = FindWindow("PreviewDemo", 0);
        //    if (winIntPtr1 != null)
        //    {
    
    

        //        UnityEngine.Debug.LogWarning("句柄找到");
        //       // SetWindowPos(winIntPtr1, 0, 0, 0, 1280, 687, 3);
        //        SetWindowPos(winIntPtr1, 0, 0, 0, 1280, 767, 3);
        //    }
        //});



        if (string.IsNullOrEmpty(ProcessName))
        {
    
    
            //ProcessName = X.ProcessName;
        }

    }

    public void CloseNetBox()
    {
    
    
        X.Kill();
        Y.Kill();
    }

    private void OnDestroy()
    {
    
    
        foreach (var process in Process.GetProcesses())
        {
    
    
            if (process.ProcessName.Equals("PreviewDemo") && !process.HasExited)
                process.Kill();
        }


        if (CamProcessList != null)
        {
    
    
            for (int i = 0; i < CamProcessList.Count; i++)
            {
    
    
                if (CamProcessList[i] != null && !CamProcessList[i].HasExited)
                {
    
    
                    CamProcessList[i].Kill();
                }
            }
        }
        CamProcessList.Clear();
        //if (X != null)
        //{
    
    
        //    X.Kill();
        //}
        //if (Y != null)
        //{
    
    

        //    Y.Kill();
        //}
    }

    void Update()
    {
    
    

    }
}