Skip to content

Instantly share code, notes, and snippets.

@Thaina
Last active March 18, 2022 05:42

Revisions

  1. Thaina revised this gist Mar 18, 2022. 2 changed files with 2 additions and 2 deletions.
    2 changes: 1 addition & 1 deletion Assets\Editor\TransparentWindowSetting.cs
    Original file line number Diff line number Diff line change
    @@ -1,4 +1,4 @@
    // origin : https://www.youtube.com/watch?v=RqgsGaMPZTw
    // origin : Transparent Unity App! : Code Monkey : https://www.youtube.com/watch?v=RqgsGaMPZTw

    using UnityEngine;
    using UnityEditor.Build;
    2 changes: 1 addition & 1 deletion Assets\Script\TransparentWindow.cs
    Original file line number Diff line number Diff line change
    @@ -1,4 +1,4 @@
    // origin : https://www.youtube.com/watch?v=RqgsGaMPZTw
    // origin : Transparent Unity App! : Code Monkey : https://www.youtube.com/watch?v=RqgsGaMPZTw

    using System;
    using System.Runtime.InteropServices;
  2. Thaina revised this gist Mar 18, 2022. 2 changed files with 4 additions and 0 deletions.
    2 changes: 2 additions & 0 deletions Assets\Editor\TransparentWindowSetting.cs
    Original file line number Diff line number Diff line change
    @@ -1,3 +1,5 @@
    // origin : https://www.youtube.com/watch?v=RqgsGaMPZTw

    using UnityEngine;
    using UnityEditor.Build;
    using UnityEditor.Build.Reporting;
    2 changes: 2 additions & 0 deletions Assets\Script\TransparentWindow.cs
    Original file line number Diff line number Diff line change
    @@ -1,3 +1,5 @@
    // origin : https://www.youtube.com/watch?v=RqgsGaMPZTw

    using System;
    using System.Runtime.InteropServices;

  3. Thaina created this gist Mar 18, 2022.
    17 changes: 17 additions & 0 deletions Assets\Editor\TransparentWindowSetting.cs
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,17 @@
    using UnityEngine;
    using UnityEditor.Build;
    using UnityEditor.Build.Reporting;

    public class TransparentWindowSetting : IPreprocessBuildWithReport
    {
    public int callbackOrder => 0;

    public void OnPreprocessBuild(BuildReport report)
    {
    #if UNITY_STANDALONE_WIN
    UnityEditor.PlayerSettings.fullScreenMode = FullScreenMode.FullScreenWindow;
    UnityEditor.PlayerSettings.useFlipModelSwapchain = false;
    UnityEditor.PlayerSettings.runInBackground = true;
    #endif
    }
    }
    67 changes: 67 additions & 0 deletions Assets\Script\TransparentWindow.cs
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,67 @@
    using System;
    using System.Runtime.InteropServices;

    using UnityEngine;

    public static class TransparentWindow
    {
    #if UNITY_STANDALONE_WIN
    private struct MARGINS
    {
    public int cxLeftWidth;
    public int cxRightWidth;
    public int cyTopHeight;
    public int cyBottomHeight;
    }

    [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", EntryPoint = "GetSystemMetrics")]
    private static extern int GetSystemMetrics(int nIndex);

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

    [DllImport("user32.dll",EntryPoint = "SetWindowPos")]
    private static extern int SetWindowPos(IntPtr hwnd,int hwndInsertAfter,int x,int y,int cx,int cy,int uFlags);

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

    const int GWL_STYLE = -16;
    const int GWL_EXSTYLE = -20;
    const uint WS_POPUP = 0x80000000;
    const uint WS_VISIBLE = 0x10000000;
    const uint WS_EX_LAYERED = 0x00080000;
    const int HWND_TOPMOST = -1;

    [RuntimeInitializeOnLoadMethod]
    public static void Init()
    {
    Camera.main.clearFlags = CameraClearFlags.SolidColor;
    Camera.main.backgroundColor = new Color(0,0,0,0);

    #if !UNITY_EDITOR // You really don't want to enable this in the editor..
    SetWindowTransparent();
    #endif
    }

    static void SetWindowTransparent()
    {
    int fWidth = Screen.width;
    int fHeight = Screen.height;
    var hwnd = GetActiveWindow();

    // Transparent windows with click through
    var margins = new MARGINS() { cxLeftWidth = -1 };
    DwmExtendFrameIntoClientArea(hwnd, ref margins);
    SetWindowLong(hwnd,GWL_EXSTYLE,WS_EX_LAYERED);
    SetLayeredWindowAttributes(hwnd, 0, 0, 1);
    SetWindowPos(hwnd,HWND_TOPMOST,0,0,0,0,0);
    }
    #endif
    }