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);
}
}
}