1.代码
/*
Type:设置分辨率
*/
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using System;
using System.Runtime.InteropServices;
public class ScreenManager : MonoBehaviour
{
public static ScreenManager instance;
private void Awake()
{
instance = this;
}
[HideInInspector]
//导入设置窗口函数
[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 SetWindowLong(IntPtr hwnd, int _nIndex, long dwNewLong);
[DllImport("user32.dll")]
public static extern long GetWindowLong(IntPtr hwd, int nIndex);
//导入当前活动窗口
[DllImport("user32.dll")]
static extern IntPtr GetActiveWindow();
//引用windows接口
[DllImport("user32.dll")]
public static extern IntPtr GetForegroundWindow();
const uint SWP_SHOWWINDOW = 0x0040;//显示窗口
const int GWL_STYLE = -16;
const int WS_BORDER = 1;
/// <summary>
/// 标题栏
/// </summary>
const int WS_CAPTION = 0x00c00000;
/// <summary>
/// 标题栏按钮
/// </summary>
const int WS_SYSMENU = 0x00080000;
void Start()
{
//编辑器状态下脚本不执行,发布exe脚本执行
#if !UNITY_EDITOR
//去掉上面的框
SetWindowLong(GetActiveWindow(), GWL_STYLE, WS_BORDER);
//显示范围
//第一个0是开始x第二个0是开始y,1920是不是位置,是长度,比如要显示开始是(500,500),而结束要想显示在最后,不是写1920.而是写1920-500的值
SetWindowPos(GetActiveWindow(), -1, 0, 0, 1920, 1080, SWP_SHOWWINDOW);
#endif
}
public void SetScreen(int x,int y,int width,int height)
{
//编辑器状态下脚本不执行,发布exe脚本执行
#if !UNITY_EDITOR
//去掉上面的框
// SetWindowLong(GetActiveWindow(), GWL_STYLE, WS_BORDER);
//显示范围
SetWindowPos(GetActiveWindow(), -1, x, y, width, height, SWP_SHOWWINDOW);//
#endif
}
}
2.发布设置