46 lines
1.3 KiB
C#
46 lines
1.3 KiB
C#
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);
|
||
}
|
||
}
|
||
}
|