144 lines
4.2 KiB
C#
144 lines
4.2 KiB
C#
|
|
using System;
|
|||
|
|
using System.Collections.Generic;
|
|||
|
|
using UnityEngine;
|
|||
|
|
using UnityEngine.Audio;
|
|||
|
|
|
|||
|
|
public class AudioManager : MonoBehaviour
|
|||
|
|
{
|
|||
|
|
public static AudioManager Instance { get; private set; }
|
|||
|
|
|
|||
|
|
public List<AudioSource> pooledSources;
|
|||
|
|
public AudioSource sourceToPool;
|
|||
|
|
public int amountToPool;
|
|||
|
|
[SerializeField] AudioMixerGroup sfxMixer;
|
|||
|
|
[SerializeField] AudioMixerGroup musicMixer;
|
|||
|
|
|
|||
|
|
private void Awake()
|
|||
|
|
{
|
|||
|
|
Instance = this;
|
|||
|
|
audioSource = GetComponent<AudioSource>();
|
|||
|
|
}
|
|||
|
|
|
|||
|
|
|
|||
|
|
private void Start()
|
|||
|
|
{
|
|||
|
|
pooledSources = new List<AudioSource>();
|
|||
|
|
AudioSource tmp;
|
|||
|
|
for (int i = 0; i < amountToPool; i++)
|
|||
|
|
{
|
|||
|
|
tmp = Instantiate(sourceToPool);
|
|||
|
|
tmp.gameObject.SetActive(false);
|
|||
|
|
pooledSources.Add(tmp);
|
|||
|
|
}
|
|||
|
|
}
|
|||
|
|
|
|||
|
|
public AudioSource GetPooledSource()
|
|||
|
|
{
|
|||
|
|
for (int i = 0; i < amountToPool; i++)
|
|||
|
|
{
|
|||
|
|
if (!pooledSources[i].gameObject.activeInHierarchy)
|
|||
|
|
{
|
|||
|
|
return pooledSources[i];
|
|||
|
|
}
|
|||
|
|
}
|
|||
|
|
return null;
|
|||
|
|
}
|
|||
|
|
|
|||
|
|
public void playSong(string songTitle, bool loop)
|
|||
|
|
{
|
|||
|
|
if (songTitle == "")
|
|||
|
|
{
|
|||
|
|
audioSource.Stop();
|
|||
|
|
}
|
|||
|
|
audioSource.outputAudioMixerGroup = musicMixer;
|
|||
|
|
audioSource.clip = OnDemandLoader.Load<AudioClip>("Music/" + songTitle);
|
|||
|
|
audioSource.loop = loop;
|
|||
|
|
audioSource.Play();
|
|||
|
|
}
|
|||
|
|
public void stopSong()
|
|||
|
|
{
|
|||
|
|
audioSource.clip = null;
|
|||
|
|
audioSource.Stop();
|
|||
|
|
}
|
|||
|
|
|
|||
|
|
public void playSound(string sound, float pitch = 1.0f)
|
|||
|
|
{
|
|||
|
|
AudioSource source = GetPooledSource();
|
|||
|
|
if(source != null)
|
|||
|
|
{
|
|||
|
|
source.outputAudioMixerGroup = sfxMixer;
|
|||
|
|
source.transform.position = this.transform.position;
|
|||
|
|
source.clip = OnDemandLoader.Load<AudioClip>("Music/Sounds/" + sound);
|
|||
|
|
source.pitch = pitch;
|
|||
|
|
source.spatialBlend = 0; // 1 = 3D, 0 = 2D.
|
|||
|
|
source.spread = 0; //0 for mono
|
|||
|
|
source.gameObject.SetActive(true);
|
|||
|
|
source.Play();
|
|||
|
|
}
|
|||
|
|
|
|||
|
|
}
|
|||
|
|
|
|||
|
|
public void PlayOneShotSound(string sound, float pitch = 1.0f)
|
|||
|
|
{
|
|||
|
|
AudioSource source = GetPooledSource();
|
|||
|
|
if (source != null)
|
|||
|
|
{
|
|||
|
|
source.outputAudioMixerGroup = sfxMixer;
|
|||
|
|
source.transform.position = this.transform.position;
|
|||
|
|
source.clip = OnDemandLoader.Load<AudioClip>("Music/Sounds/" + sound);
|
|||
|
|
source.pitch = pitch;
|
|||
|
|
source.spatialBlend = 0; // 1 = 3D, 0 = 2D.
|
|||
|
|
source.spread = 0; //0 for mono
|
|||
|
|
source.gameObject.SetActive(true);
|
|||
|
|
source.PlayOneShot(source.clip);
|
|||
|
|
}
|
|||
|
|
|
|||
|
|
}
|
|||
|
|
|
|||
|
|
public void playSound3D(string sound, Vector3 position, float pitch = 1.0f)
|
|||
|
|
{
|
|||
|
|
AudioSource source = GetPooledSource();
|
|||
|
|
if (source != null)
|
|||
|
|
{
|
|||
|
|
source.outputAudioMixerGroup = sfxMixer;
|
|||
|
|
source.transform.position = position;
|
|||
|
|
source.clip = OnDemandLoader.Load<AudioClip>("Music/Sounds/" + sound);
|
|||
|
|
|
|||
|
|
bool isRocketExplosion = sound == "rocket_explosion";
|
|||
|
|
|
|||
|
|
source.pitch = pitch;
|
|||
|
|
source.spatialBlend = 1; // 1 = 3D, 0 = 2D.
|
|||
|
|
source.spread = 1; //1 for full stereo surround sound.
|
|||
|
|
source.rolloffMode = isRocketExplosion ? AudioRolloffMode.Linear : AudioRolloffMode.Logarithmic;
|
|||
|
|
source.minDistance = 3.0f;
|
|||
|
|
source.maxDistance = 500.0f;
|
|||
|
|
source.gameObject.SetActive(true);
|
|||
|
|
source.Play();
|
|||
|
|
}
|
|||
|
|
}
|
|||
|
|
|
|||
|
|
public void PlaySound(AudioClip clip, float pitch = 1.0f)
|
|||
|
|
{
|
|||
|
|
AudioSource source = GetPooledSource();
|
|||
|
|
if(source != null)
|
|||
|
|
{
|
|||
|
|
source.outputAudioMixerGroup = sfxMixer;
|
|||
|
|
source.clip = clip;
|
|||
|
|
source.pitch = pitch;
|
|||
|
|
source.gameObject.SetActive(true);
|
|||
|
|
source.PlayOneShot(clip);
|
|||
|
|
}
|
|||
|
|
}
|
|||
|
|
|
|||
|
|
public void DisableSource(AudioSource source)
|
|||
|
|
{
|
|||
|
|
source.Stop();
|
|||
|
|
source.clip = null;
|
|||
|
|
source.spatialBlend = 0;
|
|||
|
|
source.transform.position = AudioManager.Instance.transform.position;
|
|||
|
|
source.gameObject.SetActive(false);
|
|||
|
|
}
|
|||
|
|
|
|||
|
|
private AudioSource audioSource;
|
|||
|
|
private GameObject audioObject;
|
|||
|
|
}
|