Files
zeling_v2/Assets/_Game/Scripts/Input/ConflictDetector.cs

47 lines
1.7 KiB
C#
Raw Permalink Blame History

This file contains ambiguous Unicode characters
This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.InputSystem;
namespace BaseGames.Input
{
/// <summary>
/// 输入冲突检测器(架构 04_InputModule §6
/// 扫描给定 Action 集合,返回所有绑定了相同按键路径的 Action 名称。
/// 挂载在与 RebindPanel 同一 GameObject 上(或作为子组件)。
/// </summary>
public class ConflictDetector : MonoBehaviour
{
/// <summary>
/// 返回存在冲突的 Action 名称集合。
/// 两个 Action 绑定了同一 effectivePath → 互为冲突。
/// </summary>
/// <param name="actions">待扫描的 Action 序列(通常来自 Gameplay Map。</param>
public HashSet<string> FindConflicts(IEnumerable<InputAction> actions)
{
var pathToActions = new Dictionary<string, List<string>>();
foreach (var action in actions)
{
foreach (var binding in action.bindings)
{
// 跳过复合绑定父项(如 WASD 组合中的 "2DVector"
if (binding.isComposite || string.IsNullOrEmpty(binding.effectivePath))
continue;
if (!pathToActions.TryGetValue(binding.effectivePath, out var list))
pathToActions[binding.effectivePath] = list = new List<string>();
list.Add(action.name);
}
}
var conflicted = new HashSet<string>();
foreach (var kv in pathToActions)
if (kv.Value.Count > 1)
foreach (var name in kv.Value)
conflicted.Add(name);
return conflicted;
}
}
}