一、全屏的跨屏(可以是两个屏扩展成一个屏,也可以三个屏扩展成一个屏):
一个canvas
using System;
using System.Collections;
using System.Runtime.InteropServices;
using System.Diagnostics;
using UnityEngine;
using System.Xml.Serialization;
public class NewBehaviourScript : MonoBehaviour
{
[DllImport("user32.dll")]
static extern IntPtr SetWindowLong(IntPtr hwnd, int _nIndex, int dwNewLong);
//导入设置窗口函数
[DllImport("user32.dll")]
public static extern bool SetWindowPos(System.IntPtr hWnd, int hWndInsertAfter, int X, int Y, int cx, int cy, uint uFlags);
//导入当前活动窗口
[DllImport("user32.dll")]
static extern IntPtr GetForegroundWindow();
[DllImport("user32.dll")]
public static extern IntPtr GetActiveWindow();
//显示窗口
public const uint SWP_SHOWWINDOW = 0x0040;
const int GWL_STYLE = -16;
const int WS_BORDER = 1;
void Start()
{
//最后一个参数必须为false,不能直接全屏
Screen.SetResolution(1080, 3840, false);
//隐藏标题栏
SetWindowLong(GetForegroundWindow(), GWL_STYLE, WS_BORDER);
SetWindowPos(GetActiveWindow(), -1, 0, 0, 1080, 3840, SWP_SHOWWINDOW);
}
void Awake()
{
for (int i = 0; i < Display.displays.Length; i++)
{
Display.displays[i].Activate();
}
}
// Update is called once per frame
void Update()
{
}
}
二、非全屏的跨屏:
using System;
using System.Collections;
using System.Runtime.InteropServices;
using UnityEngine;
public class SetWindow : MonoBehaviour
{
[DllImport("user32.dll")]
static extern IntPtr SetWindowLong(IntPtr hwnd, int _nIndex, int dwNewLong);
[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 GetForegroundWindow();
const uint SWP_SHOWWINDOW = 0x0040;
const int GWL_STYLE = -16; //边框用的
const int WS_BORDER = 1;
const int WS_POPUP = 0x800000;
//ShowWindow参数
public const int SW_FORCEMINIMIZE = 11; //在WindowNT5.0中最小化窗口,即使拥有窗口的线程被挂起也会最小化。在从其他线程最小化窗口时才使用这个参数。
public const int SW_HIDE = 0; //隐藏窗口并激活其他窗口
public const int SW_MAXIMIZE = 3; //最大化指定的窗口
public const int SW_MINIMIZE = 6; //最小化指定的窗口并且激活在Z序中的下一个顶层窗口。
public const int SW_RESTORE = 9; //激活并显示窗口。如果窗口最小化或最大化,则系统将窗口恢复到原来的尺寸和位置。在恢复最小化窗口时,应用程序应该指定这个标志。
public const int SW_SHOW = 5; //在窗口原来的位置以原来的尺寸激活和显示窗口
public const int SW_SHOWDEFAULT = 10; //依据在STARTUPINFO结构中指定的SW_FLAG标志设定显示状态,STARTUPINFO 结构是由启动应用程序的程序传递给CreateProcess函数的。nCmdShow=10。
public const int SW_SHOWMAXIMIZED = 3; //激活窗口并将其最大化。nCmdShow=3。
public const int SW_SHOWMINIMIZED = 2; //激活窗口并将其最小化。nCmdShow=2。
public const int SW_SHOWMINNOACTIVE = 7; //窗口最小化,激活窗口仍然维持激活状态。nCmdShow=7。
public const int SW_SHOWNA = 8; //以窗口原来的状态显示窗口。激活窗口仍然维持激活状态。nCmdShow=8。
public const int SW_SHOWNOACTIVATE = 4; //以窗口最近一次的大小和状态显示窗口。激活窗口仍然维持激活状态。nCmdShow=4。
public const int SW_SHOWNORMAL = 1; //激活并显示一个窗口。如果窗口被最小化或最大化,系统将其恢复到原来的尺寸和大小。应用程序在第一次显示窗口的时候应该指定此标志。nCmdShow=1。
const int SW_SHOWRESTORE = 1;
//SetWindowLong参数
public const int GWL_EXSTYLE = -20; //获得扩展窗口风格。
public const int GWL_HINSTANCE = -6; //获得应用实例的句柄。。
public const int GWL_HWNDPARENT = -8; //如果父窗口存在,获得父窗口句柄。
public const int GWL_ID = -12; //获得窗口标识。
//public const int GWL_STYLE = -16; //获得窗口风格。
public const int GWL_USERDATA = -21; //获得与窗口有关的32位值。每一个窗口均有一个由创建该窗口的应用程序使用的32位值。。
public const int GWL_WNDPROC = -4; //获得窗口过程的地址,或代表窗口过程的地址的句柄。必须使用CallWindowProc函数调用窗口过程。
//窗口位置
int _posX = 00;
int _posY = 00;
// 在这里设置你想要的窗口宽
int _Txtwith = 5182;
// 在这里设置你想要的窗口高
int _Txtheight = 1200;
private void Awake()
{
SetWindowLong(GetForegroundWindow(), GWL_STYLE, WS_POPUP); //无边框
bool result = SetWindowPos(GetForegroundWindow(), 0, _posX, _posY, _Txtwith, _Txtheight, SWP_SHOWWINDOW);//设置屏幕大小和位置
Screen.fullScreen = false;
}
private void Start()
{
}
}
注意:有时候跨屏第一次运行程序显示异常,等第二次再运行程序就显示正常了