Unity3D导出exe窗口参数调整
在Unity3D开发中,有时我们需要对导出的exe窗口参数进行调整,以达到特定的显示效果。下面将详细介绍具体的实现方法。
代码实现
using System;
using System.Runtime.InteropServices;
using UnityEngine;
public class WindowMod : MonoBehaviour
{
// 定义窗口样式枚举
public enum appStyle
{
FullScreen,
WindowedFullScreen,
Windowed,
WindowedWithoutBorder
}
// 定义窗口Z轴深度枚举
public enum zDepth
{
Normal,
Top,
TopMost
}
// 常量定义
private const uint SWP_SHOWWINDOW = 64u;
private const int GWL_STYLE = -16;
private const int WS_BORDER = 1;
private const int GWL_EXSTYLE = -20;
private const int WS_CAPTION = 12582912;
private const int WS_POPUP = 8388608;
private const int SM_CXSCREEN = 0;
private const int SM_CYSCREEN = 1;
// 窗口样式和深度设置
public appStyle AppWindowStyle = appStyle.WindowedFullScreen;
public zDepth ScreenDepth;
// 窗口位置和大小设置
public int windowLeft = 10;
public int windowTop = 10;
public int windowWidth = 800;
public int windowHeight = 600;
private Rect screenPosition;
private IntPtr HWND_TOP = new IntPtr(0);
private IntPtr HWND_TOPMOST = new IntPtr(-1);
private IntPtr HWND_NORMAL = new IntPtr(-2);
private int Xscreen;
private int Yscreen;
private int i;
// 导入Windows API函数
[DllImport("user32.dll")]
private static extern IntPtr GetForegroundWindow();
[DllImport("user32.dll", CharSet = CharSet.Auto)]
public static extern bool SetWindowPos(IntPtr hWnd, IntPtr hPos, int x, int y, int cx, int cy, uint nflags);
[DllImport("User32.dll")]
private static extern IntPtr FindWindow(string lpClassName, string lpWindowName);
[DllImport("User32.dll")]
private static extern int SetWindowLong(IntPtr hWnd, int nIndex, int dwNewLong);
[DllImport("User32.dll")]
private static extern int GetWindowLong(IntPtr hWnd, int dwNewLong);
[DllImport("User32.dll")]
private static extern bool MoveWindow(IntPtr hWnd, int x, int y, int width, int height, bool repaint);
[DllImport("user32.dll", CharSet = CharSet.Auto)]
public static extern int ShowWindow(IntPtr hwnd, int nCmdShow);
[DllImport("user32.dll", CharSet = CharSet.Auto)]
public static extern int SendMessage(IntPtr hwnd, int msg, IntPtr wP, IntPtr IP);
[DllImport("user32.dll", CharSet = CharSet.Auto)]
public static extern IntPtr SetParent(IntPtr hChild, IntPtr hParent);
[DllImport("user32.dll", CharSet = CharSet.Auto)]
public static extern IntPtr GetParent(IntPtr hChild);
[DllImport("User32.dll")]
public static extern IntPtr GetSystemMetrics(int nIndex);
// 初始化方法
private void Start()
{
// 获取屏幕分辨率
Xscreen = (int)GetSystemMetrics(SM_CXSCREEN);
Yscreen = (int)GetSystemMetrics(SM_CYSCREEN);
// 根据不同的窗口样式设置分辨率和位置
switch (AppWindowStyle)
{
case appStyle.FullScreen:
Screen.SetResolution(Xscreen, Yscreen, true);
break;
case appStyle.WindowedFullScreen:
Screen.SetResolution(Xscreen - 1, Yscreen - 1, false);
screenPosition = new Rect(0f, 0f, Xscreen - 1, Yscreen - 1);
break;
case appStyle.Windowed:
Screen.SetResolution(windowWidth, windowHeight, false);
break;
case appStyle.WindowedWithoutBorder:
Screen.SetResolution(windowWidth, windowHeight, false);
screenPosition = new Rect(windowLeft, windowTop, windowWidth, windowHeight);
break;
}
}
// 更新方法
private void Update()
{
if (i < 5)
{
switch (AppWindowStyle)
{
case appStyle.WindowedFullScreen:
// 设置窗口样式
SetWindowLong(GetForegroundWindow(), GWL_STYLE, 369164288);
// 根据不同的Z轴深度设置窗口位置
switch (ScreenDepth)
{
case zDepth.Normal:
SetWindowPos(GetForegroundWindow(), HWND_NORMAL, (int)screenPosition.x, (int)screenPosition.y, (int)screenPosition.width, (int)screenPosition.height, SWP_SHOWWINDOW);
break;
case zDepth.Top:
SetWindowPos(GetForegroundWindow(), HWND_TOP, (int)screenPosition.x, (int)screenPosition.y, (int)screenPosition.width, (int)screenPosition.height, SWP_SHOWWINDOW);
break;
case zDepth.TopMost:
SetWindowPos(GetForegroundWindow(), HWND_TOPMOST, (int)screenPosition.x, (int)screenPosition.y, (int)screenPosition.width, (int)screenPosition.height, SWP_SHOWWINDOW);
break;
}
// 显示窗口
ShowWindow(GetForegroundWindow(), 3);
break;
case appStyle.Windowed:
// 根据不同的Z轴深度设置窗口位置
switch (ScreenDepth)
{
case zDepth.Normal:
SetWindowPos(GetForegroundWindow(), HWND_NORMAL, 0, 0, 0, 0, 3u);
SetWindowPos(GetForegroundWindow(), HWND_NORMAL, 0, 0, 0, 0, 35u);
break;
case zDepth.Top:
SetWindowPos(GetForegroundWindow(), HWND_TOP, 0, 0, 0, 0, 3u);
SetWindowPos(GetForegroundWindow(), HWND_TOP, 0, 0, 0, 0, 35u);
break;
case zDepth.TopMost:
SetWindowPos(GetForegroundWindow(), HWND_TOPMOST, 0, 0, 0, 0, 3u);
SetWindowPos(GetForegroundWindow(), HWND_TOPMOST, 0, 0, 0, 0, 35u);
break;
}
break;
case appStyle.WindowedWithoutBorder:
// 设置窗口样式
SetWindowLong(GetForegroundWindow(), GWL_STYLE, 369164288);
// 根据不同的Z轴深度设置窗口位置
switch (ScreenDepth)
{
case zDepth.Normal:
SetWindowPos(GetForegroundWindow(), HWND_NORMAL, (int)screenPosition.x, (int)screenPosition.y, (int)screenPosition.width, (int)screenPosition.height, SWP_SHOWWINDOW);
break;
case zDepth.Top:
SetWindowPos(GetForegroundWindow(), HWND_TOP, (int)screenPosition.x, (int)screenPosition.y, (int)screenPosition.width, (int)screenPosition.height, SWP_SHOWWINDOW);
break;
case zDepth.TopMost:
SetWindowPos(GetForegroundWindow(), HWND_TOPMOST, (int)screenPosition.x, (int)screenPosition.y, (int)screenPosition.width, (int)screenPosition.height, SWP_SHOWWINDOW);
break;
}
break;
}
i++;
}
}
}
代码解释
枚举类型
appStyle:定义了四种窗口样式,分别是全屏、窗口化全屏、普通窗口和无边界窗口。zDepth:定义了三种窗口的Z轴深度,分别是正常、置顶和最置顶。
常量定义
定义了一些Windows API所需的常量,如窗口样式标志、系统指标等。
导入Windows API函数
通过DllImport特性导入了一系列Windows API函数,用于操作窗口的位置、样式和显示状态。
Start方法
在游戏开始时,根据AppWindowStyle的值设置窗口的分辨率和位置。
Update方法
在游戏运行过程中,根据AppWindowStyle和ScreenDepth的值设置窗口的样式和位置。为了确保窗口设置生效,该方法会在开始的前5帧内执行。
通过以上代码,我们可以方便地调整Unity3D导出的exe窗口的参数,实现不同的显示效果。