百花楼论坛全国_一品楼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不要用默认值,否则打包出来的软件都会对应到注册表里相同的项。



    主站蜘蛛池模板: 新昌县| 乌拉特后旗| 南康市| 武义县| 玉林市| 汕尾市| 时尚| 东阿县| 泰来县| 南昌县| 宿迁市| 新绛县| 扶绥县| 平顶山市| 海门市| 大竹县| 彭泽县| 云霄县| 田林县| 抚顺市| 佛山市| 光泽县| 获嘉县| 枣阳市| 微山县| 伊宁县| 浦城县| 边坝县| 广丰县| 斗六市| 太湖县| 高碑店市| 聊城市| 宣汉县| 米林县| 札达县| 罗江县| 汉川市| 昌乐县| 台东市| 潍坊市|