[SOLVED] Transparent window in Unity 2020

Thread that works up to Unity 2018: [SOLVED!] Windows: Transparent window with opaque contents (LWA_COLORKEY)?

I’ve been at this for a while now, feel like I’ve tried a million different transparency scripts. Eventually I got tired and made a new project in 2018 just to test it, and discovered that they all worked in 2018. Then, I upgraded the project from 2018 to 2019 LTS, and of course it doesn’t work anymore, the background is just black at that point. Downgrading my current project from 2020 to 2019 may have been feasible, but from 2020 to 2018 simply isn’t. I tried and it simply broke everything.

If there’s a way to get this working in 2020, please let me know. Otherwise, could Unity maybe fix this functionality, so that people who need it aren’t stuck in the 2018 version.

Ofcourse, moments after posting this, I discovered that I hadn’t checked the second page of the thread yet where a solution is described. For anyone else stumbling across this same issue who hasn’t dug through the thread yet, I will paste the reply from Vestigial, “Per Tautvydas-Zilys, you have to make sure to UNCHECK “Use DXGI Flip Model Swapchain for D3D11” under Player Settings → Resolution and Presentation. (I also had to tweak the shader to use alpha instead of color key, and havent tried clickthrough yet but the transparency is there, or NOT there I guess!)”

1 Like

I’ll just make this my first Godot project, as it is much simpler there…

Check if DXGI flip model swapchain is enabled, the winapi transparency hack probably wont work if it is
9680912--1380365--upload_2024-3-5_9-43-44.png
You can find it under Edit > Project settings > Player > Resolution and Presentation.
It fixed it for me in Unity 2022.3.20f1 DX11 URP, tested with a super minimal transparency script.
I also sat the cameras background type to solid color, with an alpha of zero.

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


public class TransparancyManager : MonoBehaviour
{
#if !UNITY_EDITOR
    [DllImport("user32.dll")]
    static extern IntPtr GetActiveWindow();

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

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

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

    const int GWL_EXSTYLE = -20;
    const int WS_EX_LAYERED = 0x80000;
    // Remove WS_EX_TRANSPARENT to allow window interaction
    const int LWA_COLORKEY = 0x1;
    const int LWA_ALPHA = 0x2;
#endif

    void Start()
    {
#if !UNITY_EDITOR
        IntPtr hwnd = GetActiveWindow();
        // Make window layered but not input-transparent
        SetWindowLong(hwnd, GWL_EXSTYLE, GetWindowLong(hwnd, GWL_EXSTYLE) | WS_EX_LAYERED);

        // Adjust color key and alpha for selective transparency
        SetLayeredWindowAttributes(hwnd, 0, 255, LWA_COLORKEY); // You might not need LWA_COLORKEY
#endif
    }
}
1 Like

Worked for me in Unity 2022.3.10f1 DX11 !
Thanks a lot, what a life savior!!

However, I am also curious about why I can’t make it work with SetLayeredWindowAttributes(hwnd, 0, 255, LWA_ALPHA) ?
Only works with SetLayeredWindowAttributes(hwnd, 0, 255, LWA_COLORKEY).