点击按钮,显示嵌入的程序:
using System.Diagnostics;
using System.Runtime.InteropServices;
namespace WinFormsApp1
{
public partial class Form1 : Form
{
private string AunityExecutablePath = "E:\\EXE\\My project (1).exe"; // 替换为你的Unity编辑器可执行文件路径
private Process unityProcess;
private IntPtr unityWindowHandle;
public Form1()
{
InitializeComponent();
}
private void Form1_Load(object sender, EventArgs e)
{
}
private void button1_Click(object sender, EventArgs e)
{
// 使用 Process.Start 启动 Unity 可执行文件
Process.Start(AunityExecutablePath);
}
private void button2_Click(object sender, EventArgs e)
{
CloseUnityProcess(); // 关闭之前的 Unity 进程
OpenUnityProcess("E:\\333\\ReducerExperiment.exe");
}
private void button3_Click(object sender, EventArgs e)
{
CloseUnityProcess(); // 关闭之前的 Unity 进程
OpenUnityProcess("E:\\444\\CircuitAssembly.exe");
}
public void OpenUnityProcess(string executablePath)
{
if (unityProcess == null || unityProcess.HasExited)
{
// string unityExecutablePath = "E:\\333\\ReducerExperiment.exe"; // 替换为你的 Unity 编辑器路径
// 关闭上一个 Unity 窗口
CloseUnityProcess();
unityProcess = new Process();
unityProcess.StartInfo.FileName = executablePath;
unityProcess.StartInfo.UseShellExecute = false;
unityProcess.StartInfo.CreateNoWindow = true;
unityProcess.Start();
unityProcess.WaitForInputIdle();
unityProcess.EnableRaisingEvents = true;
unityProcess.Exited += UnityProcess_Exited;
// 等待 Unity 编辑器的主窗口句柄变为可用
while (unityProcess.MainWindowHandle == IntPtr.Zero)
{
System.Threading.Thread.Sleep(100);
unityProcess.Refresh();
}
unityWindowHandle = unityProcess.MainWindowHandle;
// 将 Unity 编辑器窗口嵌入到 Panel 控件中
NativeMethods.SetParent(unityWindowHandle, panel1.Handle);
NativeMethods.SetWindowLong(unityWindowHandle, NativeMethods.GWL_STYLE, NativeMethods.WS_VISIBLE);
// 调整 Unity 游戏窗口尺寸以适应 Panel 控件尺寸
AdjustUnityWindowSize();
}
}
private void AdjustUnityWindowSize()
{
if (unityWindowHandle != IntPtr.Zero)
{
int targetWidth = panel1.Width;
int targetHeight = panel1.Height;
// 计算缩放比例
float scaleX = (float)targetWidth / 1920;
float scaleY = (float)targetHeight / 1080;
// 调整 Unity 窗口大小以适应缩放
int scaledWidth = (int)(1920 * scaleX);
int scaledHeight = (int)(1080 * scaleY);
NativeMethods.MoveWindow(unityWindowHandle, 0, 0, scaledWidth, scaledHeight, true);
// NativeMethods.MoveWindow(unityWindowHandle, 0, 0, panel1.Width, panel1.Height, true);
}
}
private void UnityProcess_Exited(object sender, EventArgs e)
{
// 处理 Unity 编辑器进程退出
}
public class NativeMethods
{
public const int GWL_STYLE = -16;
public const int WS_VISIBLE = 0x10000000;
[DllImport("user32.dll", SetLastError = true)]
public static extern IntPtr SetParent(IntPtr hWndChild, IntPtr hWndNewParent);
[DllImport("user32.dll")]
public static extern int SetWindowLong(IntPtr hWnd, int nIndex, int dwNewLong);
[DllImport("user32.dll")]
public static extern bool MoveWindow(IntPtr hWnd, int x, int y, int width, int height, bool repaint);
}
//解决关闭 Windows 窗体程序后,出现 Unity 的图标带有红色感叹号
//这种情况一般是Unity 编辑器进程没有正确关闭,或者在关闭窗体程序时可能发生了一些异常。
protected override void OnFormClosing(FormClosingEventArgs e)
{
base.OnFormClosing(e);
// 关闭 Unity 进程
CloseUnityProcess();
}
private void CloseUnityProcess()
{
if (unityProcess != null && !unityProcess.HasExited)
{
unityProcess.CloseMainWindow();
unityProcess.WaitForExit();
unityProcess.Dispose();
}
unityProcess = null; // 设置 unityProcess 为 null
}
}
}
两个按钮,切换Unity项目:
using System.Diagnostics;
using System.Runtime.InteropServices;
namespace WinFormsApp1
{
public partial class Form1 : Form
{
private string AunityExecutablePath = "E:\\EXE\\My project (1).exe"; // 替换为你的Unity编辑器可执行文件路径
private Process unityProcess;
private IntPtr unityWindowHandle;
public Form1()
{
InitializeComponent();
}
private void Form1_Load(object sender, EventArgs e)
{
}
private void button1_Click(object sender, EventArgs e)
{
// 使用 Process.Start 启动 Unity 可执行文件
Process.Start(AunityExecutablePath);
}
private void button2_Click(object sender, EventArgs e)
{
CloseUnityProcess(); // 关闭之前的 Unity 进程
OpenUnityProcess("E:\\333\\ReducerExperiment.exe");
}
private void button3_Click(object sender, EventArgs e)
{
CloseUnityProcess(); // 关闭之前的 Unity 进程
OpenUnityProcess("E:\\444\\CircuitAssembly.exe");
}
public void OpenUnityProcess(string executablePath)
{
if (unityProcess == null || unityProcess.HasExited)
{
// string unityExecutablePath = "E:\\333\\ReducerExperiment.exe"; // 替换为你的 Unity 编辑器路径
// 关闭上一个 Unity 窗口
CloseUnityProcess();
unityProcess = new Process();
unityProcess.StartInfo.FileName = executablePath;
unityProcess.StartInfo.UseShellExecute = false;
unityProcess.StartInfo.CreateNoWindow = true;
unityProcess.Start();
unityProcess.WaitForInputIdle();
unityProcess.EnableRaisingEvents = true;
unityProcess.Exited += UnityProcess_Exited;
// 等待 Unity 编辑器的主窗口句柄变为可用
while (unityProcess.MainWindowHandle == IntPtr.Zero)
{
System.Threading.Thread.Sleep(100);
unityProcess.Refresh();
}
unityWindowHandle = unityProcess.MainWindowHandle;
// 将 Unity 编辑器窗口嵌入到 Panel 控件中
NativeMethods.SetParent(unityWindowHandle, panel1.Handle);
NativeMethods.SetWindowLong(unityWindowHandle, NativeMethods.GWL_STYLE, NativeMethods.WS_VISIBLE);
// 调整 Unity 游戏窗口尺寸以适应 Panel 控件尺寸
AdjustUnityWindowSize();
}
}
private void AdjustUnityWindowSize()
{
if (unityWindowHandle != IntPtr.Zero)
{
int targetWidth = panel1.Width;
int targetHeight = panel1.Height;
// 计算缩放比例
float scaleX = (float)targetWidth / 1920;
float scaleY = (float)targetHeight / 1080;
// 调整 Unity 窗口大小以适应缩放
int scaledWidth = (int)(1920 * scaleX);
int scaledHeight = (int)(1080 * scaleY);
NativeMethods.MoveWindow(unityWindowHandle, 0, 0, scaledWidth, scaledHeight, true);
// NativeMethods.MoveWindow(unityWindowHandle, 0, 0, panel1.Width, panel1.Height, true);
}
}
private void UnityProcess_Exited(object sender, EventArgs e)
{
// 处理 Unity 编辑器进程退出
}
public class NativeMethods
{
public const int GWL_STYLE = -16;
public const int WS_VISIBLE = 0x10000000;
[DllImport("user32.dll", SetLastError = true)]
public static extern IntPtr SetParent(IntPtr hWndChild, IntPtr hWndNewParent);
[DllImport("user32.dll")]
public static extern int SetWindowLong(IntPtr hWnd, int nIndex, int dwNewLong);
[DllImport("user32.dll")]
public static extern bool MoveWindow(IntPtr hWnd, int x, int y, int width, int height, bool repaint);
}
//解决关闭 Windows 窗体程序后,出现 Unity 的图标带有红色感叹号
//这种情况一般是Unity 编辑器进程没有正确关闭,或者在关闭窗体程序时可能发生了一些异常。
protected override void OnFormClosing(FormClosingEventArgs e)
{
base.OnFormClosing(e);
// 关闭 Unity 进程
CloseUnityProcess();
}
private void CloseUnityProcess()
{
if (unityProcess != null && !unityProcess.HasExited)
{
unityProcess.CloseMainWindow();
unityProcess.WaitForExit();
unityProcess.Dispose();
}
unityProcess = null; // 设置 unityProcess 为 null
}
}
}