Skip to content

Instantly share code, notes, and snippets.

@kamyker
Created December 23, 2022 23:54
Show Gist options
  • Save kamyker/3b6945effec0c7d5c9c779974203ac32 to your computer and use it in GitHub Desktop.
Save kamyker/3b6945effec0c7d5c9c779974203ac32 to your computer and use it in GitHub Desktop.
Unity C# hide tool bar and menu bar for light panel in dark mode
#if UNITY_EDITOR
using System;
using System.Runtime.InteropServices;
using System.Text;
using UnityEditor;
using UnityEngine;
public static class HideTitleMenuBar
{
[DllImport("user32.dll")]
static extern IntPtr GetActiveWindow();
[DllImport("user32.dll")]
static extern IntPtr GetMenu(IntPtr hwnd);
[DllImport("user32.dll")]
static extern IntPtr SetMenu(IntPtr hwnd, IntPtr hMenu);
[DllImport("user32.dll", CharSet=CharSet.Unicode, SetLastError=true)]
static extern int GetWindowText(IntPtr hWnd, StringBuilder lpString, int nMaxCount);
[DllImport("user32.dll")]
static extern int GetWindowTextLength(IntPtr hWnd);
[DllImport("user32.dll")]
private static extern IntPtr GetWindowLongPtr(IntPtr hWnd, int nIndex);
[DllImport("user32.dll")]
private static extern IntPtr SetWindowLongPtr(IntPtr hWnd, int nIndex, IntPtr dwNewLong);
const string idLastMenuPtr = "ks_toggleTitleBar_lastMenuPtr";
const long titlebarStyle = 0x00C00000;
[MenuItem("Tools/Kamyker/Toggle title bar _1")]
static void ToggleTitleBar()
{
var unity = GetActiveWindow();
if(unity == IntPtr.Zero)
return;
int titleLength = GetWindowTextLength(unity);
var title = new StringBuilder(titleLength);
GetWindowText(unity, title, titleLength);
if(!title.ToString().Contains("unity", StringComparison.InvariantCultureIgnoreCase))
{
Debug.LogError($"Can't hide title bar of {title.ToString()}, change 'unity' in the code if it's translated differently");
return;
}
var menu = GetMenu(unity);
var style = GetWindowLongPtr(unity, -16);
if(menu != IntPtr.Zero)
{
PlayerPrefs.SetString(idLastMenuPtr, menu.ToString());
SetWindowLongPtr(unity, -16, new IntPtr(style.ToInt64() - titlebarStyle));
SetMenu(unity, IntPtr.Zero);
}
else
{
var lastMenuPtr = long.Parse(PlayerPrefs.GetString(idLastMenuPtr));
SetWindowLongPtr(unity, -16, new IntPtr(style.ToInt64() + titlebarStyle));
SetMenu(unity, new IntPtr(lastMenuPtr));
}
}
}
#endif
@kamyker
Copy link
Author

kamyker commented Dec 23, 2022

Set to shortcut '1'. You can change it at the end of "Tools/Kamyker/Toggle title bar _1".

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment