79 lines
2.3 KiB
C#
79 lines
2.3 KiB
C#
using UnityEngine;
|
|
using TMPro;
|
|
using System;
|
|
|
|
public class InputManager : MonoBehaviour
|
|
{
|
|
public static InputManager Instance { get; private set; }
|
|
public static event Action<KeyCode> OnKeyPressed;
|
|
public static event Action<KeyCode> OnKeyReleased;
|
|
|
|
[Header("轨道判定显示TMP对象(请在Inspector中连接)")]
|
|
public TextMeshProUGUI[] trackJudgeTexts = new TextMeshProUGUI[5];
|
|
|
|
[Header("是否显示判定文字")]
|
|
public bool showJudgeText = true;
|
|
|
|
// 判定结果对应颜色
|
|
public Color perfectColor = Color.yellow;
|
|
public Color greatColor = Color.green;
|
|
public Color goodColor = Color.cyan;
|
|
public Color missColor = Color.red;
|
|
|
|
private void Awake()
|
|
{
|
|
if (Instance == null)
|
|
Instance = this;
|
|
else
|
|
Destroy(gameObject);
|
|
}
|
|
|
|
private void Update()
|
|
{
|
|
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))
|
|
{
|
|
OnKeyPressed?.Invoke(key);
|
|
// 调用 JudgeManager 统一判断最早的音符
|
|
JudgeManager.Instance.JudgeEarliestNote(key);
|
|
}
|
|
if (Input.GetKeyUp(key))
|
|
OnKeyReleased?.Invoke(key);
|
|
}
|
|
}
|
|
|
|
/// <summary>
|
|
/// 更新指定轨道的判定文本和颜色
|
|
/// </summary>
|
|
public void ShowJudgeResult(int trackIndex, string result)
|
|
{
|
|
if (!showJudgeText) return;
|
|
if (trackJudgeTexts == null || trackIndex < 0 || trackIndex >= trackJudgeTexts.Length) return;
|
|
var textObj = trackJudgeTexts[trackIndex];
|
|
if (textObj == null) return;
|
|
textObj.text = result;
|
|
switch (result)
|
|
{
|
|
case "Perfect":
|
|
textObj.color = perfectColor;
|
|
break;
|
|
case "Great":
|
|
textObj.color = greatColor;
|
|
break;
|
|
case "Good":
|
|
textObj.color = goodColor;
|
|
break;
|
|
case "Miss":
|
|
textObj.color = missColor;
|
|
break;
|
|
default:
|
|
textObj.color = Color.white;
|
|
break;
|
|
}
|
|
}
|
|
}
|