Last active
September 17, 2020 23:54
-
-
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 :(.
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 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