52 lines
1.4 KiB
C#
52 lines
1.4 KiB
C#
using UnityEngine;
|
|
using System;
|
|
|
|
public class InputManager : MonoBehaviour
|
|
{
|
|
public static event Action<KeyCode> OnKeyPressed;
|
|
public static event Action<KeyCode> OnKeyReleased;
|
|
|
|
// 存储当前帧所有按下的按键
|
|
private KeyCode[] pressedKeysThisFrame = new KeyCode[5];
|
|
private int pressedKeyCount = 0;
|
|
|
|
private void Update()
|
|
{
|
|
pressedKeyCount = 0;
|
|
|
|
// 检查所有可能的按键
|
|
foreach (string color in new string[] { "red", "green", "yellow", "purple", "blue" })
|
|
{
|
|
KeyCode key = KeyBindingManager.GetKeyForColor(color);
|
|
if (key == KeyCode.None) continue;
|
|
|
|
if (Input.GetKeyDown(key))
|
|
{
|
|
// 记录当前帧按下的所有按键
|
|
pressedKeysThisFrame[pressedKeyCount++] = key;
|
|
OnKeyPressed?.Invoke(key);
|
|
}
|
|
if (Input.GetKeyUp(key))
|
|
{
|
|
OnKeyReleased?.Invoke(key);
|
|
}
|
|
}
|
|
|
|
// 处理多键同时按下
|
|
if (pressedKeyCount > 1)
|
|
{
|
|
HandleMultiKeyPress();
|
|
}
|
|
}
|
|
|
|
private void HandleMultiKeyPress()
|
|
{
|
|
// 对当前帧所有按下的按键进行处理
|
|
for (int i = 0; i < pressedKeyCount; i++)
|
|
{
|
|
KeyCode key = pressedKeysThisFrame[i];
|
|
// 调用 JudgeManager 统一判断对应按键的音符
|
|
JudgeManager.Instance.JudgeEarliestNote(key);
|
|
}
|
|
}
|
|
} |