Unity 透明窗口

这篇博客介绍了一个Unity脚本,通过C#调用Windows API实现了窗口透明化和点击穿透功能。用户按下Q键可以使窗口变为全透明,E键则取消透明效果。脚本使用了DllImport特性导入user32.dll和Dwmapi.dll库,设置窗口样式和扩展帧区域来达到效果。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

 


using System;
using System.Runtime.InteropServices;
using System.Collections;
using System.Collections.Generic;
using UnityEngine;

public class TransparentWindow : MonoBehaviour {

    [DllImport("user32.dll")]
    public static extern int MessageBox(IntPtr hWnd, string text, string caption, uint type);

    [DllImport("user32.dll")]
    private static extern IntPtr GetActiveWindow();

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

    [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 int SetLayeredWindowAttributes(IntPtr hwnd, uint crKey, byte bAlpha, uint dwFlags);

    private struct MARGINS {
        public int cxLeftWidth;
        public int cxRightWidth;
        public int cyTopHeight;
        public int cyBottomHeight;
    }

    [DllImport("Dwmapi.dll")]
    private static extern uint DwmExtendFrameIntoClientArea(IntPtr hWnd, ref MARGINS margins);

    const int GWL_EXSTYLE = -20;

    const uint WS_EX_LAYERED = 0x00080000;
    const uint WS_EX_TRANSPARENT = 0x00000020;

    static readonly IntPtr HWND_TOPMOST = new IntPtr(-1);

    const uint LWA_COLORKEY = 0x00000001;

    private IntPtr hWnd;

    private void Start() {

#if !UNITY_EDITOR
        hWnd = GetActiveWindow();

        MARGINS margins = new MARGINS { cxLeftWidth = -1 };
        DwmExtendFrameIntoClientArea(hWnd, ref margins);

        SetWindowLong(hWnd, GWL_EXSTYLE, WS_EX_LAYERED | WS_EX_TRANSPARENT);
        //SetLayeredWindowAttributes(hWnd, 0, 0, LWA_COLORKEY);

        SetWindowPos(hWnd, HWND_TOPMOST, 0, 0, 0, 0, 0);
#endif

    }

    private void Update() {

        if (Input.GetKeyDown(KeyCode.Q))
        {
            hWnd = GetActiveWindow();

            MARGINS margins = new MARGINS { cxLeftWidth = -1 };
            DwmExtendFrameIntoClientArea(hWnd, ref margins);
        }
        if (Input.GetKeyDown(KeyCode.E))
        {
            hWnd = GetActiveWindow();

            MARGINS margins = new MARGINS { cxLeftWidth = 0 };
            DwmExtendFrameIntoClientArea(hWnd, ref margins);
        }
        SetClickthrough(true);
    }

    private void SetClickthrough(bool clickthrough) {
        if (clickthrough) {
            SetWindowLong(hWnd, GWL_EXSTYLE, WS_EX_LAYERED | WS_EX_TRANSPARENT);
        } else {
            SetWindowLong(hWnd, GWL_EXSTYLE, WS_EX_LAYERED);
        }
    }
}

https://forum.unity.com/threads/solved-windows-transparent-window-with-opaque-contents-lwa_colorkey.323057/

### 实现 Unity 窗口透明效果 为了使 Unity 应用程序窗口变得透明,可以采用多种方法来达成这一目标。一种方式是通过调整摄像机的属性以及利用特定插件或脚本来实现。 #### 方法一:修改摄像机设置以创建半透明背景 在 Unity 编辑器中打开场景并选择主摄像机,在 Inspector 面板内定位到 "Clear Flags" 属性,并将其更改为 "Solid Color"[^2]。接着,在同一面板下的 Background 字段里调节 Alpha 值至 0 来达到完全透明的效果。此更改仅影响渲染的内容而不涉及实际窗口本身。 #### 方法二:使用第三方库或自定义代码 对于希望获得更加灵活控制的应用开发者来说,还可以考虑借助外部资源如 `Unity_TransparentWindowManager` 插件[^1]。该项目允许用户轻松地让 Unity 的窗口变为透明并且能够覆盖于桌面之上显示其他应用程序内容。安装该扩展包之后,遵循其文档说明配置即可完成所需功能开发工作。 另外,也可以参考 Alistair Lowe-Norris 提供的技术博客文章中的指导[^3],其中介绍了另一种不依赖额外工具而是在 Windows 平台上直接操作的方法,这涉及到编写 C++ 或者调用 WinAPI 函数去改变顶层窗口样式从而支持 alpha 通道混合模式。 ```csharp using UnityEngine; using System.Runtime.InteropServices; public class TransparentWindow : MonoBehaviour { [DllImport("user32.dll")] private static extern int SetWindowLong(IntPtr hWnd, int nIndex, uint dwNewLong); [DllImport("user32.dll", SetLastError = true)] private static extern int GetWindowLong(IntPtr hWnd, int nIndex); void Start() { IntPtr hwnd = new System.IntPtr(GetActiveWindow()); const int GWL_EXSTYLE = -20; const int WS_EX_LAYERED = 0x80000; const int WS_EX_TRANSPARENT = 0x20; SetWindowLong(hwnd, GWL_EXSTYLE, (GetWindowLong(hwnd, GWL_EXSTYLE)) | WS_EX_LAYERED | WS_EX_TRANSPARENT); // Additional code to set transparency level... } } ```
评论 3
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值