Unity3D跨屏(píng)幕、全屏顯示方法
Unity3D跨屏(píng)幕、全屏顯示方法,運行環境: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的值,還可以使窗口(kǒu)直接在第二個屏幕上啟(qǐ)動(x=0, y=0, width=1920, height=1080),或(huò)者窗口(kǒu)跨越(yuè)兩(liǎng)個屏(x=0, y=0, width=3840, height=1080)。 如果(guǒ)使用讀取配置(zhì)文件的方法, 可以自定義屏幕分辨率。Windows係統會記錄(lù)軟件的窗口大小和位置,並記錄在注(zhù)冊表的\HKEY_CURRENT_USER\Software\xxx\yyy 位置,xxx是Unity3D在build設置中(zhōng)的Company Name,yyy是(shì)在Build設置中(zhōng)的(de)Product Name。所以如果有時候(hòu)窗口大小有問題,可以先備份注冊表,再刪除xxx項。建(jiàn)議每個項目的Product Name不要用默認值,否(fǒu)則打包出來(lái)的軟件都會對應到注冊表裏相同(tóng)的項。
- 上一篇:UNITY3D GUI組件使用例子 2019/12/22
- 下一篇:Unity3D的射線碰撞(zhuàng)檢測方法總結 2019/12/12