首先我们先实现软件的最大化和最小化,需要使用到系统的user32.dll,这里也将系统的弹窗用到了,防止后面用到(例如,具体哪里用看自己了,我是在加载一些重要文件,文件缺失时弹出提醒)。
实现窗口最大化,最小化,使用系统弹框
[DllImport("User32.dll", SetLastError = true, ThrowOnUnmappableChar = true, CharSet = CharSet.Auto)]
private static extern int MessageBox(IntPtr handle, String message, String title, int type);//系统弹框
[DllImport("User32.dll")]
private static extern bool ShowWindow(System.IntPtr hwnd, int nCmdShow);//设置当前窗口的显示状态
[DllImport("User32.dll", EntryPoint = "GetForegroundWindow")]
private static extern IntPtr GetForegroundWindow();//获取当前激活窗口
[DllImport("User32.dll")]
private static extern IntPtr SetWindowLong(IntPtr hwnd, int _nIndex, int dwNewLong); //设置窗口边框
//边框参数
const int SW_SHOWMINIMIZED = 2;//(最小化窗口)
const int SW_SHOWMAXIMIZED = 3;//(最大化窗口)
///
/// 设置窗口最小化
///
public static void SetWindowsMin()
{
ShowWindow(GetForegroundWindow(), SW_SHOWMINIMIZED);
}
///
/// 未全屏的状态下设置窗口最大化
///
public static void SetWindowsMax()
{
ShowWindow(GetForegroundWindow(), SW_SHOWMAXIMIZED);
}
///
/// 显示系统的弹窗
///
/// 显示内容
/// 弹框标题
/// 弹框类型
public static void ShowBox(string message, string title, int type = 1)
{
MessageBox(IntPtr.Zero, message, title, type);
}
其他操作也一并写到这个里面,都是关于窗口操作的。设置多屏幕激活,设置屏幕宽高的
///
/// 设置屏幕激活 用于多屏的程序
///
public static void SetDisplayActivate()
{
for (int i = 0; i < Display.displays.Length; i++)
{
Display.displays[i].Activate(Display.displays[i].renderingWidth, Display.displays[i].renderingHeight, 60);
}
}
///
/// 设置屏幕分辨率
///
/// 宽
/// 高
/// 是否全屏 默认有边框
public static void SetWindowsResolution(int width, int height, bool fullscreen = false)
{
Screen.SetResolution(width, height, fullscreen);
}
接下来就是控制操作了,直接上代码吧,都是些最简单的代码,一看就懂。
首先是窗口工具类
using System;
using System.Collections;
using System.Collections.Generic;
using System.Runtime.InteropServices;
using UnityEngine;
namespace CommonSdk
{
///
/// 窗口类工具
///
public class WindowsUtil
{
[DllImport("User32.dll", SetLastError = true, ThrowOnUnmappableChar = true, CharSet = CharSet.Auto)]
private static extern int MessageBox(IntPtr handle, String message, String title, int type);//系统弹框
[DllImport("User32.dll")]
private static extern bool ShowWindow(System.IntPtr hwnd, int nCmdShow);//设置当前窗口的显示状态
[DllImport("User32.dll", EntryPoint = "GetForegroundWindow")]
private static extern IntPtr GetForegroundWindow();//获取当前激活窗口
[DllImport("User32.dll")]
private static extern IntPtr SetWindowLong(IntPtr hwnd, int _nIndex, int dwNewLong); //设置窗口边框
//边框参数
const int SW_SHOWMINIMIZED = 2;//(最小化窗口)
const int SW_SHOWMAXIMIZED = 3;//(最大化窗口)
///
/// 设置窗口最小化
///
public static void SetWindowsMin()
{
ShowWindow(GetForegroundWindow(), SW_SHOWMINIMIZED);
}
///
/// 未全屏的状态下设置窗口最大化
///
public static void SetWindowsMax()
{
ShowWindow(GetForegroundWindow(), SW_SHOWMAXIMIZED);
}
///
/// 显示系统的弹窗
///
/// 显示内容
/// 弹框标题
/// 弹框类型
public static void ShowBox(string message, string title, int type = 1)
{
MessageBox(IntPtr.Zero, message, title, type);
}
///
/// 设置屏幕激活 用于多屏的程序
///
public static void SetDisplayActivate()
{
for (int i = 0; i < Display.displays.Length; i++)
{
Display.displays[i].Activate(Display.displays[i].renderingWidth, Display.displays[i].renderingHeight, 60);
}
}
///
/// 设置屏幕分辨率
///
/// 宽
/// 高
/// 是否全屏 默认有边框
public static void SetWindowsResolution(int width, int height, bool fullscreen = false)
{
Screen.SetResolution(width, height, fullscreen);
}
}
}
下面是调用
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.Events;
namespace CommonSdk
{
public class CommonCtrExamples : MonoBehaviour
{
[Header("是否开启默认控制")]
public bool IsOpne;
[SerializeField, Header("默认退出按钮为Esc按钮")]
KeyCode quitKey = KeyCode.Escape;
[SerializeField, Header("默认鼠标的显示隐藏为空格按钮")]
KeyCode cursorKey = KeyCode.Space;
[SerializeField, Header("默认最小化为F11")]
KeyCode minKey = KeyCode.F11;
[SerializeField, Header("默认最大化为F12")]
KeyCode maxKey = KeyCode.F12;
[Header("自定义按钮监听")]
public KeyCode customKey;
UnityAction customAction;
void Update()
{
if (IsOpne)
{
SetCursorVisible();
MinWindows();
MaxWindows();
Quit();
CustomListen();
}
}
///
/// 自定义按钮的事件监听
///
void CustomListen()
{
if (customKey != KeyCode.None && Input.GetKeyDown(customKey))
{
if (customAction != null)
{
customAction();
}
}
}
///
/// 设置按钮的回调
///
/// 回调方法返回值为空的方法
public void AddCustomKeyAction(UnityAction action)
{
customAction += action;
}
///
/// 设置鼠标的显示与隐藏
///
void SetCursorVisible()
{
if (Input.GetKeyDown(cursorKey))
{
Debug.Log("光标控制");
Cursor.visible = !Cursor.visible;
}
}
///
/// 程序退出
///
void Quit()
{
if (Input.GetKeyDown(quitKey))
{
Debug.Log("退出控制");
Application.Quit();
}
}
///
/// 最小化程序窗口后台运行
///
void MinWindows()
{
if (Input.GetKeyDown(minKey))
{
Debug.Log("最小化控制");
WindowsUtil.SetWindowsMin();
}
}
///
/// 最大化程序窗口运行
///
void MaxWindows()
{
if (Input.GetKeyDown(maxKey))
{
Debug.Log("最大化控制");
WindowsUtil.SetWindowsMax();
}
}
}
}
后续会将工程打包为package包发出来,有问题可以加我微信S-yu12探讨。