Created
January 13, 2022 06:08
-
-
Save k4sima/b3d4319f4fbbd1b9c3d467de9c235e14 to your computer and use it in GitHub Desktop.
Hide title bar for standalone of Unity
This file contains 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
using System; | |
using System.Runtime.InteropServices; | |
using UnityEngine; | |
namespace Utils | |
{ | |
public class HideTitleBar : MonoBehaviour | |
{ | |
public static int GWL_WNDPROC = -4; | |
public static int GWL_STYLE = -16; | |
public static int WS_CHILD = 0x40000000; | |
public static int WS_BORDER = 0x00800000; | |
public static int WS_DLGFRAME = 0x00400000; | |
public static int WS_CAPTION = WS_BORDER | WS_DLGFRAME; | |
public static uint WM_NCCALCSIZE = 0x0083; | |
private Func<IntPtr, uint, IntPtr, IntPtr, IntPtr> customWndProcDelegate; | |
private IntPtr defaultWndProcPtr; | |
private void Awake() | |
{ | |
#if UNITY_STANDALONE_WIN && !UNITY_EDITOR | |
var window = GetActiveWindow(); | |
var style = GetWindowLong(window, GWL_STYLE); | |
SetWindowLong(window, GWL_STYLE, style & ~WS_CAPTION); | |
this.customWndProcDelegate = this.CustomWndProc; | |
this.defaultWndProcPtr = SetWindowLongPtr( | |
window, | |
GWL_WNDPROC, | |
Marshal.GetFunctionPointerForDelegate(this.customWndProcDelegate)); | |
SetWindowPos( | |
window, | |
0); | |
#endif | |
} | |
#if UNITY_STANDALONE_WIN && !UNITY_EDITOR | |
private IntPtr CustomWndProc(IntPtr hWnd, uint msg, IntPtr wParam, IntPtr lParam) | |
{ | |
var result = CallWindowProc(this.defaultWndProcPtr, hWnd, msg, wParam, lParam); | |
if ((msg == WM_NCCALCSIZE) && ((int)wParam == 1)) | |
{ | |
var ncCalcSizeParams = Marshal.PtrToStructure<NCCALCSIZE_PARAMS>(lParam); | |
ncCalcSizeParams.rgrc[0].Top -= 6; | |
Marshal.StructureToPtr(ncCalcSizeParams, lParam, false); | |
} | |
return result; | |
} | |
#endif | |
[StructLayout(LayoutKind.Sequential)] | |
public struct RECT | |
{ | |
public int Left, Top, Right, Bottom; | |
} | |
[StructLayout(LayoutKind.Sequential)] | |
public struct WINDOWPOS | |
{ | |
public IntPtr hwnd; | |
public IntPtr hwndInsertAfter; | |
} | |
[StructLayout(LayoutKind.Sequential)] | |
private struct NCCALCSIZE_PARAMS | |
{ | |
[MarshalAs(UnmanagedType.ByValArray, SizeConst = 3)] | |
public readonly RECT[] rgrc; | |
public WINDOWPOS lppos; | |
} | |
#if UNITY_STANDALONE_WIN && !UNITY_EDITOR | |
[DllImport("user32.dll", EntryPoint = "SetWindowPos")] | |
private static extern bool SetWindowPos( | |
int hWnd, | |
int hWndInsertAfter); | |
[DllImport("user32.dll")] | |
static extern int GetActiveWindow(); | |
[DllImport("user32.dll")] | |
public static extern int SetWindowLong(int hWnd, int nIndex, int dwNewLong); | |
[DllImport("user32.dll")] | |
public static extern int GetWindowLong(int hWnd, int nIndex); | |
[DllImport("user32.dll")] | |
public static extern IntPtr SetWindowLongPtr(int hWnd, int nIndex, IntPtr dwNewLong); | |
[DllImport("user32.dll")] | |
private static extern IntPtr GetWindowLongPtr(int hWnd, int nIndex); | |
[DllImport("user32.dll")] | |
private static extern IntPtr CallWindowProc( | |
IntPtr lpPrevWndFunc, | |
IntPtr hWnd, | |
uint Msg, | |
IntPtr wParam, | |
IntPtr lParam); | |
#endif | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment