一、第一种办法效果不是很好,请看第二种办法
效果展示:
在Files->Build Setting 中设置无对话窗模式
#region 窗口放大缩小
public int winPosX; //窗口左上角x
public int winPosY; //窗口左上角y
public int minWidth, maxWidth; //窗口宽度
public int minHeight, maxHeight; //窗口高度
[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;
private void WindowSizeEvent(bool ison)
{
if (ison)
{
//显示器支持的所有分辨率
int i = Screen.resolutions.Length;
int resWidth = Screen.resolutions[i - 1].width;
int resHeight = Screen.resolutions[i - 1].height;
winPosX = resWidth / 2 - minWidth / 2;
winPosY = resHeight / 2 - minHeight / 2;
SetWindowLong(GetForegroundWindow(), GWL_STYLE, WS_POPUP);
bool result = SetWindowPos(GetForegroundWindow(), 0, winPosX, winPosY, minWidth, minHeight, SWP_SHOWWINDOW);
windowSize.GetComponent<Image>().sprite = icon_big;//修改图标
}
else
{
//显示器支持的所有分辨率
int i = Screen.resolutions.Length;
int resWidth = Screen.resolutions[i - 1].width;
int resHeight = Screen.resolutions[i - 1].height;
winPosX = resWidth / 2 - minWidth / 2;
winPosY = resHeight / 2 - minHeight / 2;
SetWindowLong(GetForegroundWindow(), GWL_STYLE, WS_POPUP);
bool result = SetWindowPos(GetForegroundWindow(), 0, 0, 0, maxWidth, maxHeight, SWP_SHOWWINDOW);
windowSize.GetComponent<Image>().sprite = icon_smaller;//修改图标
}
}
#endregion
ps:我用的2017版本,在打包时设置公司名称和项目名称时效果会失效,如果在使用时,代码没有问题,屏幕大小设置没问题,但是效果还是不对的话,建议恢复Bulid Setting属性。
二、第二种办法是调用unity 窗口模式下的放大缩小功能
1.首先我们创建四个按钮 分别是 放大、缩小、还原、关闭
2.设置Project Settings的窗口属性 注意:2019版本没有窗口模式选项了
3.代码
using System.Collections;
using System.Collections.Generic;
using UnityEngine.UI;
using UnityEngine;
using UMP;
using System.Runtime.InteropServices;
using System;
public class Test : MonoBehaviour
{
public Button bigBtn;
public Button smallBtn;
public Button restoreBtn;
public Button closegBtn;
//public InputField input;
//public UniversalMediaPlayer UniversalMediaPlayer;
void Start()
{
bigBtn.onClick.AddListener(OnClickMaximize);
smallBtn.onClick.AddListener(OnClickMinimize);
restoreBtn.onClick.AddListener(OnClickRestore);
closegBtn.onClick.AddListener(CloseWindow);
}
#region 窗口放大缩小
[DllImport("user32.dll")]
public static extern bool ShowWindow(IntPtr hwnd, int nCmdShow);
[DllImport("user32.dll")]
static extern IntPtr GetForegroundWindow();
const int SW_SHOWMINIMIZED = 2; //{最小化, 激活}
const int SW_SHOWMAXIMIZED = 3;//最大化
const int SW_SHOWRESTORE = 1;//还原
//最小化
public void OnClickMinimize()
{
Debug.Log("最小化");
ShowWindow(GetForegroundWindow(), SW_SHOWMINIMIZED);
}
//最大化
public void OnClickMaximize()
{
Debug.Log("最大化");
ShowWindow(GetForegroundWindow(), SW_SHOWMAXIMIZED);
}
//还原
public void OnClickRestore()
{
Debug.Log("还原");
ShowWindow(GetForegroundWindow(), SW_SHOWRESTORE);
}
#endregion
void CloseWindow()
{
Application.Quit();
}
}
在场景拖入对应按钮,打包就可以了。