Unity 设置窗体透明

设置窗口透明、窗口置顶、鼠标穿透

   

方法一、

缺点:边缘不平滑,有毛边

参考博客:

1、https://alastaira.wordpress.com/2015/06/15/creating-windowless-unity-applications/

2、http://www.manew.com/thread-43230-1-1.html

3、https://blog.csdn.net/dark00800/article/details/70314432

关键代码

Shader "Custom/ChromakeyTransparent" {
    Properties {
        _MainTex ("Base (RGB)", 2D) = "white" {}
        _TransparentColourKey ("Transparent Colour Key", Color) = (0,0,0,1)
        _TransparencyTolerance ("Transparency Tolerance", Float) = 0.01 
    }
    SubShader {
        Pass {
            Tags { "RenderType" = "Opaque" }
            LOD 200
         
            CGPROGRAM
 
            #pragma vertex vert
            #pragma fragment frag
            #include "UnityCG.cginc"
 
            struct a2v
            {
                float4 pos : POSITION;
                float2 uv : TEXCOORD0;
            };
 
            struct v2f
            {
                float4 pos : SV_POSITION;
                float2 uv : TEXCOORD0;
            };
 
            v2f vert(a2v input)
            {
                v2f output;
                output.pos = mul (UNITY_MATRIX_MVP, input.pos);
                output.uv = input.uv;
                return output;
            }
         
            sampler2D _MainTex;
            float3 _TransparentColourKey;
            float _TransparencyTolerance;
 
            float4 frag(v2f input) : SV_Target
            {
                // What is the colour that *would* be rendered here?
                float4 colour = tex2D(_MainTex, input.uv);
             
                // Calculate the different in each component from the chosen transparency colour
                float deltaR = abs(colour.r - _TransparentColourKey.r);
                float deltaG = abs(colour.g - _TransparentColourKey.g);
                float deltaB = abs(colour.b - _TransparentColourKey.b);
 
                // If colour is within tolerance, write a transparent pixel
                if (deltaR < _TransparencyTolerance && deltaG < _TransparencyTolerance && deltaB < _TransparencyTolerance)
                {
                    return float4(0.0f, 0.0f, 0.0f, 0.0f);
                }
 
                // Otherwise, return the regular colour
                return colour;
            }
            ENDCG
        }
    }
}
using System;
using System.Runtime.InteropServices;
using UnityEngine;
 
public class TransparentWindow : MonoBehaviour
{
    [SerializeField]
    private Material m_Material;
 
    private struct MARGINS
    {
        public int cxLeftWidth;
        public int cxRightWidth;
        public int cyTopHeight;
        public int cyBottomHeight;
    }
 
    // Define function signatures to import from Windows APIs
 
    [DllImport("user32.dll")]
    private static extern IntPtr GetActiveWindow();
 
    [DllImport("user32.dll")]
    private static extern int SetWindowLong(IntPtr hWnd, int nIndex, uint dwNewLong);
     
    [DllImport("Dwmapi.dll")]
    private static extern uint DwmExtendFrameIntoClientArea(IntPtr hWnd, ref MARGINS margins);
 
 
    // Definitions of window styles
    const int GWL_STYLE = -16;
    const uint WS_POPUP = 0x80000000;
    const uint WS_VISIBLE = 0x10000000;
 
    void Start()
    {
        #if !UNITY_EDITOR
        var margins = new MARGINS() { cxLeftWidth = -1 };
 
        // Get a handle to the window
        var hwnd = GetActiveWindow();
 
        // Set properties of the window
        // See: https://msdn.microsoft.com/en-us/library/windows/desktop/ms633591%28v=vs.85%29.aspx
        SetWindowLong(hwnd, GWL_STYLE, WS_POPUP | WS_VISIBLE);
         
        // Extend the window into the client area
        See: https://msdn.microsoft.com/en-us/library/windows/desktop/aa969512%28v=vs.85%29.aspx 
        DwmExtendFrameIntoClientArea(hwnd, ref margins);
        #endif
    }
 
    // Pass the output of the camera to the custom material
    // for chroma replacement
    void OnRenderImage(RenderTexture from, RenderTexture to)
    {
        Graphics.Blit(from, to, m_Material);
    }
}

下面是改进版,添加窗口置顶和鼠标穿透,不过依然存在毛边现象,shader还是用上面的

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

 
public class TransparentWindow :MonoBehaviour
{

    [SerializeField]
    public Material m_Material;

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

    // Define function signatures to import from Windows APIs

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

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

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

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

    /// <summary>   
    /// 窗口置顶
    /// </summary>   
    /// <returns></returns>
    [DllImport("user32.dll")]
    private static extern int SetWindowPos(IntPtr hWnd, int hWndInsertAfter, int x, int y, int Width, int Height, int flags);
    /// <summary>   
    /// 得到当前活动的窗口   
    /// </summary>   
    /// <returns></returns>   
    [DllImport("user32.dll")]
    private static extern IntPtr GetForegroundWindow();


   

const uint LWA_COLORKEY = 0x1;
    // Definitions of window styles
    const int GWL_STYLE = -16;
    const uint WS_POPUP = 0x80000000;
    const uint WS_VISIBLE = 0x10000000;

   

    private const uint WS_EX_LAYERED = 0x80000;
    private const int WS_EX_TRANSPARENT = 0x20;
 
    private const int GWL_EXSTYLE = (-20);
    private const int LWA_ALPHA = 0x2;
    IntPtr hwnd;
    void Start()
    {
        Application.runInBackground=true;
       var margins = new MARGINS() { cxLeftWidth = -
  • 1
    点赞
  • 12
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
### 回答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中实现窗体透明。记得在操作之前备份项目文件,以防发生意外。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值