Files
zeling_v2/Assets/_Game/Scripts/VFX/HitFXSpawner.cs

46 lines
1.3 KiB
C#
Raw 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 UnityEngine;
using BaseGames.Core;
using BaseGames.Core.Events;
using BaseGames.Combat;
namespace BaseGames.VFX
{
/// <summary>
/// 全局命中特效派发器:订阅 HitConfirmedEventChannel
/// 根据 DamageInfo.FxType 从 VFXCatalog 查找并播放特效。
/// 放置在 Persistent 场景的 [Systems] GameObject 上。
/// </summary>
public class HitFXSpawner : MonoBehaviour
{
[SerializeField] private HitConfirmedEventChannelSO _onHitConfirmed;
[SerializeField] private VFXCatalogSO _catalog;
private readonly CompositeDisposable _subs = new();
private void Awake()
{
Debug.Assert(_catalog != null, "[HitFXSpawner] _catalog 未赋值,请在 Inspector 中指定 VFXCatalogSO。", this);
_catalog.Initialize();
}
private void OnEnable()
{
_onHitConfirmed?.Subscribe(HandleHit).AddTo(_subs);
}
private void OnDisable()
{
_subs.Clear();
}
private void HandleHit(HitInfo info)
{
var pool = ServiceLocator.GetOrDefault<IVFXPoolService>();
if (pool == null) return;
if (_catalog.TryGetHitFX(info.DamageInfo.FxType, out var vfxRef))
pool.Play(vfxRef, info.HitPoint);
}
}
}