U3D让窗口完全透明

目前有几个前提条件
一 全屏
二 camera 设置为solid color 背景色为(0 0 0 0)
三 projectSettings->Player->OrtherSetting : Auto Graphics API for Windows 勾选-去掉
四 projectSettings->Player->ResolutionAndPresentation : Use DXGI flip model swapchain for D3D11 勾选-去掉

using System;
using System.Runtime.InteropServices;
using UnityEngine;

public class Transparents : MonoBehaviour
{
    // 导入 user32.dll 库以使用 Windows API 函数
    [DllImport("user32.dll")]
    public static extern int MessageBox(IntPtr hWnd, string text, string caption, uint type);

    // 定义一个结构来存储窗口边框的边距大小
    private struct MARGINS
    {
        public int cxLeftWidth;
        public int cxRightWidth;
        public int cyTopHeight;
        public int cyBottomHeight;
    }

    // 导入 user32.dll 以获取活动窗口句柄 (HWND)
    [DllImport("user32.dll")]
    private static extern IntPtr GetActiveWindow();

    // 导入 Dwmapi.dll 以将窗口边框扩展到客户区域
    [DllImport("Dwmapi.dll")]
    private static extern uint DwmExtendFrameIntoClientArea(IntPtr hWnd, ref MARGINS margins);

    // 导入 user32.dll 以修改窗口属性
    [DllImport("user32.dll")]
    private static extern int GetWindowLong(IntPtr hWnd, int nIndex);

    [DllImport("user32.dll")]
    private static extern int SetWindowLong(IntPtr hWnd, int nIndex, int dwNewLong);

    // 导入 user32.dll 以设置窗口位置
    [DllImport("user32.dll", SetLastError = true)]
    static extern bool SetWindowPos(IntPtr hWnd, IntPtr hWndInsertAfter, int X, int Y, int cx, int cy, uint uFlags);

    [DllImport("user32.dll")]
    static extern bool ShowWindow(IntPtr hWnd, int nCmdShow);

    // 导入 user32.dll 以设置分层窗口属性 (透明度)
    [DllImport("user32.dll")]
    static extern int SetLayeredWindowAttributes(IntPtr hWnd, uint crKey, byte bAlpha, uint dwFlags);

    // 代码中使用的常量和变量
    const int GWL_EXSTYLE = -20;  // 修改窗口样式的索引
    const uint WS_EX_LAYERED = 0x00080000;  // 分层窗口的扩展样式
    const uint WS_EX_TRANSPARENT = 0x00000020;  // 透明窗口的扩展样式
    const uint WS_EX_TOOLWINDOW = 0x00000080;  // 从 Alt+Tab 和任务栏隐藏
    const uint WS_EX_APPWINDOW = 0x00040000;  // 显示在任务栏/Alt+Tab
    static readonly IntPtr HWND_TOPMOST = new IntPtr(-1);  // 窗口插入位置(始终置顶)
    const uint LWA_COLORKEY = 0x00000001;  // 设置颜色键的标志(用于透明度)
    const int SW_HIDE = 0;
    const int SW_SHOW = 5;
    const uint SWP_NOSIZE = 0x0001;
    const uint SWP_NOMOVE = 0x0002;
    const uint SWP_NOZORDER = 0x0004;
    const uint SWP_NOACTIVATE = 0x0010;
    private IntPtr hWnd;  // 活动窗口的句柄
    [SerializeField] private float restoreDelay = 2f;
    [SerializeField] private bool restoreAutomatically = true;

    private void Start()
    {
        // 显示一个消息框(仅用于演示目的)
        // MessageBox(new IntPtr(0), "Hello world", "Hello Dialog", 0);
        Camera.main.clearFlags = CameraClearFlags.SolidColor;
        Camera.main.backgroundColor = Color.black;
#if !UNITY_EDITOR
        // 获取活动窗口的句柄
        hWnd = GetActiveWindow();
    
        // 创建一个边距结构来定义边框大小
        MARGINS margins = new MARGINS { cxLeftWidth = -1 };
 
        // 将窗口边框扩展到客户区域(玻璃效果)
        //DwmExtendFrameIntoClientArea(hWnd, ref margins);
 
        // 将窗口样式设置为分层、透明,并从 Alt+Tab/任务栏隐藏
        int exStyle = GetWindowLong(hWnd, GWL_EXSTYLE);
        exStyle |= unchecked((int)WS_EX_LAYERED);
        exStyle |= unchecked((int)WS_EX_TOOLWINDOW);
        exStyle &= unchecked((int)~WS_EX_APPWINDOW);
        SetWindowLong(hWnd, GWL_EXSTYLE, exStyle);
 
        // 设置窗口颜色键(用于透明度)
        SetLayeredWindowAttributes(hWnd, 0x000000, 0, LWA_COLORKEY);
 
        // 将窗口位置设置为始终置顶
        SetWindowPos(hWnd, HWND_TOPMOST, 0, 0, 0, 0, 0);

        // 启动时先隐藏到不可见位置,完成初始化后再恢复显示
        ShowWindow(hWnd, SW_HIDE);
        SetWindowPos(hWnd, IntPtr.Zero, -32000, -32000, 0, 0, SWP_NOSIZE | SWP_NOZORDER | SWP_NOACTIVATE);

        if (restoreAutomatically)
        {
            Invoke(nameof(RestoreWindow), restoreDelay);
        }
#endif

        // 允许应用在后台运行
        Application.runInBackground = true;
    }

    public void RestoreWindow()
    {
#if !UNITY_EDITOR
        if (hWnd == IntPtr.Zero)
        {
            hWnd = GetActiveWindow();
        }

        if (hWnd != IntPtr.Zero)
        {
            ShowWindow(hWnd, SW_SHOW);
            SetWindowPos(hWnd, HWND_TOPMOST, 0, 0, 0, 0, SWP_NOMOVE | SWP_NOSIZE | SWP_NOACTIVATE);
        }
#endif
    }
}
posted @ 2026-03-31 17:04  stweily  阅读(4)  评论(0)    收藏  举报