【Unity进阶】Unity发布PC端,隐藏并自定义默认标题栏
开发环境:
Unity2019.3.16f1c1 - 个人版
Visual Studio Community 2019
Windows10 专业版 x64
嘿,各位朋友们!当咱们欢欢喜喜地把项目打包成PC平台的exe窗口程序,准备在电脑上一展游戏风采时,却发现冒出来个Windows风格的白条标题栏,就像一个不速之客闯进了咱们精心布置的游戏派对,和游戏那炫酷风格完全不搭调,这多闹心呐!
别慌,咱有办法把这位“不速之客”请出去,再给它换个符合游戏气质的“酷炫外衣”——隐藏默认标题栏,然后自定义一个超有个性的游戏风格标题栏。这操作就像给游戏窗口变个小魔术,关键就在于调用Windows API函数里的“两大高手”SetWindowLong
和GetWindowLong
,再拉上WS_CAPTION
样式标志来帮忙。下面咱就来看看这场“魔术秀”的具体步骤:
创建C#脚本:在Unity项目里新建一个C#脚本,给它起个响亮的名字,就叫WindowStyle
。然后别忘了在脚本开头把System.Runtime.InteropServices
这个“魔法宝库”的命名空间引进来,这样咱就能召唤出Windows API函数来施展魔法啦。
using System;
using UnityEngine;
using UnityEngine.UI;
using System.Runtime.InteropServices;public class WindowsStyle : MonoBehaviour
{}
声明Windows API函数:在脚本里,像念咒语一样,把GetWindowLong
、SetWindowLong
还有GetForegroundWindow
这些Windows API函数都声明出来,它们可是这场“魔术”的关键道具。
[DllImport("user32.dll")]
public static extern IntPtr GetForegroundWindow();[DllImport("user32.dll")]
public static extern bool ShowWindow(IntPtr hwd, int cmdShow);[DllImport("user32.dll")]
public static extern long GetWindowLong(IntPtr hwd, int nIndex);[DllImport("user32.dll")]
public static extern void SetWindowLong(IntPtr hwd, int nIndex, long dwNewLong);
定义窗口样式常量:接下来,要定义一些能修改窗口样式的“魔法密码”,像GWL_STYLE
(它掌控着窗口样式)、WS_CAPTION
(专门和标题栏样式打交道)这些,有了它们,咱就能轻松改变窗口的“模样”啦。
/// <summary>
/// 最小化窗口,并激活顶部窗口
/// </summary>
private const int SW_MINIMIZED = 6;/// <summary>
/// 最大化窗口
/// </summary>
private const int SW_MAXIMIZED = 3;/// <summary>
/// 恢复窗口到正常状态(非最小化/最大化)
/// </summary>
private const int SW_RESTORE = 9;/// <summary>
/// 窗口风格
/// </summary>
private const int GWL_STYLE = -16;/// <summary>
/// 标题栏
/// </summary>
private const int WS_CAPTION = 0x00c00000;
编写标题栏处理方法:在脚本里大笔一挥,写个专门的方法。这个方法就像一个神奇的魔法棒,通过调用前面召唤出来的Windows API函数,就能把默认标题栏藏起来。而且,它还能让窗口实现最大化、还原、关闭这些功能,就像给窗口装上了各种“超能力按钮”(不过咱这次就先介绍怎么让方法有这些本事,不搞那些花里胡哨的美术效果啦,就用几个原生Button来假装是标题栏的按钮,凑合凑合先)。
挂载脚本:把写好的这个“魔法脚本”像挂勋章一样,挂到场景里的任意一个对象上,比如主摄像机,让它跟着对象一起在游戏的舞台上发光发热。
打包并测试:最后一步,把挂好脚本的Unity项目打包成exe文件,就像把魔法封印进一个小盒子里。然后打开这个“魔法盒子”,看看隐藏标题栏的效果是不是像咱期待的那样,让游戏窗口变得又酷又炫!
去标题栏前:
去标题栏后:
怎么样,是不是感觉给游戏窗口变个“魔术”也没那么难啦?赶紧动手试试,让你的游戏窗口也来一场华丽的变身吧!
这里是完整代码:
using System;
using UnityEngine;
using UnityEngine.UI;
using System.Runtime.InteropServices;public class WindowsStyle : MonoBehaviour
{[DllImport("user32.dll")]public static extern IntPtr GetForegroundWindow();[DllImport("user32.dll")]public static extern bool ShowWindow(IntPtr hwd, int cmdShow);[DllImport("user32.dll")]public static extern long GetWindowLong(IntPtr hwd, int nIndex);[DllImport("user32.dll")]public static extern void SetWindowLong(IntPtr hwd, int nIndex, long dwNewLong);/// <summary>/// 最小化窗口,并激活顶部窗口/// </summary>private const int SW_MINIMIZED = 6;/// <summary>/// 最大化窗口/// </summary>private const int SW_MAXIMIZED = 3;/// <summary>/// 恢复窗口到正常状态(非最小化/最大化)/// </summary>private const int SW_RESTORE = 9;/// <summary>/// 窗口风格/// </summary>private const int GWL_STYLE = -16;/// <summary>/// 标题栏/// </summary>private const int WS_CAPTION = 0x00c00000;private Button btn_hide_bar;private Button btn_show_bar;private Button btn_minimized;private Button btn_maximized;private Button btn_restore;private Button btn_close;private void Awake(){btn_hide_bar = transform.Find("btn_hide_bar").GetComponent<Button>();btn_show_bar = transform.Find("btn_show_bar").GetComponent<Button>();btn_minimized = transform.Find("btn_minimized").GetComponent<Button>();btn_maximized = transform.Find("btn_maximized").GetComponent<Button>();btn_restore = transform.Find("btn_restore").GetComponent<Button>();btn_close = transform.Find("btn_close").GetComponent<Button>();}private void Start(){btn_hide_bar.onClick.AddListener(() => HideTitleBar());btn_show_bar.onClick.AddListener(() => ShowTitleBar());btn_minimized.onClick.AddListener(() => Minimize());btn_maximized.onClick.AddListener(() => Maximize());btn_restore.onClick.AddListener(() => Restore());btn_close.onClick.AddListener(() => Close());}/// <summary>/// 隐藏标题栏/// </summary>private void HideTitleBar(){var hwd = GetForegroundWindow();var wl = GetWindowLong(hwd, GWL_STYLE);wl &= ~WS_CAPTION;SetWindowLong(hwd, GWL_STYLE, wl);}/// <summary>/// 显示标题栏/// </summary> private void ShowTitleBar(){var hwd = GetForegroundWindow();var wl = GetWindowLong(hwd, GWL_STYLE);wl |= WS_CAPTION;SetWindowLong(hwd, GWL_STYLE, wl);}/// <summary>/// 最小化/// </summary>private void Minimize(){var hwd = GetForegroundWindow();ShowWindow(hwd, SW_MINIMIZED);}/// <summary>/// 最大化/// </summary>private void Maximize(){var hwd = GetForegroundWindow();ShowWindow(hwd, SW_MAXIMIZED);}/// <summary>/// 还原/// </summary>private void Restore(){var hwd = GetForegroundWindow();ShowWindow(hwd, SW_RESTORE);}/// <summary>/// 关闭/// </summary> private void Close(){Application.Quit();}
}