Search Unity

  1. We are migrating the Unity Forums to Unity Discussions. On July 12, the Unity Forums will become read-only. On July 15, Unity Discussions will become read-only until July 18, when the new design and the migrated forum contents will go live. Read our full announcement for more information and let us know if you have any questions.

Resolved [SOLVED] Transparent window in Unity 2020

Discussion in 'Windows' started by cadynbombaci, Oct 24, 2021.

  1. cadynbombaci

    cadynbombaci

    Joined:
    Oct 10, 2017
    Posts:
    10
    Thread that works up to Unity 2018: https://forum.unity.com/threads/sol...dow-with-opaque-contents-lwa_colorkey.323057/

    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.
     
  2. cadynbombaci

    cadynbombaci

    Joined:
    Oct 10, 2017
    Posts:
    10
    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!)"
     
  3. mrmattbett

    mrmattbett

    Joined:
    Jun 15, 2020
    Posts:
    2
    I'll just make this my first Godot project, as it is much simpler there...
     
  4. bjoe3041

    bjoe3041

    Joined:
    Mar 25, 2019
    Posts:
    1
    Check if DXGI flip model swapchain is enabled, the winapi transparency hack probably wont work if it is
    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.

    Code (CSharp):
    1. using System;
    2. using System.Collections;
    3. using System.Collections.Generic;
    4. using System.Runtime.InteropServices;
    5. using UnityEngine;
    6.  
    7.  
    8. public class TransparancyManager : MonoBehaviour
    9. {
    10. #if !UNITY_EDITOR
    11.     [DllImport("user32.dll")]
    12.     static extern IntPtr GetActiveWindow();
    13.  
    14.     [DllImport("user32.dll")]
    15.     static extern int SetWindowLong(IntPtr hWnd, int nIndex, int dwNewLong);
    16.  
    17.     [DllImport("user32.dll", SetLastError = true)]
    18.     static extern int GetWindowLong(IntPtr hWnd, int nIndex);
    19.  
    20.     [DllImport("user32.dll")]
    21.     static extern bool SetLayeredWindowAttributes(IntPtr hwnd, uint crKey, byte bAlpha, uint dwFlags);
    22.  
    23.     const int GWL_EXSTYLE = -20;
    24.     const int WS_EX_LAYERED = 0x80000;
    25.     // Remove WS_EX_TRANSPARENT to allow window interaction
    26.     const int LWA_COLORKEY = 0x1;
    27.     const int LWA_ALPHA = 0x2;
    28. #endif
    29.  
    30.     void Start()
    31.     {
    32. #if !UNITY_EDITOR
    33.         IntPtr hwnd = GetActiveWindow();
    34.         // Make window layered but not input-transparent
    35.         SetWindowLong(hwnd, GWL_EXSTYLE, GetWindowLong(hwnd, GWL_EXSTYLE) | WS_EX_LAYERED);
    36.  
    37.         // Adjust color key and alpha for selective transparency
    38.         SetLayeredWindowAttributes(hwnd, 0, 255, LWA_COLORKEY); // You might not need LWA_COLORKEY
    39. #endif
    40.     }
    41. }
    42.