Skip to content

Instantly share code, notes, and snippets.

@Thaina
Last active March 18, 2022 05:42
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save Thaina/8ee6b6a3129f0d1b57c69f96c1146c2d to your computer and use it in GitHub Desktop.
Save Thaina/8ee6b6a3129f0d1b57c69f96c1146c2d to your computer and use it in GitHub Desktop.
Unity Setting for transparent background in StandAlone Windows
// origin : Transparent Unity App! : Code Monkey : https://www.youtube.com/watch?v=RqgsGaMPZTw
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
}
}
// origin : Transparent Unity App! : Code Monkey : https://www.youtube.com/watch?v=RqgsGaMPZTw
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
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment