StellarXipher/Assets/Sounds/Scripts/SoundFXManager.cs
EthanPisani 5585bacaca
All checks were successful
Build project / Build for (StandaloneLinux64, 6000.0.37f1) (push) Successful in 1h9m47s
Build project / Build for (StandaloneWindows64, 6000.0.37f1) (push) Successful in 22m28s
Build project / Publish to itch.io (StandaloneLinux64) (push) Successful in 16s
Build project / Publish to itch.io (StandaloneWindows64) (push) Successful in 14s
- remove ambient lighting - add alert sound
2025-02-26 14:33:27 -05:00

62 lines
1.6 KiB
C#

using UnityEngine;
using System.Collections;
using System.Collections.Generic;
public class SoundFXManager : MonoBehaviour
{
public static SoundFXManager instance;
[SerializeField] private AudioSource soundFXObject;
private void Awake()
{
if (instance == null)
{
instance = this;
}
}
public void PlaySound(AudioClip clip, Transform spawnPosition, float volume)
{
// spawn gameobject
AudioSource audioSource = Instantiate(soundFXObject, spawnPosition.position, Quaternion.identity);
// assign the audio clip
audioSource.clip = clip;
// set the volume
audioSource.volume = volume;
// play the sound
audioSource.Play();
// get the length of the clip
float clipLength = audioSource.clip.length;
// destroy the gameobject after the clip has finished playing
Destroy(audioSource.gameObject, clipLength);
}
public AudioSource PlayLoopingSound(AudioClip clip, Transform spawnPosition, float volume)
{
// Instantiate and configure audio source
AudioSource audioSource = Instantiate(soundFXObject, spawnPosition.position, Quaternion.identity);
audioSource.clip = clip;
audioSource.volume = volume;
audioSource.loop = true; // Enable looping
audioSource.Play();
return audioSource;
}
public void StopLoopingSound(AudioSource audioSource)
{
if (audioSource != null)
{
audioSource.Stop();
Destroy(audioSource.gameObject);
}
}
}