福感科技有限公司 欢迎您!
联系方式

    地址:北京市平谷区马坊镇金河北街17号院3号楼7层712

    电话:010-89968230

    网站:http://www.fgsense.com

如何使用Unity3D跨屏幕、全屏显示

2021-1-3 20:04:47      点击:

Unity3D跨屏幕、全屏显示方法,运行环境:Win10 64bit, Unity3D 2017.3.1

using System;
using System.Collections;
using System.Runtime.InteropServices;
using System.Diagnostics;
using UnityEngine;
using System.Xml.Serialization;

public class WindowMod : MonoBehaviour
{
    [HideInInspector]
    public Rect screenPosition;
    [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 GetActiveWindow();
    const uint SWP_SHOWWINDOW = 0x0040;
    const int GWL_STYLE = -16;
    const int WS_BORDER = 1;
    private int i = 0;

    void Start()
    {
        SetWindowLong(GetActiveWindow(), GWL_STYLE, WS_BORDER);
        SetWindowPos(GetActiveWindow(), -1, (int)screenPosition.x, (int)screenPosition.y, (int)screenPosition.width, (int)screenPosition.height, SWP_SHOWWINDOW);
    }

    void Update()
    {
        i++;
        if(i<5)
        {
            SetWindowLong(GetActiveWindow(), GWL_STYLE, WS_BORDER);
            SetWindowPos(GetActiveWindow(), -1, (int)screenPosition.x, (int)screenPosition.y, (int)screenPosition.width, (int)screenPosition.height, SWP_SHOWWINDOW);
        }
    }
}


上面这个脚本,可以使Unity3D窗口全屏,没有标题栏,通过更改screenPosition的值,还可以使窗口直接在第二个屏幕上启动(x=0, y=0, width=1920, height=1080),或者窗口跨越两个屏(x=0, y=0, width=3840, height=1080)。 如果使用读取配置文件的方法, 可以自定义屏幕分辨率。Windows系统会记录软件的窗口大小和位置,并记录在注册表的\HKEY_CURRENT_USER\Software\xxx\yyy 位置,xxx是Unity3D在build设置中的Company Name,yyy是在Build设置中的Product Name。所以如果有时候窗口大小有问题,可以先备份注册表,再删除xxx项。建议每个项目的Product Name不要用默认值,否则打包出来的软件都会对应到注册表里相同的项。

Copyright 2019 www.fgsense.com

福感科技有限公司 版权所有 All Rights Reserved

京ICP备20002031号

010-89968230