2023最新unity 发布exe程序背景透明穿透

由于网上的一些帖子比较老了,好多人评论不能用黑屏之类的。这边重新整理个最新的,亲测可用。

先大体概括下,一共就几处设置,一个脚本。

一、设置

普通渲染的项目摄像机的Clear Flags选择Solid Color,颜色为黑色(0,0,0,0)。

使用HDRP的项目,关闭掉摄像机上的脚本:HDAdditionalCameraData,摄像机默认的backgroundType,Sky改为Color,颜色为黑色(0,0,0,0)见下图。

↓↓↓特别注意下图,所使用的unity版本如果没有下面的选项则不用管。

下面放上脚本:

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

/// <summary>
/// 一共可选择三种样式
/// </summary>
public enum enumWinStyle
{
    /// <summary>
    /// 置顶
    /// </summary>
    WinTop,
    /// <summary>
    /// 置顶并且透明
    /// </summary>
    WinTopApha,
    /// <summary>
    /// 置顶透明并且可以穿透
    /// </summary>
    WinTopAphaPenetrate
}
public class WinSetting : MonoBehaviour
{

    #region Win函数常量
    private struct MARGINS
    {
        public int cxLeftWidth;
        public int cxRightWidth;
        public int cyTopHeight;
        public int cyBottomHeight;
    }

    [DllImport("user32.dll")]
    static extern IntPtr FindWindow(string lpClassName, string lpWindowName);
    [DllImport("user32.dll")]
    static extern int SetWindowLong(IntPtr hWnd, int nIndex, int dwNewLong);

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

    [DllImport("user32.dll")]
    static extern int SetWindowPos(IntPtr hWnd, int hWndInsertAfter, int X, int Y, int cx, int cy, int uFlags);

    [DllImport("user32.dll")]
    static extern int SetLayeredWindowAttributes(IntPtr hwnd, int crKey, int bAlpha, int dwFlags);

    [DllImport("Dwmapi.dll")]
    static extern uint DwmExtendFrameIntoClientArea(IntPtr hWnd, ref MARGINS margins);
    [DllImport("user32.dll")]
    private static extern int SetWindowLong(IntPtr hWnd, int nIndex, uint dwNewLong);
    private const int WS_POPUP = 0x800000;
	//设定一个新的扩展风格
    private const int GWL_EXSTYLE = -20;
	// 定义窗体样式,-16表示设定一个新的窗口风格
    private const int GWL_STYLE = -16;
    private const int WS_EX_LAYERED = 0x00080000;
    private const int WS_BORDER = 0x00800000;
    private const int WS_CAPTION = 0x00C00000;
    private const int SWP_SHOWWINDOW = 0x0040;
    private const int LWA_COLORKEY = 0x00000001;
    private const int LWA_ALPHA = 0x00000002;
    private const int WS_EX_TRANSPARENT = 0x20;
    //
    private const int ULW_COLORKEY = 0x00000001;
    private const int ULW_ALPHA = 0x00000002;
    private const int ULW_OPAQUE = 0x00000004;
    private const int ULW_EX_NORESIZE = 0x00000008;
    #endregion
    //
    public string strProduct;//项目名称
    public enumWinStyle WinStyle = enumWinStyle.WinTop;//窗体样式
    //
    public int ResWidth;//窗口宽度
    public int ResHeight;//窗口高度
    //
    public int currentX;//窗口左上角坐标x
    public int currentY;//窗口左上角坐标y
    //
    private bool isApha;//是否透明
    private bool isAphaPenetrate;//是否要穿透窗体
    // Use this for initialization
    void Awake()
    {
        Screen.fullScreen = false;
#if UNITY_EDITOR
        print("编辑模式不更改窗体");
#else
        switch (WinStyle)
        {
            case enumWinStyle.WinTop:
                isApha = false;
                isAphaPenetrate = false;
                break;
            case enumWinStyle.WinTopApha:
                isApha = true;
                isAphaPenetrate = false;
                break;
            case enumWinStyle.WinTopAphaPenetrate:
                isApha = true;
                isAphaPenetrate = true;
                break;
        }
        //获得窗口句柄
        IntPtr hwnd = FindWindow(null, strProduct);
        //
        if (isApha)
        {
            //去边框并且透明
            SetWindowLong(hwnd, GWL_EXSTYLE, WS_EX_LAYERED);
			// 获得当前样式
            int intExTemp = GetWindowLong(hwnd, GWL_EXSTYLE);
            if (isAphaPenetrate)//是否透明穿透窗体
            {
                SetWindowLong(hwnd, GWL_EXSTYLE, intExTemp | WS_EX_TRANSPARENT | WS_EX_LAYERED);
            }
            //
            SetWindowLong(hwnd, GWL_STYLE, GetWindowLong(hwnd, GWL_STYLE) & ~WS_BORDER & ~WS_CAPTION); //无边框、无标题栏
            SetWindowPos(hwnd, -1, currentX, currentY, ResWidth, ResHeight, SWP_SHOWWINDOW);
            var margins = new MARGINS() { cxLeftWidth = -1 };// 边距内嵌值确定在窗口四侧扩展框架的距离 -1为没有窗口边框
            //
            DwmExtendFrameIntoClientArea(hwnd, ref margins);
			
			SetLayeredWindowAttributes(hwnd, 0, 255, 1);
        }
        else
        {
            //单纯去边框
            SetWindowLong(hwnd, GWL_STYLE, WS_POPUP);
            SetWindowPos(hwnd, -1, currentX, currentY, ResWidth, ResHeight, SWP_SHOWWINDOW);
        }
#endif
    }
}

 随便扔到哪,我是扔到摄像机上了,说下参数填写,见下图1图2

挂上脚本后,strproduct要和图2处的ProductName填写的一样,老版本的unity导出的exe名字也要一样,,新版的unity只要选择存放的文件夹,名字不用管。

 下面是效果

普通的

unity 发布exe透明穿透(普通)

 HDRP的

unity 发布exe透明穿透(HDRP)

  • 10
    点赞
  • 27
    收藏
    觉得还不错? 一键收藏
  • 6
    评论
### 回答1: 要在Unity3D中实现窗体透明,可以按照以下步骤进行操作: 首先,确保你正在使用Unity3D的最新版本。然后,创建一个新的项目或打开现有的项目。 在Unity编辑器中,点击"Window"(窗口)选项卡,然后选择"Package Manager"(管理器)。在管理器中,确保你已经安装了"Universal Windows Platform"(通用Windows平台)模块。 接下来,点击"Window"(窗口)选项卡,然后选择"Project Settings"(项目设置)。在项目设置窗口中,选择"Player"(玩家)选项卡,然后点击"XR Settings"(XR设置)下拉菜单。 在XR设置下拉菜单中,选择"Player"(玩家)选项卡,并将"Virtual Reality Supported"(支持虚拟现实)选项设置为关闭。 然后,在"Player"(玩家)选项卡中,点击"Resolution and Presentation"(分辨率和演示)部分的"Run In Background"(在后台运行)复选框,以允许Unity应用在失去焦点时继续运行。 现在,你可以编写自己的代码来实现窗体透明。例如,你可以在脚本中使用"Color"(颜色)类型来设置窗体的背景色,并将Alpha通道值设置为0,以实现全透明效果。 请注意,实现窗体透明可能只在Windows平台有效。在其他平台上,窗体透明可能会有不同的实现方法。 希望这些步骤可以帮助你在Unity3D中实现窗体透明。祝你好运! ### 回答2: 要在Unity3D中实现窗体透明,可以按照以下步骤进行操作: 首先,在Unity3D中创建一个新项目或者打开现有项目。 然后,在Hierarchy视图中创建一个新的空对象(GameObject)。 接下来,在Inspector视图中将空对象的名称设置为"TransparentWindow"。 然后,为了将Unity项目转换为窗体应用程序,需要使用Unity的Build Settings功能。打开菜单栏中的File,选择Build Settings选项。 在Build Settings窗口中,选择目标平台为Windows(或者根据需要选择其他平台)。 点击Player Settings,在Player窗口的Resolution and Presentation选项中,取消勾选Default Is Full Screen。 然后,在Resolution and Presentation选项中取消勾选Run in Background和Fullscreen Window等选项。 接下来,在Player窗口的Other Settings选项卡中,选择Allow Title Bar to Display和Custom Title Bar Style等选项。 然后,在Unity项目的Assets文件夹中创建一个新的C#脚本。 在脚本中,使用System.Runtime.InteropServices命名空间中的[DllImport]特性导入Windows API函数。例如: [DllImport("user32.dll")] public static extern int SetWindowLong(IntPtr hWnd, int nIndex, int dwNewLong); 设置窗体参数函数如下: const int GWL_STYLE = -16; const int WS_POPUP = 0x80000000; const int WS_VISIBLE = 0x10000000; 然后,在脚本的Start函数中,获取Unity Player的窗体句柄并调用SetWindowLong函数来设置窗体样式,使其透明: IntPtr hwnd = GetActiveWindow(); SetWindowLong(hwnd, GWL_STYLE, WS_POPUP | WS_VISIBLE); 最后,将该脚本附加到在Hierarchy视图中创建的"TransparentWindow"对象上。 保存并运行Unity项目,将生成的可执行文件(.exe)作为窗体应用程序打开,应该能够看到透明的窗体。 这些步骤将帮助你在Unity3D中实现窗体透明。记得在操作之前备份项目文件,以防发生意外。
评论 6
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值