1,在Unity中嵌入另一个软件,主要方法是调用Win32系统Api的方法
1)、用Process.Start启动一个进程,把该进程存为process
2)、用win32方法FindWindow寻找process窗口句柄,SetParent把当前窗口设为process窗口的父窗口,SetWindowLong去掉process窗口的标题栏,SetWindowPos设置process窗口的位置
using System;
using System.Collections;
using System.Diagnostics;
using System.Runtime.InteropServices;
using UnityEngine;
public class TestEmbedExe : MonoBehaviour
{
#region Win32 Api
[DllImport("user32.dll")]
static extern bool SetWindowPos(IntPtr hWnd, int hWndInserAfter, int x, int y, int cx, int cy, uint uFlags);
[DllImport("user32.dll")]
static extern IntPtr SetWindowLong(IntPtr hwnd, int _nIndex, long dwNewLong);
[DllImport("user32.dll")]
static extern IntPtr GetForegroundWindow();
[DllImport("user32.dll")]
static extern IntPtr FindWindow(string className, string windowName);
[DllImport("user32.dll")]
private static extern long SetParent(IntPtr hWndChild, IntPtr hWndNewParent);
#endregion
OpenFileName fileInfo;
Process process;
private void Start() {
Screen.SetResolution(1600, 900, false);
}
private void OnGUI() {
if (GUILayout.Button("Open")) {
fileInfo = FileOperate.GetOpenFile();
if(fileInfo != null) {
if(process != null) {
if (!process.HasExited)
process.Kill();
process = null;
}
ProcessStartInfo info = new ProcessStartInfo(fileInfo.file);
process = Process.Start(info);
process.WaitForInputIdle();
StartCoroutine(Wait());
//IntPtr win = FindWindow(null, process.ProcessName);
//SetWindowLong(win, -16, 1);
//SetParent(win, GetForegroundWindow());
//SetWindowPos(win, 0, 0, 0, 300, 200, 0x0040);
}
}
if (GUILayout.Button("去边框")) {
IntPtr win = FindWindow(null, process.ProcessName);
SetWindowLong(win, -16, 1);
}
}
IEnumerator Wait() {
yield return 0;
IntPtr win = FindWindow(null, process.ProcessName);
SetParent(win, GetForegroundWindow());
yield return new WaitForSeconds(0.5f);
SetWindowLong(win, -16, 0x00800000L);
SetWindowPos(win, 0, 20, 20, 300, 200, 0x0040);
}
private void OnDestroy() {
if (process != null) {
if (!process.HasExited)
process.Kill();
process = null;
}
}
}
2,本例中用到调用windows资源管理器功能,代码如下
using Microsoft.Win32;
using System;
using System.Collections;
using System.Collections.Generic;
using System.IO;
using System.Runtime.InteropServices;
using UnityEngine;
public class FileOperate {
private void OnGUI() {
if (GUILayout.Button("OpenDialog")) {
OpenFileName ofn = new OpenFileName();
ofn.structSize = Marshal.SizeOf(ofn);
ofn.filter = "All Files(.json)\0*.json\0\0";
ofn.file = new string(new char[256]);
ofn.maxFile = ofn.file.Length;
ofn.fileTitle = new string(new char[64]);
ofn.maxFileTitle = ofn.fileTitle.Length;
ofn.initialDir = Application.dataPath;
ofn.title = "open";
ofn.defExt = "jpg";
ofn.flags = 0x00080000 | 0x00001000 | 0x00000800 | 0x00000200 | 0x00000008;
if (GetOpenFileName1(ofn)) {
Debug.Log(ofn.file);
}
}
}
[DllImport("Comdlg32.dll", SetLastError = true, ThrowOnUnmappableChar = true, CharSet = CharSet.Auto)]
public static extern bool GetOpenFileName([In, Out]OpenFileName ofn);
public static bool GetOpenFileName1([In, Out]OpenFileName ofn) {
return GetOpenFileName(ofn);
}
public static OpenFileName GetOpenFile() {
OpenFileName ofn = new OpenFileName();
ofn.structSize = System.Runtime.InteropServices.Marshal.SizeOf(ofn);
ofn.filter = "All Files()\0*.*\0\0";
ofn.file = new string(new char[256]);
ofn.maxFile = ofn.file.Length;
ofn.fileTitle = new string(new char[64]);
ofn.maxFileTitle = ofn.fileTitle.Length;
ofn.initialDir = Application.streamingAssetsPath;
ofn.title = "open";
ofn.defExt = "jpg";
ofn.flags = 0x00080000 | 0x00001000 | 0x00000800 | 0x00000200 | 0x00000008;
GetOpenFileName1(ofn);
return ofn;
}
}
[StructLayout(LayoutKind.Sequential, CharSet = CharSet.Auto)]
public class OpenFileName {
public int structSize = 0;
public IntPtr dlgOwner = IntPtr.Zero;
public IntPtr instance = IntPtr.Zero;
public string filter = null;
public string custonFilter = null;
public int maxCustFilter = 0;
public int filterIndex = 0;
public string file = null;
public int maxFile = 0;
public string fileTitle = null;
public int maxFileTitle = 0;
public string initialDir = null;
public string title = null;
public int flags = 0;
public short fileOffect = 0;
public short fileExtension = 0;
public string defExt = null;
public IntPtr custData = IntPtr.Zero;
public IntPtr hook = IntPtr.Zero;
public string templateName = null;
public IntPtr reservedPtr = IntPtr.Zero;
public int reservedInt = 0;
public int flagsEx = 0;
}