Skip to content

Instantly share code, notes, and snippets.

@Hotrian
Last active September 17, 2020 23:54
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save Hotrian/1b0512e9feed42085a11446d17c26a12 to your computer and use it in GitHub Desktop.
Save Hotrian/1b0512e9feed42085a11446d17c26a12 to your computer and use it in GitHub Desktop.
Takes a normal Unity Standalone Player Window and hides it off screen and from the Taskbar :). This was made because -batchmode seems to automatically call -nographics now, which breaks things :(.
using System;
using UnityEngine;
using System.Runtime.InteropServices;
public class HeadlessScript : MonoBehaviour
{
#if UNITY_STANDALONE_WIN && !UNITY_EDITOR
[DllImport("user32.dll", EntryPoint = "FindWindow", SetLastError = true)]
static extern IntPtr FindWindowByCaption(IntPtr zeroOnly, string lpWindowName);
[DllImport("user32.dll", EntryPoint = "SetWindowPos")]
private static extern bool SetWindowPos(IntPtr hwnd, int hWndInsertAfter, int x, int Y, int cx, int cy, int wFlags);
[DllImport("user32.dll")]
[return: MarshalAs(UnmanagedType.Bool)]
private static extern bool ShowWindow(IntPtr hWnd, uint nCmdShow);
[DllImport("user32.dll")]
static extern int SetWindowLong(IntPtr hWnd, int nIndex, int dwNewLong);
[DllImport("user32.dll", SetLastError = true)]
static extern int GetWindowLong(IntPtr hWnd, int nIndex);
private const int GWL_EXSTYLE = -0x14;
private const int WS_EX_TOOLWINDOW = 0x0080;
// Use this for initialization
void Start()
{
// Find the application's Window
var handle = FindWindowByCaption(IntPtr.Zero, Application.productName);
if (handle == IntPtr.Zero) return;
// Move the Window Off Screen
SetWindowPos(handle, 0, -720, 0, 720, 480, 0);
// Remove the Window from the Taskbar
ShowWindow(handle, (uint)0);
SetWindowLong(handle, GWL_EXSTYLE, GetWindowLong(handle, GWL_EXSTYLE) | WS_EX_TOOLWINDOW);
ShowWindow(handle, (uint)5);
}
#endif
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment