Danro/Assets/Scripts/FX/AmbientVFXController.cs

65 lines
2.2 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 UnityEngine.VFX;
namespace RR.Game2D.FX
{
public class AmbientVFXController : MonoBehaviour
{
[SerializeField] private VisualEffect vfx;
private VisualEffectAsset _currentVFXAsset;
private void Start()
{
// Инициализация VFX компонента и установка текущего эффекта
_currentVFXAsset = null;
if (!vfx) vfx = GetComponent<VisualEffect>();
SetVFXAsset(vfx.visualEffectAsset);
// Воспроизведение текущего VFX эффекта при старте
PlayCurrentVFX();
}
/// <summary>
/// Метод для воспроизведения текущего VFX эффекта
/// </summary>
private void PlayCurrentVFX()
{
// Если VFX компонент или текущий эффект не установлены, выходим из метода
if (!vfx) return;
if (!_currentVFXAsset) return;
vfx.Play();
}
/// <summary>
/// Метод для остановки текущего VFX эффекта
/// </summary>
private void StopCurrentVFX()
{
vfx.Stop();
}
/// <summary>
/// Метод для установки нового VFX эффекта
/// </summary>
/// <param name="newVFXAsset">Новый VFX эффект</param>
public void SetVFXAsset(VisualEffectAsset newVFXAsset)
{
// Если новый эффект совпадает с текущим, выходим из метода
if (newVFXAsset == _currentVFXAsset) return;
StopCurrentVFX();
// Обновление текущего эффекта и установка его в VFX компонент
_currentVFXAsset = newVFXAsset;
vfx.visualEffectAsset = _currentVFXAsset;
// Если новый эффект не установлен, выходим из метода
if (!newVFXAsset) return;
PlayCurrentVFX();
}
}
}