Files
Continentis/Assets/Scripts/MainGame/Commands/Cmd_SpawnVFX.cs

92 lines
3.8 KiB
C#
Raw Normal View History

2025-10-23 00:49:44 -04:00
using System;
using Continentis.MainGame.Character;
2026-03-20 11:56:50 -04:00
using Cysharp.Threading.Tasks;
2025-10-23 00:49:44 -04:00
using Lean.Pool;
using SLSFramework.General;
using SLSFramework.UModAssistance;
using UnityEngine;
namespace Continentis.MainGame.Commands
{
public class Cmd_SpawnVFX : CommandBase
{
2026-03-20 11:56:50 -04:00
private readonly VisualEffectBase vfxPrefab;
private readonly CharacterBase target;
private readonly Vector3 fixedPosition;
private readonly Vector3 positionOffset;
private readonly bool willWaitUntilFinish;
private readonly float overrideDuration;
/// <summary>在目标角色位置生成 VFX。</summary>
public Cmd_SpawnVFX(string vfxID, CharacterBase target, Vector3 positionOffset = default,
bool willWaitUntilFinish = false, float overrideDuration = -1f)
2025-10-23 00:49:44 -04:00
{
this.vfxPrefab = ModManager.GetAsset<GameObject>(vfxID).GetComponent<VisualEffectBase>();
2026-03-20 11:56:50 -04:00
this.target = target;
this.positionOffset = positionOffset;
this.fixedPosition = Vector3.zero;
2025-10-23 00:49:44 -04:00
this.willWaitUntilFinish = willWaitUntilFinish;
this.overrideDuration = overrideDuration;
}
2026-03-20 11:56:50 -04:00
/// <summary>在世界坐标固定位置生成 VFX。</summary>
public Cmd_SpawnVFX(string vfxID, Vector3 position = default,
bool willWaitUntilFinish = false, float overrideDuration = -1f)
{
this.vfxPrefab = ModManager.GetAsset<GameObject>(vfxID).GetComponent<VisualEffectBase>();
this.target = null;
this.fixedPosition = position;
this.positionOffset = Vector3.zero;
this.willWaitUntilFinish = willWaitUntilFinish;
this.overrideDuration = overrideDuration;
}
/// <summary>在目标角色位置生成 VFX直接传入 Prefab GameObject。</summary>
public Cmd_SpawnVFX(GameObject prefab, CharacterBase target, Vector3 positionOffset = default,
bool willWaitUntilFinish = false, float overrideDuration = -1f)
2025-10-23 00:49:44 -04:00
{
this.vfxPrefab = prefab.GetComponent<VisualEffectBase>();
2026-03-20 11:56:50 -04:00
this.target = target;
this.positionOffset = positionOffset;
this.fixedPosition = Vector3.zero;
2025-10-23 00:49:44 -04:00
this.willWaitUntilFinish = willWaitUntilFinish;
this.overrideDuration = overrideDuration;
}
2026-03-20 11:56:50 -04:00
/// <summary>在世界坐标固定位置生成 VFX直接传入 Prefab GameObject。</summary>
public Cmd_SpawnVFX(GameObject prefab, Vector3 position = default,
bool willWaitUntilFinish = false, float overrideDuration = -1f)
2025-10-23 00:49:44 -04:00
{
2026-03-20 11:56:50 -04:00
this.vfxPrefab = prefab.GetComponent<VisualEffectBase>();
this.target = null;
this.fixedPosition = position;
this.positionOffset = Vector3.zero;
this.willWaitUntilFinish = willWaitUntilFinish;
this.overrideDuration = overrideDuration;
2025-12-10 18:22:26 -05:00
}
2026-03-20 11:56:50 -04:00
protected override async UniTask ExecuteAsync(CommandContext outerContext)
2025-12-10 18:22:26 -05:00
{
2026-03-20 11:56:50 -04:00
if (vfxPrefab == null)
2025-10-23 00:49:44 -04:00
{
2026-03-20 11:56:50 -04:00
Debug.LogWarning("[Cmd_SpawnVFX] VFX Prefab 为空。");
return;
2025-10-23 00:49:44 -04:00
}
2026-03-20 11:56:50 -04:00
Vector3 spawnPosition = target != null
? target.characterView.centerPoint.transform.position + positionOffset
: fixedPosition;
VisualEffectBase spawnedVFX = LeanPool.Spawn(vfxPrefab, spawnPosition, Quaternion.identity);
if (!spawnedVFX.isAutoDespawn)
Debug.LogWarning("[Cmd_SpawnVFX] 生成的 VFX 未设置自动销毁,可能导致内存泄漏。");
2025-10-23 00:49:44 -04:00
if (willWaitUntilFinish)
{
2026-03-20 11:56:50 -04:00
float duration = overrideDuration > 0f ? overrideDuration : spawnedVFX.autoDespawnTime;
await UniTask.Delay(TimeSpan.FromSeconds(duration));
2025-10-23 00:49:44 -04:00
}
}
}
}