百花楼论坛全国_一品楼QM论坛_泻火吧论坛重庆

  • 您的位置:首页 > 新闻动态 > Unity3D

    Unity3D跨屏幕、全屏显示方法

    2019/12/14??????点击:

    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不要用默认值,否则打包出来的软件都会对应到注册表里相同的项。



    主站蜘蛛池模板: 康平县| 阳新县| 乐业县| 独山县| 富平县| 太仓市| 岳池县| 廉江市| 平和县| 阜阳市| 德安县| 岑巩县| 灵璧县| 家居| 文化| 四子王旗| 宜兰市| 长葛市| 南投市| 清镇市| 石首市| 长乐市| 许昌县| 上栗县| 襄垣县| 和政县| 阜宁县| 安仁县| 肥西县| 天门市| 云阳县| 丁青县| 阜城县| 大同县| 土默特左旗| 张掖市| 临潭县| 高要市| 盖州市| 潼南县| 铜山县|