Created
October 13, 2023 08:52
-
-
Save unitycoder/4cae17cb537cfb140e1cc08277ecf564 to your computer and use it in GitHub Desktop.
change unity window alpha
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| // https://yal.cc/unity3d-window-alpha/ | |
| using System; | |
| using System.Runtime.InteropServices; | |
| public class WindowAlpha { | |
| private static IntPtr __defaultWindow = IntPtr.Zero; | |
| private static IntPtr getDefaultWindow() { | |
| if (__defaultWindow == IntPtr.Zero) { | |
| #if (UNITY_EDITOR || UNITY_STANDALONE) | |
| __defaultWindow = GetActiveWindow(); | |
| #else | |
| __defaultWindow = System.Diagnostics.Process.GetCurrentProcess().MainWindowHandle; | |
| #endif | |
| } | |
| return __defaultWindow; | |
| } | |
| /// <summary>Retrieves opacity of a window</summary> | |
| /// <param name="hwnd">A window to apply to (optional - defaults to main window)</param> | |
| /// <returns>Window's 0...255 opacity, or -1 in case of error</returns> | |
| public static int Get(IntPtr hwnd = default) { | |
| if (hwnd == IntPtr.Zero) { | |
| hwnd = getDefaultWindow(); | |
| if (hwnd == IntPtr.Zero) return -1; | |
| } | |
| var style = GetWindowLongPtr(hwnd, GWL_EXSTYLE).ToInt64(); | |
| if ((style & WS_EX_LAYERED) == 0) return 255; // not semi-transparent | |
| uint crKey; byte bAlpha; uint dwFlags; | |
| if (!GetLayeredWindowAttributes(hwnd, out crKey, out bAlpha, out dwFlags)) return -1; | |
| return dwFlags == LWA_ALPHA ? bAlpha : 255; | |
| } | |
| /// <summary>Changes opacity of a window</summary> | |
| /// <param name="alpha">A value between 0 (transparent) to 255 (opaque)</param> | |
| /// <param name="hwnd">A window to apply to (optional - defaults to main window)</param> | |
| /// <returns>Whether the operation succeeded.</returns> | |
| public static bool Set(int alpha, IntPtr hwnd = default) { | |
| if (hwnd == IntPtr.Zero) { | |
| hwnd = getDefaultWindow(); | |
| if (hwnd == IntPtr.Zero) return false; | |
| } | |
| if (hwnd == IntPtr.Zero) return false; | |
| var style = GetWindowLongPtr(hwnd, GWL_EXSTYLE).ToInt64(); | |
| style |= WS_EX_LAYERED; | |
| SetWindowLongPtr(hwnd, GWL_EXSTYLE, (IntPtr)style); | |
| var alpha_b = (byte)(alpha < 0 ? 0 : (alpha > 255 ? 255 : alpha)); | |
| return SetLayeredWindowAttributes(hwnd, 0, alpha_b, LWA_ALPHA); | |
| } | |
| [DllImport("user32.dll", EntryPoint = "GetWindowLong")] | |
| private static extern IntPtr GetWindowLongPtr(IntPtr hWnd, int nIndex); | |
| private const int GWL_EXSTYLE = -20; | |
| private static IntPtr SetWindowLongPtr(IntPtr hWnd, int nIndex, IntPtr dwNewLong) { | |
| if (IntPtr.Size == 8) | |
| return SetWindowLongPtr64(hWnd, nIndex, dwNewLong); | |
| else | |
| return new IntPtr(SetWindowLong32(hWnd, nIndex, dwNewLong.ToInt32())); | |
| } | |
| private const int WS_EX_LAYERED = 0x00080000; | |
| [DllImport("user32.dll", EntryPoint = "SetWindowLong")] | |
| private static extern int SetWindowLong32(IntPtr hWnd, int nIndex, int dwNewLong); | |
| [DllImport("user32.dll", EntryPoint = "SetWindowLongPtr")] | |
| private static extern IntPtr SetWindowLongPtr64(IntPtr hWnd, int nIndex, IntPtr dwNewLong); | |
| [DllImport("user32.dll", SetLastError = true)] private static extern bool SetLayeredWindowAttributes(IntPtr hwnd, uint crKey, byte bAlpha, uint dwFlags); | |
| [DllImport("user32.dll", SetLastError = true)] static extern bool GetLayeredWindowAttributes(IntPtr hwnd, out uint crKey, out byte bAlpha, out uint dwFlags); | |
| private const int LWA_ALPHA = 0x00000002; | |
| [DllImport("user32.dll")] | |
| private static extern IntPtr GetActiveWindow(); | |
| } |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment