using UnityEngine; namespace SpellBoundAR.MusicalInstruments.Piano { [RequireComponent(typeof(ParticleSystem))] public class PianoKeyDownParticleSystem : MonoBehaviour { [SerializeField] private new ParticleSystem particleSystem; [SerializeField] private PianoKey pianoKey; private void Awake() { if (!particleSystem) particleSystem = GetComponent(); if (!pianoKey) pianoKey = GetComponentInParent(); } private void OnEnable() { if (pianoKey) pianoKey.OnDownChanged += Refresh; MusicalInstrumentsManager.OnParticleSystemsChanged += Refresh; Refresh(); } private void OnDisable() { if (pianoKey) pianoKey.OnDownChanged -= Refresh; MusicalInstrumentsManager.OnParticleSystemsChanged -= Refresh; } private void Refresh() { if (!particleSystem) return; if (MusicalInstrumentsManager.ParticleSystems && pianoKey && pianoKey.Down) { particleSystem.Play(); } else particleSystem.Stop(); } private void OnValidate() { if (!particleSystem) particleSystem = GetComponent(); if (!pianoKey) pianoKey = GetComponentInParent(); } } }