using System; using System.Collections; using System.Collections.Generic; using System.Runtime.InteropServices; using UnityEditor; using UnityEngine; public class WindowsBasicConfig : MonoBehaviour { [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 GetForegroundWindow(); const uint SWP_SHOWWINDOW = 0x0040; const int GWL_STYLE = -16; //邊框用的 const int WS_BORDER = 1; const int WS_POPUP = 0x800000; int _posX ; int _posY ; int _Txtwith ; int _Txtheight ; //刷新FPS顯示間隔 private float updateInterval = 0.5F; //是否顯示FPS private bool IsGUI; //刷新FPS顯示間隔計時器 private float timeleft; // 當前FPS private float m_FPS; private GUIStyle titleStyle2 = new GUIStyle(); private void Awake() { XmlManager xml = new XmlManager(); } void Start() { _posX = int.Parse(XmlManager.XmlData["窗口化無邊框窗口位置x"]); _posY = int.Parse(XmlManager.XmlData["窗口化無邊框窗口位置y"]); _Txtwith = int.Parse(XmlManager.XmlData["屏幕寬"]); _Txtheight = int.Parse(XmlManager.XmlData["屏幕高"]); Cursor.visible = XmlManager.XmlData["鼠標隱藏"] == "0" ? false : true; bool isScence = XmlManager.XmlData["屏幕全屏"] == "0" ? false : true; Screen.SetResolution(int.Parse(XmlManager.XmlData["屏幕寬"]), int.Parse(XmlManager.XmlData["屏幕高"]), isScence); if (!isScence) { if (XmlManager.XmlData["窗口化無邊框"] == "0" ? false : true) { StartCoroutine("Setposition"); } } //垂直同步 QualitySettings.vSyncCount = int.Parse(XmlManager.XmlData["是否開啟垂直同步,0關閉,1默認65Fps,2默認30FPS"]); //關閉垂直同步限制FPS Application.targetFrameRate = int.Parse(XmlManager.XmlData["限制FPS"]); titleStyle2.fontSize = int.Parse(XmlManager.XmlData["FPS字體大小"]); titleStyle2.normal.textColor = Color.green; IsGUI = XmlManager.XmlData["顯示FPS"] == "0" ? false : true; timeleft = updateInterval; } IEnumerator Setposition() { yield return new WaitForSeconds(0.1f); //不知道為什么發布於行后,設置位置的不會生效,我延遲0.1秒就可以 SetWindowLong(GetForegroundWindow(), GWL_STYLE, WS_POPUP); //無邊框 bool result = SetWindowPos(GetForegroundWindow(), 0, _posX, _posY, _Txtwith, _Txtheight, SWP_SHOWWINDOW); //設置屏幕大小和位置 } void Update() { timeleft -= Time.deltaTime; // 間隔結束-更新gui文本並開始新間隔 if (timeleft <= 0.0) { m_FPS = 1 / Time.deltaTime; timeleft = updateInterval; } if (Input.GetKeyDown(KeyCode.Escape)) { // 退出全屏窗口化 Screen.fullScreen = false; }
// 1除於當前每一幀間隔時間 等於FPS // m_FPS=1f/Time.deltaTime; } void OnGUI() { if (IsGUI) { string format = System.String.Format("{0:F2} FPS", m_FPS); GUI.Label(new Rect(Screen.width / 2, 10, 100, 100), format, titleStyle2); } } }