StellarXipher/Assets/Sounds/Scripts/SoundFXManager.cs

41 lines
984 B
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);
}
}