技能主要更新,修复卡顿并加入动画,以及各种其他更新。
This commit is contained in:
@@ -1,176 +1,190 @@
|
||||
using UnityEngine;
|
||||
using TMPro;
|
||||
using System;
|
||||
using UnityEngine.UI;
|
||||
|
||||
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("按键指示文本(独立于判定文本,五个分别对应按键:D,F,Space,J,K)")]
|
||||
public Text[] trackKeyTexts = new Text[5];
|
||||
|
||||
[Header("是否显示判定文字")]
|
||||
public bool showJudgeText = true;
|
||||
|
||||
// 按键激活/非激活颜色
|
||||
[Header("按键高亮颜色设置")]
|
||||
[Tooltip("按键被按下时的颜色")]
|
||||
public Color keyActiveColor = Color.yellow;
|
||||
[Tooltip("按键未按下时的颜色(默认黑色)")]
|
||||
public Color keyInactiveColor = Color.black;
|
||||
|
||||
// 判定结果对应颜色
|
||||
[Header("判定结果对应颜色")]
|
||||
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);
|
||||
|
||||
// 初始化按键文本为非激活颜色
|
||||
if (trackKeyTexts != null)
|
||||
{
|
||||
foreach (var t in trackKeyTexts)
|
||||
{
|
||||
if (t != null) t.color = keyInactiveColor;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
private void Start()
|
||||
{
|
||||
// Ensure UI shows current bindings from KeyBindingManager/PlayerPrefs
|
||||
RefreshKeyLabels();
|
||||
}
|
||||
|
||||
private void Update()
|
||||
{
|
||||
foreach (string color in new string[] { "red", "green", "yellow", "purple", "blue" })
|
||||
{
|
||||
KeyCode key = KeyBindingManager.GetKeyForColor(color);
|
||||
if (key == KeyCode.None) continue;
|
||||
|
||||
int index = GetIndexForColor(color);
|
||||
|
||||
if (Input.GetKeyDown(key))
|
||||
{
|
||||
OnKeyPressed?.Invoke(key);
|
||||
// 调用 JudgeManager 统一判断最早的音符
|
||||
JudgeManager.Instance.JudgeEarliestNote(key);
|
||||
|
||||
// 按下时设置对应的按键文本颜色为激活色(如果已设置)
|
||||
if (trackKeyTexts != null && index >= 0 && index < trackKeyTexts.Length)
|
||||
{
|
||||
var txt = trackKeyTexts[index];
|
||||
if (txt != null)
|
||||
txt.color = keyActiveColor;
|
||||
}
|
||||
}
|
||||
if (Input.GetKeyUp(key))
|
||||
{
|
||||
OnKeyReleased?.Invoke(key);
|
||||
|
||||
// 松开时恢复为非激活颜色
|
||||
if (trackKeyTexts != null && index >= 0 && index < trackKeyTexts.Length)
|
||||
{
|
||||
var txt = trackKeyTexts[index];
|
||||
if (txt != null)
|
||||
txt.color = keyInactiveColor;
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Force a release event for all bound keys and reset UI indicators.
|
||||
/// Useful for external "clear input" operations where the system should treat
|
||||
/// all keys as released regardless of physical state.
|
||||
/// </summary>
|
||||
public void ForceReleaseAllKeys()
|
||||
{
|
||||
string[] colors = new string[] { "red", "green", "yellow", "purple", "blue" };
|
||||
foreach (var color in colors)
|
||||
{
|
||||
KeyCode key = KeyBindingManager.GetKeyForColor(color);
|
||||
if (key == KeyCode.None) continue;
|
||||
try { OnKeyReleased?.Invoke(key); } catch { }
|
||||
|
||||
int index = GetIndexForColor(color);
|
||||
if (trackKeyTexts != null && index >= 0 && index < trackKeyTexts.Length)
|
||||
{
|
||||
var txt = trackKeyTexts[index];
|
||||
if (txt != null) txt.color = keyInactiveColor;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
// Refresh displayed labels for key bindings (called after rebind)
|
||||
public void RefreshKeyLabels()
|
||||
{
|
||||
string[] colors = new string[] { "red", "green", "yellow", "purple", "blue" };
|
||||
if (trackKeyTexts == null) return;
|
||||
for (int i = 0; i < colors.Length && i < trackKeyTexts.Length; i++)
|
||||
{
|
||||
var txt = trackKeyTexts[i];
|
||||
if (txt == null) continue;
|
||||
KeyCode k = KeyBindingManager.GetKeyForColor(colors[i]);
|
||||
txt.text = KeyBindingManager.GetDisplayName(k);
|
||||
}
|
||||
}
|
||||
|
||||
private int GetIndexForColor(string color)
|
||||
{
|
||||
// 保持与 trackJudgeTexts 相同的索引映射
|
||||
switch (color)
|
||||
{
|
||||
case "red": return 0;
|
||||
case "green": return 1;
|
||||
case "yellow": return 2;
|
||||
case "purple": return 3;
|
||||
case "blue": return 4;
|
||||
default: return -1;
|
||||
}
|
||||
}
|
||||
|
||||
/// <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;
|
||||
}
|
||||
}
|
||||
}
|
||||
using UnityEngine;
|
||||
using TMPro;
|
||||
using System;
|
||||
using UnityEngine.UI;
|
||||
|
||||
public class InputManager : MonoBehaviour
|
||||
{
|
||||
public static InputManager Instance { get; private set; }
|
||||
public static event Action<KeyCode> OnKeyPressed;
|
||||
public static event Action<KeyCode> OnKeyReleased;
|
||||
|
||||
private static readonly string[] TrackColors = { "red", "green", "yellow", "purple", "blue" };
|
||||
|
||||
[Header("����ж���ʾTMP��������Inspector�����ӣ�")]
|
||||
public TextMeshProUGUI[] trackJudgeTexts = new TextMeshProUGUI[5];
|
||||
|
||||
[Header("����ָʾ�ı����������ж��ı�������ֱ��Ӧ������D,F,Space,J,K��")]
|
||||
public Text[] trackKeyTexts = new Text[5];
|
||||
|
||||
[Header("�Ƿ���ʾ�ж�����")]
|
||||
public bool showJudgeText = true;
|
||||
|
||||
// ��������/�Ǽ�����ɫ
|
||||
[Header("����������ɫ����")]
|
||||
[Tooltip("����������ʱ����ɫ")]
|
||||
public Color keyActiveColor = Color.yellow;
|
||||
[Tooltip("����δ����ʱ����ɫ��Ĭ�Ϻ�ɫ��")]
|
||||
public Color keyInactiveColor = Color.black;
|
||||
|
||||
// �ж������Ӧ��ɫ
|
||||
[Header("�ж������Ӧ��ɫ")]
|
||||
public Color perfectColor = Color.yellow;
|
||||
public Color greatColor = Color.green;
|
||||
public Color goodColor = Color.cyan;
|
||||
public Color missColor = Color.red;
|
||||
|
||||
private KeyCode[] cachedKeys = new KeyCode[5];
|
||||
|
||||
private void Awake()
|
||||
{
|
||||
if (Instance == null)
|
||||
Instance = this;
|
||||
else
|
||||
Destroy(gameObject);
|
||||
|
||||
// 初始化按键缓存
|
||||
RefreshKeyCache();
|
||||
|
||||
// ��ʼ�������ı�Ϊ�Ǽ�����ɫ
|
||||
if (trackKeyTexts != null)
|
||||
{
|
||||
foreach (var t in trackKeyTexts)
|
||||
{
|
||||
if (t != null) t.color = keyInactiveColor;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
private void RefreshKeyCache()
|
||||
{
|
||||
for (int i = 0; i < TrackColors.Length; i++)
|
||||
{
|
||||
cachedKeys[i] = KeyBindingManager.GetKeyForColor(TrackColors[i]);
|
||||
}
|
||||
}
|
||||
|
||||
private void Start()
|
||||
{
|
||||
// Ensure UI shows current bindings from KeyBindingManager/PlayerPrefs
|
||||
RefreshKeyLabels();
|
||||
}
|
||||
|
||||
private void Update()
|
||||
{
|
||||
for (int i = 0; i < TrackColors.Length; i++)
|
||||
{
|
||||
KeyCode key = cachedKeys[i];
|
||||
if (key == KeyCode.None) continue;
|
||||
|
||||
int index = i; // TrackColors array index matches track index logic here
|
||||
|
||||
if (Input.GetKeyDown(key))
|
||||
{
|
||||
OnKeyPressed?.Invoke(key);
|
||||
// ���� JudgeManager ͳһ�ж����������
|
||||
JudgeManager.Instance.JudgeEarliestNote(key);
|
||||
|
||||
// ����ʱ���ö�Ӧ�İ����ı���ɫΪ����ɫ����������ã�
|
||||
if (trackKeyTexts != null && index >= 0 && index < trackKeyTexts.Length)
|
||||
{
|
||||
var txt = trackKeyTexts[index];
|
||||
if (txt != null)
|
||||
txt.color = keyActiveColor;
|
||||
}
|
||||
}
|
||||
if (Input.GetKeyUp(key))
|
||||
{
|
||||
OnKeyReleased?.Invoke(key);
|
||||
|
||||
// �ɿ�ʱ�ָ�Ϊ�Ǽ�����ɫ
|
||||
if (trackKeyTexts != null && index >= 0 && index < trackKeyTexts.Length)
|
||||
{
|
||||
var txt = trackKeyTexts[index];
|
||||
if (txt != null)
|
||||
txt.color = keyInactiveColor;
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Force a release event for all bound keys and reset UI indicators.
|
||||
/// Useful for external "clear input" operations where the system should treat
|
||||
/// all keys as released regardless of physical state.
|
||||
/// </summary>
|
||||
public void ForceReleaseAllKeys()
|
||||
{
|
||||
for (int i = 0; i < TrackColors.Length; i++)
|
||||
{
|
||||
string color = TrackColors[i];
|
||||
KeyCode key = KeyBindingManager.GetKeyForColor(color);
|
||||
if (key == KeyCode.None) continue;
|
||||
try { OnKeyReleased?.Invoke(key); } catch { }
|
||||
|
||||
int index = GetIndexForColor(color);
|
||||
if (trackKeyTexts != null && index >= 0 && index < trackKeyTexts.Length)
|
||||
{
|
||||
var txt = trackKeyTexts[index];
|
||||
if (txt != null) txt.color = keyInactiveColor;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
// Refresh displayed labels for key bindings (called after rebind)
|
||||
public void RefreshKeyLabels()
|
||||
{
|
||||
if (trackKeyTexts == null) return;
|
||||
for (int i = 0; i < TrackColors.Length && i < trackKeyTexts.Length; i++)
|
||||
{
|
||||
var txt = trackKeyTexts[i];
|
||||
if (txt == null) continue;
|
||||
KeyCode k = KeyBindingManager.GetKeyForColor(TrackColors[i]);
|
||||
txt.text = KeyBindingManager.GetDisplayName(k);
|
||||
}
|
||||
}
|
||||
|
||||
private int GetIndexForColor(string color)
|
||||
{
|
||||
// ������ trackJudgeTexts ��ͬ������ӳ��
|
||||
switch (color)
|
||||
{
|
||||
case "red": return 0;
|
||||
case "green": return 1;
|
||||
case "yellow": return 2;
|
||||
case "purple": return 3;
|
||||
case "blue": return 4;
|
||||
default: return -1;
|
||||
}
|
||||
}
|
||||
|
||||
/// <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;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user