Skip to content

Instantly share code, notes, and snippets.

@the6th
Last active January 3, 2021 03:56
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save the6th/5d6b90ba82304ba64171462635a1bc49 to your computer and use it in GitHub Desktop.
Save the6th/5d6b90ba82304ba64171462635a1bc49 to your computer and use it in GitHub Desktop.
Unityでマルチディスプレイ対応/メインディスプレイはWindow表示でResize可能
using UnityEngine;
public class DisplayScript : MonoBehaviour
{
void Start()
{
Debug.Log("displays connected: " + Display.displays.Length);
Display.displays[0].Activate();
Display.displays[1].Activate();
////Secondary Displayの解像度設定(タイトルバー隠す)
Invoke("DelayWindow", 0f);
}
private void DelayWindow()
{
WindowController.windowReplace(Application.productName, 100, 100, 640, 480, false);
WindowController.windowReplace("Unity Secondary Display", 2560, 0, 3840, 2160, true);
}
}
using System;
using System.Runtime.InteropServices;
public static class WindowController
{
[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", EntryPoint = "FindWindow")]
public static extern IntPtr FindWindow(System.String className, System.String windowName);
// Sets window attributes
[DllImport("user32.dll")]
public static extern int SetWindowLong(IntPtr hWnd, int nIndex, int dwNewLong);
// Gets window attributes
[DllImport("user32.dll")]
public static extern int GetWindowLong(IntPtr hWnd, int nIndex);
// assorted constants needed
public static int GWL_STYLE = -16;
public static int WS_CHILD = 0x40000000; //child window
public static int WS_BORDER = 0x00800000; //window with border
public static int WS_DLGFRAME = 0x00400000; //window with double border but no title
public static int WS_SIZEBOX = 0x00040000; //window with double border but no title
public static int WS_SYSMENU = 0x00080000; //window with double border but no title
public static int WS_CAPTION = WS_BORDER | WS_DLGFRAME; //window with a title bar
public static void windowReplace(string name, int x, int y, int width, int height, bool hideTitleBar)
{
var window = FindWindow(null, name);
if (hideTitleBar)
{
int style = GetWindowLong(window, GWL_STYLE);
SetWindowLong(window, GWL_STYLE, (style & ~WS_CAPTION));
}
else
{
int style = GetWindowLong(window, GWL_STYLE);
style |= WS_CAPTION ; //show title bar
style |= WS_SIZEBOX; //Resizable window
style |= WS_SYSMENU; //Show Close button
SetWindowLong(window, GWL_STYLE, style);
}
SetWindowPos(window, 0, x, y, width, height, width * height == 0 ? 1 : 0);
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment