開發環境:
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();}
}