當Untiy最小化后,游戲窗口不會立刻失去焦點,此時依然可以使用Input來獲取按鍵,但是點擊其他窗口后,就會失去焦點,此時系統會把按鍵輸入分配到其他窗口里,此時要用windowsAPI獲取按鍵輸入,應對兩種情況的代如下
[DllImport("user32.dll")]public static extern short GetAsyncKeyState(int vKey);public const int VK_3 = 0x33; // 數字鍵3的虛擬鍵碼public const int VK_NUMPAD3 = 0x63; // 數字鍵3的虛擬鍵碼
private void Update()
{if (!Application.isFocused){//如果點了桌面或別的窗口,會觸發windowsapiif (GetAsyncKeyState(VK_3) != 0|| GetAsyncKeyState(VK_NUMPAD3) != 0){RestoreWIndow();//恢復窗口}}else //最小化后,如果沒有在別的地方點擊鼠標,此時untiy還沒有失去焦點,直接按3會觸發Untiy輸入{if (Input.GetKeyDown(KeyCode.Alpha3) || Input.GetKeyDown(KeyCode.Keypad3)){RestoreWIndow();//恢復窗口}}
}