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() {#if UNITY_STANDALONE_WIN SetWindowLong(GetActiveWindow(), GWL_STYLE, WS_BORDER); SetWindowPos(GetActiveWindow(), -1, (int)screenPosition.x, (int)screenPosition.y, (int)screenPosition.width, (int)screenPosition.height, SWP_SHOWWINDOW); #endif } void Update() { i++; if(i<5) { #if UNITY_STANDALONE_WIN SetWindowLong(GetActiveWindow(), GWL_STYLE, WS_BORDER); SetWindowPos(GetActiveWindow(), -1, (int)screenPosition.x, (int)screenPosition.y, (int)screenPosition.width, (int)screenPosition.height, SWP_SHOWWINDOW); #endif } } }
用這個腳本,可以使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不要用默認值,否則打包出來的軟件都會對應到注冊表里相同的項。
在實際測試代碼的時候直接點三角號運行了,結果unity的標題欄被隱藏,始終在最前,而且窗口大小改變了……而且改不回來,十分無語……
看來一定要打包后再實際進行測試。
現在只要將腳本綁定到場景物體上,并設置一下窗口大小就可以使用了
原文鏈接:http://www.chinaar.com/unity/1722.html
另一種方式:
在生成的執行文件目錄下創建一個bat文件,寫入內容:
demo.exe -popupwindow -screen-width 3840 -screen-height 1080用這個bat啟動程序即可。其中demo.exe替換成你的exe名稱,-screen-width是兩個屏幕分辨率寬度的總和,-screen-height是一個屏幕的高度(當然,一般這種情況下,兩個屏幕分辨率都是一致的)
原文鏈接:http://www.chinaar.com/unity/1752.html
新聞熱點
疑難解答