1.需求:一台电脑上链接着两个显示设备A,B,unity程序运行时,自动在第二块屏上B显示(unity3D默认是在主屏上显示的)
2.情况一:两块屏的分辨率相同,如A:1920x1080 B:1920x1080,可在主相机中挂如下脚本:
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using System;
using System.Runtime.InteropServices;
public class WindowsShow : MonoBehaviour
{
[HideInInspector]
//导入设置窗口函数
[DllImport("user32.dll")]
static extern bool SetWindowPos(IntPtr hWnd, int hWndInsertAfter, int X, int Y, int cx, int cy, uint uFlags);
//导入当前活动窗口
[DllImport("user32.dll")]
static extern IntPtr GetActiveWindow();
const uint SWP_SHOWWINDOW = 0x0040;//显示窗口
void Start()
{
SetWindowPos(GetActiveWindow(), -1, 1920, 0, 1920,1080, SWP_SHOWWINDOW);
}
情况二:两块屏的分辨率不相同,A:1920x1080 B:1024x768,可在主相机中挂如下脚本:
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using System;
using System.Runtime.InteropServices;
public class WindowsShow : MonoBehaviour
{
[HideInInspector]
//导入设置窗口函数
[DllImport("user32.dll")]
static extern bool SetWindowPos(IntPtr hWnd, int hWndInsertAfter, int X, int Y, int cx, int cy, uint uFlags);
//导入当前活动窗口
[DllImport("user32.dll")]
static extern IntPtr GetActiveWindow();
//改变指定窗口的属性
[DllImport("user32.dll")]
static extern IntPtr SetWindowLong(IntPtr hwnd, int _nIndex, int dwNewLong);
const int GWL_STYLE = -16;
const int WS_BORDER = 1;
const uint SWP_SHOWWINDOW = 0x0040;//显示窗口
float m_timer = 0.0f;
bool m_init = false;
void Start()
{
SetWindowLong(GetActiveWindow(), GWL_STYLE, WS_BORDER);
SetWindowPos(GetActiveWindow(), -1, 1920, 0, 1024, 768, SWP_SHOWWINDOW);
}
void Update()
{
if(!m_init)
{
m_timer += Time.deltaTime;
if (m_timer > 5.0f)
{
m_init = true;
SetWindowLong(GetActiveWindow(), GWL_STYLE, WS_BORDER);
SetWindowPos(GetActiveWindow(), -1, 1920, 0, 1024, 768, SWP_SHOWWINDOW);
}
}
}
}
【说明】:
(1)如果在第二种情况下,使用情况一中的脚本,画面两边会有黑边出现,未满屏。
(2)如果有好的方法请告知。