经过测试通过设置Project Settings无法达成无窗口的效果,但是可以通过 C# 调用 Windows 系统 API(User32.dll),在程序启动时获取窗口句柄并隐藏。
脚本代码如下:
using System;
using System.Runtime.InteropServices;
using UnityEngine;
public class HideWindow : MonoBehaviour
{
// 导入Windows API:查找窗口句柄
[DllImport("user32.dll", CharSet = CharSet.Unicode)]
private static extern IntPtr FindWindow(string lpClassName, string lpWindowName);
// 导入Windows API:隐藏窗口
[DllImport("user32.dll")]
private static extern bool ShowWindow(IntPtr hWnd, int nCmdShow);
private const int SW_HIDE = 0; // 隐藏窗口的命令
void Start()
{
// 确保程序在后台运行不暂停
Application.runInBackground = true;
// 根据"Product Name"查找窗口句柄(需与Player Settings中的Product Name一致)
string windowTitle = Application.productName;
IntPtr hWnd = FindWindow(null, windowTitle);
if (hWnd != IntPtr.Zero)
{
// 隐藏窗口
ShowWindow(hWnd, SW_HIDE);
Debug.Log("窗口已隐藏");
}
else
{
Debug.LogError("未找到窗口句柄,可能标题不匹配");
}
// (可选)若无需渲染,禁用所有Camera
Camera[] cameras = FindObjectsOfType<Camera>();
foreach (var cam in cameras)
{
cam.enabled = false;
}
}
// (可选)添加退出逻辑,避免后台残留进程
void Update()
{
if (Input.GetKeyDown(KeyCode.Escape))
{
Application.Quit();
}
}
}
将脚本挂载到场景的任意 GameObject 上,打包即可。



1万+

被折叠的 条评论
为什么被折叠?



