Files
Cielonos/Assets/Scripts/MainGame/Effects/VFX/VFXObject.cs
SoulliesOfficial d94241f36c 场景设计
2026-01-12 03:22:16 -05:00

95 lines
2.7 KiB
C#

using System;
using System.Collections.Generic;
using Cielonos.MainGame.Characters;
using Lean.Pool;
using Sirenix.OdinInspector;
using SLSFramework.LeanPoolAssistance;
using UnityEngine;
namespace Cielonos.MainGame
{
public class VFXObject : PooledObject
{
[NonSerialized]
public CharacterBase creator;
public bool affectedByCreatorTimeScale = true;
public List<ParticleSystem> particles = new List<ParticleSystem>();
[NonSerialized] private List<ParticleSystem.MainModule> particleMainModules;
public static GameObject Spawn(GameObject vfxPrefab, CharacterBase creator, Transform parent = null)
{
VFXObject vfxObject = LeanPool.Spawn(vfxPrefab, parent).GetComponent<VFXObject>();
vfxObject.SetCreator(creator);
return vfxObject.gameObject;
}
public static GameObject Spawn(GameObject vfxPrefab, CharacterBase creator, Vector3 position, Quaternion rotation, Transform parent = null)
{
VFXObject vfxObject = LeanPool.Spawn(vfxPrefab, position, rotation, parent).GetComponent<VFXObject>();
vfxObject.SetCreator(creator);
return vfxObject.gameObject;
}
private void Reset()
{
CollectParticles();
}
public override void OnSpawn()
{
if (spawnExecuted)
{
return;
}
base.OnSpawn();
if (particleMainModules == null)
{
particleMainModules = new List<ParticleSystem.MainModule>();
foreach (var ps in particles)
{
particleMainModules.Add(ps.main);
}
}
}
[Button("Collect Particles")]
private void CollectParticles()
{
particles.Clear();
ParticleSystem[] foundParticles = GetComponentsInChildren<ParticleSystem>();
foreach (var ps in foundParticles)
{
particles.Add(ps);
}
}
protected override void Update()
{
float deltaTime = Time.deltaTime;
float timeScale = 1f;
if (creator != null)
{
deltaTime = creator.selfTimeSm.DeltaTime;
timeScale = creator.selfTimeSm.TimeScale;
}
UpdateTimer(deltaTime);
if (affectedByCreatorTimeScale)
{
particleMainModules.ForEach(main => main.simulationSpeed = timeScale);
}
}
public void SetCreator(CharacterBase character)
{
creator = character;
}
}
}