Files
2026-12-03 15:43:28 +08:00

107 lines
3.5 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.
#if ENABLE_INPUT_SYSTEM
using System;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.InputSystem;
using UnityEngine.InputSystem.Controls;
using XericLibrary.Runtime.Player;
namespace XericLibrary.Runtime.Player
{
/// <summary>
/// 新输入系统适配器:按 InputAction 名读取轴/按钮,注册到 PawnInputSourceRegistry。
/// 该文件位于 Release 侧,可引用 Unity.InputSystem;dll 侧核心不依赖它。
/// </summary>
[AddComponentMenu("Xeric Library/Player/Pawn Input System Source")]
public sealed class PawnInputSource_InputSystem : MonoBehaviour, IPawnInputSource
{
public InputActionAsset Asset;
private readonly Dictionary<string, InputAction> cache = new Dictionary<string, InputAction>(StringComparer.Ordinal);
public PawnInputSourceKind Kind { get { return PawnInputSourceKind.InputSystem; } }
public string SourceId { get { return Asset != null ? Asset.name : "inputsystem"; } }
public bool IsAlive { get { return this != null; } }
public bool IsAvailable { get { return Asset != null; } }
public bool TryRead(PawnInputChannel channel, out float value)
{
value = 0f;
if (Asset == null || channel == null || string.IsNullOrEmpty(channel.AxisName)) return false;
string key = (channel.SourceId ?? string.Empty) + "|" + channel.AxisName;
InputAction action;
if (!cache.TryGetValue(key, out action))
{
action = Asset.FindAction(channel.AxisName, false);
if (action == null && !string.IsNullOrEmpty(channel.SourceId))
{
InputActionMap map = Asset.FindActionMap(channel.SourceId, false);
if (map != null) action = map.FindAction(channel.AxisName, false);
}
cache[key] = action;
}
if (action == null) return false;
if (channel.Kind == PawnLegacyChannelKind.Axis)
{
value = action.ReadValue<float>();
// 手动更新/编辑器测试下 Action 状态可能不刷新;单控件时直接读按下的键。
if (Mathf.Abs(value) < 0.0001f)
{
InputControl active = action.activeControl;
if (active == null && action.controls.Count == 1) active = action.controls[0];
float fallback = ReadControlFloat(active);
if (Mathf.Abs(fallback) >= 0.5f) value = fallback;
}
}
else
{
value = action.ReadValue<float>() > 0.5f ? 1f : 0f;
if (value <= 0f)
{
ButtonControl button = action.activeControl as ButtonControl;
if (button != null && button.isPressed) value = 1f;
}
}
if (channel.Invert) value = -value;
value *= channel.Scale;
return true;
}
private static float ReadControlFloat(InputControl control)
{
if (control == null) return 0f;
object raw = control.ReadValueAsObject();
if (raw is float) return (float)raw;
if (raw is int) return (int)raw;
if (raw is bool) return (bool)raw ? 1f : 0f;
return 0f;
}
private sealed class Provider : PawnInputSourceProvider
{
public override PawnInputSourceKind Kind { get { return PawnInputSourceKind.InputSystem; } }
public override bool TryCreate(XericPawnController pawn, out IPawnInputSource source)
{
source = null;
if (pawn == null) return false;
PawnInputSource_InputSystem adapter = pawn.GetComponent<PawnInputSource_InputSystem>();
if (adapter == null) adapter = pawn.gameObject.AddComponent<PawnInputSource_InputSystem>();
source = adapter;
return true;
}
}
[RuntimeInitializeOnLoadMethod(RuntimeInitializeLoadType.SubsystemRegistration)]
private static void RegisterProvider()
{
PawnInputSourceRegistry.Register(new Provider());
}
}
}
#endif