Advertisement
RealiteeeyyyyTV

Untitled

Jan 4th, 2018
58
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.19 KB | None | 0 0
  1. using UnityEngine;
  2. using System.Collections;
  3.  
  4. public class AudioManager : MonoBehaviour {
  5.  
  6. public enum AudioChannel {Master, Sfx, Music};
  7.  
  8. public float masterVolumePercent { get; private set; }
  9. public float sfxVolumePercent { get; private set; }
  10. public float musicVolumePercent { get; private set; }
  11.  
  12. AudioSource sfx2DSource;
  13. AudioSource[] musicSources;
  14. int activeMusicSourceIndex;
  15.  
  16. public static AudioManager instance;
  17.  
  18. Transform audioListener;
  19.  
  20. SoundLibrary library;
  21.  
  22. void Awake() {
  23.  
  24. if (instance != null) {
  25. Destroy (gameObject);
  26. } else {
  27.  
  28. instance = this;
  29. DontDestroyOnLoad (gameObject);
  30.  
  31. library = GetComponent<SoundLibrary> ();
  32.  
  33. musicSources = new AudioSource[2];
  34. for (int i = 0; i < 2; i++) {
  35. GameObject newMusicSource = new GameObject ("Music source " + (i + 1));
  36. musicSources [i] = newMusicSource.AddComponent<AudioSource> ();
  37. newMusicSource.transform.parent = transform;
  38. }
  39. GameObject newSfx2Dsource = new GameObject ("2D sfx source");
  40. sfx2DSource = newSfx2Dsource.AddComponent<AudioSource> ();
  41. newSfx2Dsource.transform.parent = transform;
  42.  
  43. audioListener = FindObjectOfType<AudioListener> ().transform;
  44.  
  45. masterVolumePercent = PlayerPrefs.GetFloat ("master vol", 1);
  46. sfxVolumePercent = PlayerPrefs.GetFloat ("sfx vol", 1);
  47. musicVolumePercent = PlayerPrefs.GetFloat ("music vol",1);
  48. }
  49. }
  50.  
  51. void Update() {
  52.  
  53. }
  54.  
  55. public void SetVolume(float volumePercent, AudioChannel channel) {
  56. switch (channel) {
  57. case AudioChannel.Master:
  58. masterVolumePercent = volumePercent;
  59. break;
  60. case AudioChannel.Sfx:
  61. sfxVolumePercent = volumePercent;
  62. break;
  63. case AudioChannel.Music:
  64. musicVolumePercent = volumePercent;
  65. break;
  66. }
  67.  
  68. musicSources [0].volume = musicVolumePercent * masterVolumePercent;
  69. musicSources [1].volume = musicVolumePercent * masterVolumePercent;
  70.  
  71. PlayerPrefs.SetFloat ("master vol", masterVolumePercent);
  72. PlayerPrefs.SetFloat ("sfx vol", sfxVolumePercent);
  73. PlayerPrefs.SetFloat ("music vol", musicVolumePercent);
  74. }
  75.  
  76. public void PlayMusic(AudioClip clip, float fadeDuration = 1) {
  77. activeMusicSourceIndex = 1 - activeMusicSourceIndex;
  78. musicSources [activeMusicSourceIndex].clip = clip;
  79. musicSources [activeMusicSourceIndex].Play ();
  80.  
  81. StartCoroutine(AnimateMusicCrossfade(fadeDuration));
  82. }
  83.  
  84. public void PlaySound(AudioClip clip, Vector3 pos) {
  85. if (clip != null) {
  86. AudioSource.PlayClipAtPoint (clip, pos, sfxVolumePercent * masterVolumePercent);
  87. }
  88. }
  89.  
  90. public void PlaySound(string soundName, Vector3 pos) {
  91. PlaySound (library.GetClipFromName (soundName), pos);
  92. }
  93.  
  94. public void PlaySound2D(string soundName) {
  95. sfx2DSource.PlayOneShot (library.GetClipFromName (soundName), sfxVolumePercent * masterVolumePercent);
  96. }
  97.  
  98.  
  99. IEnumerator AnimateMusicCrossfade(float duration) {
  100. float percent = 0;
  101.  
  102. while (percent < 1) {
  103. percent += Time.deltaTime * 1 / duration;
  104. musicSources [activeMusicSourceIndex].volume = Mathf.Lerp (0, musicVolumePercent * masterVolumePercent, percent);
  105. musicSources [1-activeMusicSourceIndex].volume = Mathf.Lerp (musicVolumePercent * masterVolumePercent, 0, percent);
  106. yield return null;
  107. }
  108. }
  109. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement