using BrewMonster.Scripts; using UnityEngine; namespace BrewMonster.PerfectWorld.Scripts.Vfx { public enum GFX_STATE { ST_STOP = 0, ST_PLAY = 1, ST_PAUSE = 2, ST_EDITMODE = 3 } public class BaseVfxObject : MonoBehaviour { [SerializeField] private ParticleSystem _vfx; private string _vfxPath; private Vector3 _originalScale = Vector3.one; private Vector3 _offset = Vector3.zero; private GFX_STATE m_iState; private void Awake() { if (_vfx == null) { _vfx = GetComponent(); } if (_vfx != null) { _originalScale = _vfx.transform.localScale; } } private void OnDestroy() { if(!string.IsNullOrEmpty(_vfxPath)) { AddressableManager.Instance.UnloadAsset(_vfxPath); } } public void Init(string vfxPath) { _vfxPath = vfxPath; } #region EC public GFX_STATE GetState() { return m_iState; } #endregion /// /// Set VFX scale /// public void SetScale(float scale) { SetScale(new Vector3(scale, scale, scale)); } /// /// Set VFX scale with vector /// public void SetScale(Vector3 scale) { if (_vfx != null) { _vfx.transform.localScale = Vector3.Scale(_originalScale, scale); // 调整粒子大小 / Adjust particle size var main = _vfx.main; main.startSizeMultiplier *= scale.magnitude / Mathf.Sqrt(3); } } /// /// Set VFX offset /// public void SetOffset(Vector3 offset) { _offset = offset; if (_vfx != null) { _vfx.transform.localPosition = offset; } } /// /// Set VFX world position /// public void SetPosition(Vector3 position) { if (_vfx != null) { _vfx.transform.position = position; } } /// /// Set VFX rotation /// public void SetRotation(Quaternion rotation) { if (_vfx != null) { _vfx.transform.rotation = rotation; } } /// /// Set VFX rotation with euler angles /// public void SetRotation(Vector3 eulerAngles) { SetRotation(Quaternion.Euler(eulerAngles)); } /// /// Set VFX color /// public void SetColor(Color color) { if (_vfx != null) { var main = _vfx.main; main.startColor = color; } } /// /// Set VFX duration /// public void SetDuration(float duration) { if (_vfx != null) { var main = _vfx.main; main.duration = duration; } } /// /// Set VFX playback speed /// public void SetSpeed(float speed) { if (_vfx != null) { var main = _vfx.main; main.simulationSpeed = speed; } } /// /// Play VFX /// public void Play() { Stop(); if (_vfx != null) { m_iState = GFX_STATE.ST_PLAY; _vfx.Play(); } } /// /// Play VFX with children /// public void PlayWithChildren() { if (_vfx != null) { m_iState = GFX_STATE.ST_PLAY; _vfx.Play(true); } } /// /// Stop VFX /// public void Stop() { if (_vfx != null) { m_iState = GFX_STATE.ST_STOP; _vfx.Stop(); } } /// /// Pause VFX /// public void Pause() { if (_vfx != null) { m_iState = GFX_STATE.ST_PAUSE; _vfx.Pause(); } } /// /// Clear all VFX particles /// public void Clear() { if (_vfx != null) { _vfx.Clear(); } } /// /// 重新开始播放 / Restart VFX /// public void Restart() { if (_vfx != null) { _vfx.Stop(); _vfx.Clear(); _vfx.Play(); } } /// /// 检查特效是否正在播放 / Check if VFX is playing /// public bool IsPlaying() { return _vfx != null && _vfx.isPlaying; } /// /// 检查特效是否已结束 / Check if VFX is stopped /// public bool IsStopped() { return _vfx != null && _vfx.isStopped; } /// /// 获取粒子系统 / Get ParticleSystem component /// public ParticleSystem GetParticleSystem() { return _vfx; } } }