Refactor interaction prompt system to use world space prompts

- Removed the InteractPromptWidget from HUD and its references in HUDController.
- Introduced IInteractPromptView interface for world space interaction prompts.
- Implemented WorldInteractPrompt class to manage display of interaction prompts in world space.
- Updated InteractableDetector to handle showing/hiding of world space prompts based on player proximity to interactable objects.
- Created a new prefab for UI_WorldInteractPrompt to facilitate the new interaction prompt system.
This commit is contained in:
2026-06-10 14:14:08 +08:00
parent 32566020c7
commit a1f54b68e6
23 changed files with 6085 additions and 554 deletions

View File

@@ -1,22 +0,0 @@
namespace BaseGames.Core.Events
{
/// <summary>
/// 交互提示事件负载。
/// 由 InteractableDetector 广播,包含触发动作名称和显示文本,
/// UI 层InteractPromptWidget据此查询图标并显示提示。
/// </summary>
public readonly struct InteractPromptEvent
{
/// <summary>InputSystem Action 名称,如 "Interact"。用于查询按键图标。</summary>
public readonly string ActionName;
/// <summary>交互物提供的说明文本,如 "对话"、"存档"、"传送"。</summary>
public readonly string LabelText;
public InteractPromptEvent(string actionName, string labelText)
{
ActionName = actionName;
LabelText = labelText;
}
}
}

View File

@@ -1,7 +0,0 @@
using UnityEngine;
namespace BaseGames.Core.Events
{
[CreateAssetMenu(menuName = "BaseGames/Events/InteractPrompt")]
public class InteractPromptEventChannelSO : BaseEventChannelSO<InteractPromptEvent> { }
}

View File

@@ -1,11 +0,0 @@
fileFormatVersion: 2
guid: 5e6db212f7619344588f054af0c6330a
MonoImporter:
externalObjects: {}
serializedVersion: 2
defaultReferences: []
executionOrder: 0
icon: {instanceID: 0}
userData:
assetBundleName:
assetBundleVariant:

View File

@@ -0,0 +1,8 @@
fileFormatVersion: 2
guid: af30250f65fc2f745a3b4089172739be
folderAsset: yes
DefaultImporter:
externalObjects: {}
userData:
assetBundleName:
assetBundleVariant:

View File

@@ -0,0 +1,21 @@
namespace BaseGames.Core.Interaction
{
/// <summary>
/// 世界空间交互提示视图接口。
/// <para>
/// 由可交互物上的子节点组件实现(如 <c>WorldInteractPrompt</c>),挂在世界空间里跟随物体显示。
/// 玩家身上的交互探测器在「最近可交互物」变化时调用 <see cref="Show"/> / <see cref="Hide"/> 驱动其显隐。
/// </para>
/// <para>
/// 接口放在 Core 程序集使「世界检测」World与「UI 表现」UI解耦避免程序集互相引用。
/// </para>
/// </summary>
public interface IInteractPromptView
{
/// <summary>显示提示。<paramref name="label"/> 为交互动作文字(来自 IInteractable.InteractPrompt。</summary>
void Show(string label);
/// <summary>隐藏提示。</summary>
void Hide();
}
}

View File

@@ -1,5 +1,5 @@
fileFormatVersion: 2
guid: 85bdb69d66e546f49b6c89941beda368
guid: 8559c1ac8ee8243459db9370032e789a
MonoImporter:
externalObjects: {}
serializedVersion: 2

View File

@@ -1,7 +1,8 @@
using TMPro;
using UnityEngine;
using UnityEngine.InputSystem;
using UnityEngine.UI;
using BaseGames.Core;
using BaseGames.UI;
namespace BaseGames.Dialogue
{
@@ -26,10 +27,9 @@ namespace BaseGames.Dialogue
[SerializeField] private TMP_Text _label;
[Header("按键图标")]
[Tooltip("键盘/鼠标设备激活时使用的按键图标 Sprite。")]
[SerializeField] private Sprite _keyboardIcon;
[Tooltip("手柄设备激活时使用的按键图标 Sprite。")]
[SerializeField] private Sprite _gamepadIcon;
[Tooltip("用于查询按键图标的输入动作名(与 InputReader 的 Action 对应)。\n" +
"图标走 IInputIconService随设备切换 / 改键自动刷新。")]
[SerializeField] private string _actionName = "Interact";
[Header("位置与动画")]
[Tooltip("相对于本组件 transform 的世界空间偏移。调整此值可控制气泡与 NPC 的相对位置。")]
@@ -42,10 +42,11 @@ namespace BaseGames.Dialogue
[SerializeField] [Min(0f)] private float _fadeOutDuration = 0.08f;
// ── 运行时状态 ────────────────────────────────────────────────────────
private InteractableNPC _npc;
private bool _visible;
private float _alpha;
private Camera _cam;
private InteractableNPC _npc;
private bool _visible;
private float _alpha;
private Camera _cam;
private IInputIconService _iconService;
// ── Lifecycle ─────────────────────────────────────────────────────────
@@ -69,6 +70,11 @@ namespace BaseGames.Dialogue
_npc.PlayerEnteredRange -= OnPlayerEntered;
_npc.PlayerExitedRange -= OnPlayerExited;
}
if (_iconService != null)
{
_iconService.OnIconSetChanged -= UpdateIcon;
_iconService = null;
}
}
private void Update()
@@ -151,9 +157,19 @@ namespace BaseGames.Dialogue
private void UpdateIcon()
{
if (_icon == null) return;
bool isGamepad = Gamepad.current != null && Gamepad.current.enabled;
_icon.sprite = isGamepad ? _gamepadIcon : _keyboardIcon;
if (_icon == null || string.IsNullOrEmpty(_actionName)) return;
// 延迟绑定:首次需要时获取服务(确保 ServiceLocator 已初始化),并订阅设备/改键刷新
if (_iconService == null)
{
_iconService = ServiceLocator.GetOrDefault<IInputIconService>();
if (_iconService != null)
_iconService.OnIconSetChanged += UpdateIcon;
}
var sprite = _iconService?.GetActionIcon(_actionName);
if (sprite != null) { _icon.sprite = sprite; _icon.enabled = true; }
else { _icon.enabled = false; }
}
private void ApplyAlpha(float a)

View File

@@ -22,7 +22,7 @@ namespace BaseGames.Editor.Input
/// ① 当前 Action × 当前设备的绑定路径 + Sprite 字段
/// ② 48px 大图预览
/// ③ 所有设备快览行4 行)
/// ④ 模拟 InteractPromptWidget 外观预览
/// ④ 模拟交互提示 外观预览
///
/// 菜单BaseGames / Input Icon Studio (priority=55)
/// </summary>
@@ -621,7 +621,7 @@ namespace BaseGames.Editor.Input
var effectivePath = GetEffectivePath(_selectedAction);
var sprite = (iconSet != null && effectivePath != null) ? iconSet.GetIcon(effectivePath) : null;
// 模拟 InteractPromptWidget 的外观
// 模拟交互提示 的外观
var mockup = new VisualElement();
mockup.style.flexDirection = FlexDirection.Row;
mockup.style.alignItems = Align.Center;

View File

@@ -224,6 +224,14 @@ namespace BaseGames.Editor
if (equipmentConfig != null) AssignReference(equipmentManager, "_config", equipmentConfig, report);
if (charmCatalog != null) AssignReference(equipmentManager, "_charmCatalog", charmCatalog, report);
// ── 交互探测器(检测最近可交互物 + 交互键调用 IInteractable.Interact + 驱动其世界空间提示)──
// 扫描 TriggerZone 层上的可交互物(门/存档点/商店/传送点…)。
// 提示表现由各交互物自带的世界空间子节点WorldInteractPrompt负责本组件不持有 UI。
InteractableDetector interactDetector = GetOrAddComponent<InteractableDetector>(root);
AssignLayerMask(interactDetector, "_interactableLayer", new[] { "TriggerZone" }, report);
if (inputReader != null)
AssignReference(interactDetector, "_inputReader", inputReader, report);
if (animConfig == null) report.Add("★ 需创建并绑定PlayerController._animConfigPLY_PlayerAnimationConfig");
if (statsConfig == null) report.Add("★ 需创建并绑定PlayerStats._configPlayerStatsSO");
if (inputReader == null) report.Add("★ 需手动绑定PlayerController._inputReader / FormController._input / SkillManager._inputInputReaderSO");
@@ -1687,6 +1695,9 @@ namespace BaseGames.Editor
AssignAsset(savePoint, "_onSceneLoaded", report, false, "EVT_SceneLoaded");
AssignAsset(savePoint, "_onSavePointActivated", report, false, "EVT_SavePointActivated");
// 世界空间交互提示
AttachInteractPrompt(go, 1.3f, report);
report.Add("已自动生成唯一 _savePointId可按需改为语义化 ID如 SP_Forest_Entrance。");
Selection.activeGameObject = go;
@@ -1747,6 +1758,30 @@ namespace BaseGames.Editor
MarkDirtyAndLog("Room Transition", go, report);
}
/// <summary>
/// 在交互物上挂载世界空间交互提示UI_WorldInteractPrompt 预制体),作为子节点跟随物体显示。
/// 提示显隐由玩家身上的 InteractableDetector 在进入/离开最近可交互物时驱动(按 IInteractPromptView 接口)。
/// 幂等已存在同名子节点则跳过。localY 为气泡相对物体的高度,可后续在场景中拖动该子节点单独微调。
/// </summary>
private static void AttachInteractPrompt(GameObject host, float localY, List<string> report)
{
if (host.transform.Find("InteractPrompt") != null) return;
var prefab = AssetDatabase.LoadAssetAtPath<GameObject>(
"Assets/_Game/Prefabs/UI/UI_WorldInteractPrompt.prefab");
if (prefab == null)
{
report.Add("★ 未找到 UI_WorldInteractPrompt 预制体,已跳过交互提示挂载(检查 Assets/_Game/Prefabs/UI/)。");
return;
}
var go = (GameObject)PrefabUtility.InstantiatePrefab(prefab, host.transform);
go.name = "InteractPrompt";
go.transform.localPosition = new Vector3(0f, localY, 0f);
Undo.RegisterCreatedObjectUndo(go, "Attach InteractPrompt");
report.Add("已挂载世界空间交互提示InteractPrompt 子节点)。拖动它可单独微调气泡位置/样式。");
}
[MenuItem("BaseGames/Scene/Place/Door Transition", priority = 141)]
public static void PlaceDoorTransition()
{
@@ -1774,6 +1809,9 @@ namespace BaseGames.Editor
AssignBool(door, "_autoTrigger", false); // 默认需玩家按交互键
AssignReference(door, "_animancer", animancer, report);
// 世界空间交互提示(按交互键模式默认显示)
AttachInteractPrompt(go, 1.6f, report);
AssignAsset(door, "_onSceneLoadRequest", report, false, "EVT_SceneLoadRequest");
report.Add("填写 _targetSceneAddress目标场景 Addressable Key与 _targetTransitionId目标 PlayerSpawnPoint 的 _transitionId。");
@@ -1843,6 +1881,10 @@ namespace BaseGames.Editor
AssignReference(doorA, "_linkedDoor", doorB, report);
AssignReference(doorB, "_linkedDoor", doorA, report);
// 世界空间交互提示(仅在该门切到按交互键模式 _autoTrigger=false 时才会显示)
AttachInteractPrompt(goA, 1.6f, report);
AttachInteractPrompt(goB, 1.6f, report);
report.Add("LinkedDoor_A ↔ LinkedDoor_B 已互相绑定,统一挂在 LinkedDoorPair 父节点下。");
report.Add("将两扇门移到场景中正确位置后,拖动各自的子节点 SpawnPoint 调整玩家传送到达位置。");
report.Add("转场效果:在各门 GameObject 上添加 SceneFeedback 组件并绑定 MMF_Player如淡入淡出再将其拖入 _transitionOut淡出和 _transitionIn淡入字段。");

View File

@@ -87,21 +87,8 @@ namespace BaseGames.Editor.UI
le.preferredHeight = 40f;
}
// ── 交互提示InteractPrompt─────────────────────────────────────
GameObject interactPromptGo = GetOrCreateChild(hudRootGo.transform, "InteractPrompt").gameObject;
InteractPromptWidget interactWidget = GetOrAddComponent<InteractPromptWidget>(interactPromptGo);
GameObject promptRootGo = GetOrCreateChild(interactPromptGo.transform, "Root").gameObject;
GameObject promptKeyIconGo = GetOrCreateChild(promptRootGo.transform, "KeyIcon").gameObject;
Image keyIcon = GetOrAddComponent<Image>(promptKeyIconGo);
GameObject promptLabelGo = GetOrCreateChild(promptRootGo.transform, "LabelText").gameObject;
TMP_Text promptLabel = GetOrAddComponent<TextMeshProUGUI>(promptLabelGo);
promptLabel.text = "互动";
promptRootGo.SetActive(false);
AssignRef(interactWidget, "_keyIcon", keyIcon);
AssignRef(interactWidget, "_labelText", promptLabel);
AssignRef(interactWidget, "_root", promptRootGo);
// 交互提示已改为「每个交互物自带的世界空间提示」WorldInteractPrompt
// 不再放在屏幕固定 HUD 上。由交互物脚手架在各自预制体/场景物体上创建。
// ── 法术槽SpellSlot───────────────────────────────────────────
GameObject spellSlotGo = GetOrCreateChild(hudRootGo.transform, "SpellSlot").gameObject;
@@ -209,7 +196,6 @@ namespace BaseGames.Editor.UI
AssignRef(hudCtrl, "_spiritGaugeFill", spiritFill);
AssignRef(hudCtrl, "_lingZhuText", lingZhuText);
AssignRef(hudCtrl, "_springContainer", springContainerGo.transform);
AssignRef(hudCtrl, "_interactPromptWidget", interactWidget);
AssignArrayRefs(hudCtrl, "_formIcons", formImages, report);
// ── HP 格子 / 回春图标 Prefab自动创建并绑定无需手工补──────────
@@ -229,9 +215,6 @@ namespace BaseGames.Editor.UI
AssignAsset(hudCtrl, "_onSpringChargesChanged", report, false, "EVT_SpringChargesChanged", "EVT_SpringCharges");
AssignAsset(hudCtrl, "_onFormChanged", report, false, "EVT_FormChanged");
AssignAsset(interactWidget, "_onShowPrompt", report, false, "EVT_ShowInteractPrompt", "EVT_InteractPromptShow");
AssignAsset(interactWidget, "_onHidePrompt", report, false, "EVT_HideInteractPrompt", "EVT_InteractPromptHide");
AssignAsset(bossHPBar, "_onBossFightToggled", report, false, "EVT_BossFightToggled", "EVT_BossFightStarted");
AssignAsset(bossHPBar, "_onBossHPChanged", report, false, "EVT_BossHPChanged");
AssignAsset(bossHPBar, "_onBossHPMaxSet", report, false, "EVT_BossHPMaxSet");
@@ -266,8 +249,6 @@ namespace BaseGames.Editor.UI
// 右下角:法术槽 + 工具栏
SetRect(spellSlotGo.transform, BottomRight, BottomRight, new Vector2(-40f, 40f), new Vector2(90f, 90f));
SetRect(toolHUDGo.transform, BottomRight, BottomRight, new Vector2(-150f, 40f), new Vector2(180f, 90f));
// 底部居中:交互提示
SetRect(interactPromptGo.transform, BottomCenter, BottomCenter, new Vector2(0f, 140f), new Vector2(320f, 64f));
// ── 手工步骤说明 ──────────────────────────────────────────────────
// _hpCellPrefab / _springIconPrefab 已自动创建并绑定(占位红/青方块,美术可替换)。

View File

@@ -0,0 +1,8 @@
fileFormatVersion: 2
guid: cd6a85a37d5b2be4bb80f145d8990ba5
folderAsset: yes
DefaultImporter:
externalObjects: {}
userData:
assetBundleName:
assetBundleVariant:

View File

@@ -30,9 +30,7 @@ namespace BaseGames.UI.HUD
[Header("Form")]
[SerializeField] private Image[] _formIcons;
[Header("Interact Prompt")]
[Tooltip("独立 Widget 组件负责渲染图标+文本HUDController 仅保留引用供编辑器配置检查")]
[SerializeField] private InteractPromptWidget _interactPromptWidget;
// 交互提示已改为「每个交互物自带的世界空间提示」WorldInteractPrompt),不再由 HUD 承载。
[Header("Event Channels - Subscribe")]
[SerializeField] private IntEventChannelSO _onHPChanged;

View File

@@ -1,119 +0,0 @@
using TMPro;
using UnityEngine;
using UnityEngine.UI;
using BaseGames.Core;
using BaseGames.Core.Events;
namespace BaseGames.UI.HUD
{
/// <summary>
/// 交互提示 Widget。
///
/// 职责:
/// • 订阅 InteractPromptEventChannelSO 显示/隐藏提示
/// • 显示按键图标Image+ 动作文本TMP_Text
/// • 监听 IInputIconService.OnIconSetChanged在设备切换或改键后自动刷新图标
///
/// 布置方式:放在 HUD Canvas 下,引用对应的事件频道 SO 资产。
/// 不依赖 HUDController可独立使用。
/// </summary>
public sealed class InteractPromptWidget : MonoBehaviour
{
[Header("UI 引用")]
[SerializeField] private Image _keyIcon;
[SerializeField] private TMP_Text _labelText;
[Tooltip("整个提示根节点,控制显示/隐藏")]
[SerializeField] private GameObject _root;
[Header("Event Channels")]
[SerializeField] private InteractPromptEventChannelSO _onShowPrompt;
[SerializeField] private VoidEventChannelSO _onHidePrompt;
// ── 运行时状态 ────────────────────────────────────────────────────────
private IInputIconService _iconService;
private string _currentActionName;
private readonly CompositeDisposable _subs = new();
// ── Lifecycle ─────────────────────────────────────────────────────────
private void OnEnable()
{
// ServiceLocator 可能在此组件 OnEnable 时尚未注册(执行顺序问题),
// 延迟到 ShowPrompt 首次调用时再获取,确保服务可用
_onShowPrompt?.Subscribe(ShowPrompt).AddTo(_subs);
_onHidePrompt?.Subscribe(HidePrompt).AddTo(_subs);
HidePrompt();
}
private void OnDisable()
{
_subs.Clear();
UnsubscribeFromIconService();
}
// ── Handlers ──────────────────────────────────────────────────────────
private void ShowPrompt(InteractPromptEvent evt)
{
_currentActionName = evt.ActionName;
// 延迟绑定:首次显示时获取服务(确保 ServiceLocator 已初始化)
if (_iconService == null)
{
_iconService = ServiceLocator.GetOrDefault<IInputIconService>();
if (_iconService != null)
_iconService.OnIconSetChanged += RefreshIcon;
}
if (_labelText != null)
_labelText.text = evt.LabelText;
RefreshIcon();
if (_root != null)
_root.SetActive(true);
else
gameObject.SetActive(true);
}
private void HidePrompt()
{
_currentActionName = null;
if (_root != null)
_root.SetActive(false);
else
gameObject.SetActive(false);
}
// ── Icon Refresh ──────────────────────────────────────────────────────
/// <summary>设备切换或改键后刷新图标。由 IInputIconService.OnIconSetChanged 调用。</summary>
private void RefreshIcon()
{
if (_keyIcon == null || string.IsNullOrEmpty(_currentActionName)) return;
var sprite = _iconService?.GetActionIcon(_currentActionName);
if (sprite != null)
{
_keyIcon.sprite = sprite;
_keyIcon.enabled = true;
}
else
{
// 找不到图标时隐藏图标格,避免显示错误占位图
_keyIcon.enabled = false;
}
}
private void UnsubscribeFromIconService()
{
if (_iconService != null)
{
_iconService.OnIconSetChanged -= RefreshIcon;
_iconService = null;
}
}
}
}

View File

@@ -0,0 +1,8 @@
fileFormatVersion: 2
guid: 8e5e071d3ce62d74284bcd89d5bbe6d4
folderAsset: yes
DefaultImporter:
externalObjects: {}
userData:
assetBundleName:
assetBundleVariant:

View File

@@ -0,0 +1,141 @@
using TMPro;
using UnityEngine;
using UnityEngine.UI;
using BaseGames.Core;
using BaseGames.Core.Interaction;
namespace BaseGames.UI
{
/// <summary>
/// 世界空间交互提示(按键图标 + 文字),挂在可交互物的子节点上,跟随物体显示。
///
/// 职责:
/// • 实现 <see cref="IInteractPromptView"/>,由玩家身上的交互探测器在进入/离开最近可交互物时驱动显隐。
/// • 显示按键图标Image+ 动作文本TMP_Text图标走 <see cref="IInputIconService"/>
/// 随设备切换 / 改键自动刷新(与全局一致)。
/// • 通过 <see cref="CanvasGroup"/> 做淡入 / 淡出。
///
/// 布置方式:作为可交互物(门 / 存档点 / 商店 / 传送点…)的子节点,
/// 用世界空间 Canvas 承载。气泡相对物体的位置 = 本节点的 Transform
/// 直接在场景里拖动本子节点即可单独微调每个物体的提示锚点。
/// </summary>
public sealed class WorldInteractPrompt : MonoBehaviour, IInteractPromptView
{
[Header("UI 引用")]
[Tooltip("淡入淡出用 CanvasGroup一般挂在本节点的 Canvas 上)。")]
[SerializeField] private CanvasGroup _canvasGroup;
[Tooltip("按键图标 Image。由 IInputIconService 按当前设备填充。")]
[SerializeField] private Image _keyIcon;
[Tooltip("提示文字 TMP_Text。显示 IInteractable.InteractPrompt 的当前值。")]
[SerializeField] private TMP_Text _labelText;
[Header("按键动作")]
[Tooltip("用于查询按键图标的输入动作名(与 InputReader 的 Action 对应)。")]
[SerializeField] private string _actionName = "Interact";
[Header("淡入淡出")]
[Tooltip("淡入持续时间。0 = 立即显示。")]
[SerializeField] [Min(0f)] private float _fadeInDuration = 0.12f;
[Tooltip("淡出持续时间。0 = 立即隐藏。")]
[SerializeField] [Min(0f)] private float _fadeOutDuration = 0.08f;
// ── 运行时状态 ────────────────────────────────────────────────────────
private IInputIconService _iconService;
private bool _visible;
private float _alpha;
// ── Lifecycle ─────────────────────────────────────────────────────────
private void Awake()
{
_alpha = 0f;
_visible = false;
ApplyAlpha(0f);
}
private void OnDisable()
{
UnsubscribeFromIconService();
_visible = false;
_alpha = 0f;
ApplyAlpha(0f);
}
private void Update()
{
// 完全隐藏且无需继续淡出时,跳过
if (!_visible && _alpha <= 0f) return;
float target = _visible ? 1f : 0f;
if (!Mathf.Approximately(_alpha, target))
{
float dur = _visible ? _fadeInDuration : _fadeOutDuration;
float speed = dur > 0f ? Time.unscaledDeltaTime / dur : 1f;
_alpha = Mathf.MoveTowards(_alpha, target, speed);
ApplyAlpha(_alpha);
}
}
// ── IInteractPromptView ───────────────────────────────────────────────
public void Show(string label)
{
if (_labelText != null)
_labelText.text = label;
// 延迟绑定:首次显示时获取服务(确保 ServiceLocator 已初始化)
if (_iconService == null)
{
_iconService = ServiceLocator.GetOrDefault<IInputIconService>();
if (_iconService != null)
_iconService.OnIconSetChanged += RefreshIcon;
}
RefreshIcon();
_visible = true;
}
public void Hide() => _visible = false;
// ── Icon ──────────────────────────────────────────────────────────────
/// <summary>设备切换或改键后刷新图标。由 IInputIconService.OnIconSetChanged 调用。</summary>
private void RefreshIcon()
{
if (_keyIcon == null || string.IsNullOrEmpty(_actionName)) return;
var sprite = _iconService?.GetActionIcon(_actionName);
if (sprite != null)
{
_keyIcon.sprite = sprite;
_keyIcon.enabled = true;
}
else
{
_keyIcon.enabled = false; // 找不到图标时隐藏图标格,避免错误占位图
}
}
private void UnsubscribeFromIconService()
{
if (_iconService != null)
{
_iconService.OnIconSetChanged -= RefreshIcon;
_iconService = null;
}
}
// ── 辅助 ──────────────────────────────────────────────────────────────
private void ApplyAlpha(float a)
{
if (_canvasGroup != null)
{
_canvasGroup.alpha = a;
// 提示纯展示,不接收交互;同时彻底关闭射线避免遮挡
_canvasGroup.interactable = false;
_canvasGroup.blocksRaycasts = false;
}
}
}
}

View File

@@ -1,5 +1,5 @@
fileFormatVersion: 2
guid: 9eccce8fdbd936b46a467d078957a387
guid: 845b4e4cb1d22f54e8b0910d937650a8
MonoImporter:
externalObjects: {}
serializedVersion: 2

View File

@@ -1,21 +1,23 @@
using System.Collections.Generic;
using BaseGames.Core.Events;
using BaseGames.Core.Interaction;
using BaseGames.Input;
using UnityEngine;
namespace BaseGames.World
{
/// <summary>
/// 挂在 Player 上,检测附近可交互物,驱动 UI 提示显示/隐藏
/// 挂在 Player 上,检测附近可交互物,驱动其世界空间提示(<see cref="IInteractPromptView"/>)显隐
/// 通过 InputReaderSO.InteractEvent 绑定交互输入。
/// <para>
/// 提示表现是「每个交互物自带」的世界空间子节点(跟随物体、可单独配样式),
/// 本组件只负责「检测最近可交互物」并通知它显示/隐藏,不持有任何 UI。
/// </para>
/// </summary>
public class InteractableDetector : MonoBehaviour
{
[SerializeField] private float _detectRadius = 1.5f;
[SerializeField] private LayerMask _interactableLayer;
[SerializeField] private InputReaderSO _inputReader;
[SerializeField] private InteractPromptEventChannelSO _onShowInteractPrompt;
[SerializeField] private VoidEventChannelSO _onHideInteractPrompt;
[SerializeField] private float _detectRadius = 1.5f;
[SerializeField] private LayerMask _interactableLayer;
[SerializeField] private InputReaderSO _inputReader;
private IInteractable _nearest;
private IInteractable _previousNearest;
@@ -26,6 +28,9 @@ namespace BaseGames.World
// Collider → IInteractable 缓存,避免 FindNearest 每帧重复 GetComponentInParent
private readonly Dictionary<Collider2D, IInteractable> _componentCache = new();
// IInteractable → 其世界空间提示视图缓存(可能为 null表示该物体未配置提示
private readonly Dictionary<IInteractable, IInteractPromptView> _promptCache = new();
private void OnEnable()
{
_inputReader.InteractEvent += TryInteract;
@@ -34,7 +39,12 @@ namespace BaseGames.World
private void OnDisable()
{
_inputReader.InteractEvent -= TryInteract;
// 离场前隐藏当前提示,防止跨场景残留
ResolvePrompt(_previousNearest)?.Hide();
_componentCache.Clear(); // 清理缓存,防止跨场景持有旧引用
_promptCache.Clear();
_previousNearest = null;
_nearest = null;
}
private void Update()
@@ -48,13 +58,13 @@ namespace BaseGames.World
if (_previousNearest != null)
{
_previousNearest.OnPlayerExitRange();
_onHideInteractPrompt?.Raise();
ResolvePrompt(_previousNearest)?.Hide();
}
if (_nearest != null)
{
_nearest.OnPlayerEnterRange(transform);
_onShowInteractPrompt?.Raise(new InteractPromptEvent("Interact", _nearest.InteractPrompt));
ResolvePrompt(_nearest)?.Show(_nearest.InteractPrompt);
}
_previousNearest = _nearest;
@@ -98,6 +108,17 @@ namespace BaseGames.World
return best;
}
/// <summary>解析交互物的世界空间提示视图(在其子节点上查找一次并缓存;无则记 null。</summary>
private IInteractPromptView ResolvePrompt(IInteractable interactable)
{
if (interactable == null) return null;
if (_promptCache.TryGetValue(interactable, out var view)) return view;
view = (interactable as Component)?.GetComponentInChildren<IInteractPromptView>(true);
_promptCache[interactable] = view;
return view;
}
private void OnDrawGizmosSelected()
{
Gizmos.color = Color.cyan;