欢迎来到尧图网

客户服务 关于我们

您的位置:首页 > 房产 > 建筑 > Unity开启外部EXE程序

Unity开启外部EXE程序

2024/11/30 6:52:21 来源:https://blog.csdn.net/ForKnowledgeMe/article/details/144074541  浏览:    关键词: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#elseOpenNetBox();
#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(){}
}

版权声明:

本网仅为发布的内容提供存储空间,不对发表、转载的内容提供任何形式的保证。凡本网注明“来源:XXX网络”的作品,均转载自其它媒体,著作权归作者所有,商业转载请联系作者获得授权,非商业转载请注明出处。

我们尊重并感谢每一位作者,均已注明文章来源和作者。如因作品内容、版权或其它问题,请及时与我们联系,联系邮箱:809451989@qq.com,投稿邮箱:809451989@qq.com