当前位置: 首页 > news >正文

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(){}
}
http://www.lryc.cn/news/493642.html

相关文章:

  • CTF之密码学(埃特巴什码 )
  • 深入解析 PyTorch 的 torch.load() 函数:用法、参数与实际应用示例
  • ros2键盘实现车辆: 简单的油门_刹车_挡位_前后左右移动控制
  • ubuntu安装chrome无法打开问题
  • CTF-RE 从0到N:Chacha20逆向实战 2024 强网杯青少年专项赛 EnterGame WP (END)
  • vue3 ajax获取json数组排序举例
  • web安全之信息收集
  • 报错:java: 无法访问org.springframework.boot.SpringApplication
  • 线上+线下≠新零售,6大互通诠释新零售的核心要点-亿发
  • GitHub Copilot革命性更新:整合顶尖AI模型,如何重塑开发体验?
  • AWS账户是否支持区域划分?
  • Easy Excel 通过【自定义批注拦截器】实现导出的【批注】功能
  • 整数对最小和(Java Python JS C++ C )
  • MySQL 启动失败问题分析与解决方案:`mysqld.service failed to run ‘start-pre‘ task`
  • 谷歌浏览器Chrome打开百度很慢,其他网页正常的解决办法,试了很多,找到了适合的
  • 深度学习Pytorch中的模型保存与加载方法
  • 小红书矩阵运营:怎么通过多个账号来提升品牌曝光?
  • Llama-2-7b:vocab size:32000;embeddings:4096;hidden_layers是什么意思
  • 【moveit!】ROS学习笔记
  • 【Leetcode 每日一题 - 补卡】3259. 超级饮料的最大强化能量
  • 【人工智能】使用Python实现序列到序列(Seq2Seq)模型进行机器翻译
  • 量化交易系统开发-实时行情自动化交易-4.4.1.做市策略实现
  • Pinia之2:计数器案例、computed函数、异步action、storeToRefs函数、pinia调试
  • Microsoft Excel如何插入多行
  • Redis【1】- 如何阅读Redis 源码
  • shell查看服务器的内存和CPU,实时使用情况
  • 软件/游戏提示:mfc42u.dll没有被指定在windows上运行如何解决?多种有效解决方法汇总分享
  • 《Python基础》之函数、模块与库
  • selinux和防火墙实验
  • k8s Init:ImagePullBackOff 的解决方法