Commit 2f9b5166 authored by Gerard Gascón's avatar Gerard Gascón
Browse files

feature: Created audio manager

parent cb907bc7
Loading
Loading
Loading
Loading

Assets/Jokes/test.txt

deleted100644 → 0
+0 −1
Original line number Diff line number Diff line
aaaccc|van 2:1234|y se cae el del medio
 No newline at end of file
+367 −1
Original line number Diff line number Diff line
@@ -1438,6 +1438,81 @@ RectTransform:
  m_CorrespondingSourceObject: {fileID: 5783143251392024687, guid: 5430324ec74b5f04581d090658d90b3e, type: 3}
  m_PrefabInstance: {fileID: 804374705}
  m_PrefabAsset: {fileID: 0}
--- !u!1 &843204719
GameObject:
  m_ObjectHideFlags: 0
  m_CorrespondingSourceObject: {fileID: 0}
  m_PrefabInstance: {fileID: 0}
  m_PrefabAsset: {fileID: 0}
  serializedVersion: 6
  m_Component:
  - component: {fileID: 843204720}
  - component: {fileID: 843204722}
  - component: {fileID: 843204721}
  m_Layer: 5
  m_Name: Progress
  m_TagString: Untagged
  m_Icon: {fileID: 0}
  m_NavMeshLayer: 0
  m_StaticEditorFlags: 0
  m_IsActive: 1
--- !u!224 &843204720
RectTransform:
  m_ObjectHideFlags: 0
  m_CorrespondingSourceObject: {fileID: 0}
  m_PrefabInstance: {fileID: 0}
  m_PrefabAsset: {fileID: 0}
  m_GameObject: {fileID: 843204719}
  m_LocalRotation: {x: -0, y: -0, z: -0, w: 1}
  m_LocalPosition: {x: 0, y: 0, z: 0}
  m_LocalScale: {x: 1, y: 1, z: 1}
  m_ConstrainProportionsScale: 0
  m_Children: []
  m_Father: {fileID: 1540972244}
  m_LocalEulerAnglesHint: {x: 0, y: 0, z: 0}
  m_AnchorMin: {x: 0, y: 1}
  m_AnchorMax: {x: 0, y: 1}
  m_AnchoredPosition: {x: 344, y: -69.000015}
  m_SizeDelta: {x: 15, y: 177}
  m_Pivot: {x: 0, y: 1}
--- !u!114 &843204721
MonoBehaviour:
  m_ObjectHideFlags: 0
  m_CorrespondingSourceObject: {fileID: 0}
  m_PrefabInstance: {fileID: 0}
  m_PrefabAsset: {fileID: 0}
  m_GameObject: {fileID: 843204719}
  m_Enabled: 1
  m_EditorHideFlags: 0
  m_Script: {fileID: 11500000, guid: fe87c0e1cc204ed48ad3b37840f39efc, type: 3}
  m_Name: 
  m_EditorClassIdentifier: 
  m_Material: {fileID: 0}
  m_Color: {r: 1, g: 1, b: 1, a: 1}
  m_RaycastTarget: 1
  m_RaycastPadding: {x: 0, y: 0, z: 0, w: 0}
  m_Maskable: 1
  m_OnCullStateChanged:
    m_PersistentCalls:
      m_Calls: []
  m_Sprite: {fileID: -791923143, guid: 05014e62dbe8d3e41800f5cf0f739c9e, type: 3}
  m_Type: 3
  m_PreserveAspect: 0
  m_FillCenter: 1
  m_FillMethod: 1
  m_FillAmount: 1
  m_FillClockwise: 1
  m_FillOrigin: 0
  m_UseSpriteMesh: 0
  m_PixelsPerUnitMultiplier: 6.25
--- !u!222 &843204722
CanvasRenderer:
  m_ObjectHideFlags: 0
  m_CorrespondingSourceObject: {fileID: 0}
  m_PrefabInstance: {fileID: 0}
  m_PrefabAsset: {fileID: 0}
  m_GameObject: {fileID: 843204719}
  m_CullTransparentMesh: 1
--- !u!1 &861656303
GameObject:
  m_ObjectHideFlags: 0
@@ -2103,7 +2178,7 @@ GameObject:
  m_Icon: {fileID: 0}
  m_NavMeshLayer: 0
  m_StaticEditorFlags: 0
  m_IsActive: 0
  m_IsActive: 1
--- !u!225 &1198578935
CanvasGroup:
  m_ObjectHideFlags: 0
@@ -2387,6 +2462,183 @@ MonoBehaviour:
  m_Script: {fileID: 11500000, guid: f4688fdb7df04437aeb418b961361dc5, type: 3}
  m_Name: 
  m_EditorClassIdentifier: 
--- !u!1 &1352489076
GameObject:
  m_ObjectHideFlags: 0
  m_CorrespondingSourceObject: {fileID: 0}
  m_PrefabInstance: {fileID: 0}
  m_PrefabAsset: {fileID: 0}
  serializedVersion: 6
  m_Component:
  - component: {fileID: 1352489080}
  - component: {fileID: 1352489079}
  - component: {fileID: 1352489078}
  - component: {fileID: 1352489077}
  m_Layer: 5
  m_Name: ProgressCanvas
  m_TagString: Untagged
  m_Icon: {fileID: 0}
  m_NavMeshLayer: 0
  m_StaticEditorFlags: 0
  m_IsActive: 1
--- !u!114 &1352489077
MonoBehaviour:
  m_ObjectHideFlags: 0
  m_CorrespondingSourceObject: {fileID: 0}
  m_PrefabInstance: {fileID: 0}
  m_PrefabAsset: {fileID: 0}
  m_GameObject: {fileID: 1352489076}
  m_Enabled: 1
  m_EditorHideFlags: 0
  m_Script: {fileID: 11500000, guid: dc42784cf147c0c48a680349fa168899, type: 3}
  m_Name: 
  m_EditorClassIdentifier: 
  m_IgnoreReversedGraphics: 1
  m_BlockingObjects: 0
  m_BlockingMask:
    serializedVersion: 2
    m_Bits: 4294967295
--- !u!114 &1352489078
MonoBehaviour:
  m_ObjectHideFlags: 0
  m_CorrespondingSourceObject: {fileID: 0}
  m_PrefabInstance: {fileID: 0}
  m_PrefabAsset: {fileID: 0}
  m_GameObject: {fileID: 1352489076}
  m_Enabled: 1
  m_EditorHideFlags: 0
  m_Script: {fileID: 11500000, guid: 0cd44c1031e13a943bb63640046fad76, type: 3}
  m_Name: 
  m_EditorClassIdentifier: 
  m_UiScaleMode: 1
  m_ReferencePixelsPerUnit: 100
  m_ScaleFactor: 1
  m_ReferenceResolution: {x: 480, y: 270}
  m_ScreenMatchMode: 0
  m_MatchWidthOrHeight: 1
  m_PhysicalUnit: 3
  m_FallbackScreenDPI: 96
  m_DefaultSpriteDPI: 96
  m_DynamicPixelsPerUnit: 1
  m_PresetInfoIsWorld: 0
--- !u!223 &1352489079
Canvas:
  m_ObjectHideFlags: 0
  m_CorrespondingSourceObject: {fileID: 0}
  m_PrefabInstance: {fileID: 0}
  m_PrefabAsset: {fileID: 0}
  m_GameObject: {fileID: 1352489076}
  m_Enabled: 1
  serializedVersion: 3
  m_RenderMode: 0
  m_Camera: {fileID: 0}
  m_PlaneDistance: 100
  m_PixelPerfect: 0
  m_ReceivesEvents: 1
  m_OverrideSorting: 0
  m_OverridePixelPerfect: 0
  m_SortingBucketNormalizedSize: 0
  m_VertexColorAlwaysGammaSpace: 0
  m_AdditionalShaderChannelsFlag: 0
  m_UpdateRectTransformForStandalone: 0
  m_SortingLayerID: 0
  m_SortingOrder: 1
  m_TargetDisplay: 0
--- !u!224 &1352489080
RectTransform:
  m_ObjectHideFlags: 0
  m_CorrespondingSourceObject: {fileID: 0}
  m_PrefabInstance: {fileID: 0}
  m_PrefabAsset: {fileID: 0}
  m_GameObject: {fileID: 1352489076}
  m_LocalRotation: {x: 0, y: 0, z: 0, w: 1}
  m_LocalPosition: {x: 0, y: 0, z: 0}
  m_LocalScale: {x: 0, y: 0, z: 0}
  m_ConstrainProportionsScale: 0
  m_Children:
  - {fileID: 1540972244}
  m_Father: {fileID: 0}
  m_LocalEulerAnglesHint: {x: 0, y: 0, z: 0}
  m_AnchorMin: {x: 0, y: 0}
  m_AnchorMax: {x: 0, y: 0}
  m_AnchoredPosition: {x: 0, y: 0}
  m_SizeDelta: {x: 0, y: 0}
  m_Pivot: {x: 0, y: 0}
--- !u!1 &1488949019
GameObject:
  m_ObjectHideFlags: 0
  m_CorrespondingSourceObject: {fileID: 0}
  m_PrefabInstance: {fileID: 0}
  m_PrefabAsset: {fileID: 0}
  serializedVersion: 6
  m_Component:
  - component: {fileID: 1488949020}
  - component: {fileID: 1488949022}
  - component: {fileID: 1488949021}
  m_Layer: 5
  m_Name: Border
  m_TagString: Untagged
  m_Icon: {fileID: 0}
  m_NavMeshLayer: 0
  m_StaticEditorFlags: 0
  m_IsActive: 1
--- !u!224 &1488949020
RectTransform:
  m_ObjectHideFlags: 0
  m_CorrespondingSourceObject: {fileID: 0}
  m_PrefabInstance: {fileID: 0}
  m_PrefabAsset: {fileID: 0}
  m_GameObject: {fileID: 1488949019}
  m_LocalRotation: {x: -0, y: -0, z: -0, w: 1}
  m_LocalPosition: {x: 0, y: 0, z: 0}
  m_LocalScale: {x: 1, y: 1, z: 1}
  m_ConstrainProportionsScale: 0
  m_Children: []
  m_Father: {fileID: 1540972244}
  m_LocalEulerAnglesHint: {x: 0, y: 0, z: 0}
  m_AnchorMin: {x: 0, y: 1}
  m_AnchorMax: {x: 0, y: 1}
  m_AnchoredPosition: {x: 343, y: -68}
  m_SizeDelta: {x: 17, y: 180}
  m_Pivot: {x: 0, y: 1}
--- !u!114 &1488949021
MonoBehaviour:
  m_ObjectHideFlags: 0
  m_CorrespondingSourceObject: {fileID: 0}
  m_PrefabInstance: {fileID: 0}
  m_PrefabAsset: {fileID: 0}
  m_GameObject: {fileID: 1488949019}
  m_Enabled: 1
  m_EditorHideFlags: 0
  m_Script: {fileID: 11500000, guid: fe87c0e1cc204ed48ad3b37840f39efc, type: 3}
  m_Name: 
  m_EditorClassIdentifier: 
  m_Material: {fileID: 0}
  m_Color: {r: 1, g: 1, b: 1, a: 1}
  m_RaycastTarget: 1
  m_RaycastPadding: {x: 0, y: 0, z: 0, w: 0}
  m_Maskable: 1
  m_OnCullStateChanged:
    m_PersistentCalls:
      m_Calls: []
  m_Sprite: {fileID: 21300000, guid: b09081262d17c3044b0775a2be56d51d, type: 3}
  m_Type: 1
  m_PreserveAspect: 0
  m_FillCenter: 1
  m_FillMethod: 4
  m_FillAmount: 1
  m_FillClockwise: 1
  m_FillOrigin: 0
  m_UseSpriteMesh: 0
  m_PixelsPerUnitMultiplier: 6.25
--- !u!222 &1488949022
CanvasRenderer:
  m_ObjectHideFlags: 0
  m_CorrespondingSourceObject: {fileID: 0}
  m_PrefabInstance: {fileID: 0}
  m_PrefabAsset: {fileID: 0}
  m_GameObject: {fileID: 1488949019}
  m_CullTransparentMesh: 1
--- !u!1 &1539763154
GameObject:
  m_ObjectHideFlags: 0
@@ -2521,6 +2773,44 @@ CanvasRenderer:
  m_PrefabAsset: {fileID: 0}
  m_GameObject: {fileID: 1539763154}
  m_CullTransparentMesh: 1
--- !u!1 &1540972243
GameObject:
  m_ObjectHideFlags: 0
  m_CorrespondingSourceObject: {fileID: 0}
  m_PrefabInstance: {fileID: 0}
  m_PrefabAsset: {fileID: 0}
  serializedVersion: 6
  m_Component:
  - component: {fileID: 1540972244}
  m_Layer: 5
  m_Name: Container
  m_TagString: Untagged
  m_Icon: {fileID: 0}
  m_NavMeshLayer: 0
  m_StaticEditorFlags: 0
  m_IsActive: 1
--- !u!224 &1540972244
RectTransform:
  m_ObjectHideFlags: 0
  m_CorrespondingSourceObject: {fileID: 0}
  m_PrefabInstance: {fileID: 0}
  m_PrefabAsset: {fileID: 0}
  m_GameObject: {fileID: 1540972243}
  m_LocalRotation: {x: 0, y: 0, z: 0, w: 1}
  m_LocalPosition: {x: 0, y: 0, z: 0}
  m_LocalScale: {x: 1, y: 1, z: 1}
  m_ConstrainProportionsScale: 0
  m_Children:
  - {fileID: 1639838825}
  - {fileID: 843204720}
  - {fileID: 1488949020}
  m_Father: {fileID: 1352489080}
  m_LocalEulerAnglesHint: {x: 0, y: 0, z: 0}
  m_AnchorMin: {x: 0.5, y: 0}
  m_AnchorMax: {x: 0.5, y: 1}
  m_AnchoredPosition: {x: 0, y: 0}
  m_SizeDelta: {x: 480, y: 0}
  m_Pivot: {x: 0.5, y: 0.5}
--- !u!114 &1620770958 stripped
MonoBehaviour:
  m_CorrespondingSourceObject: {fileID: 5596010678248324506, guid: 5430324ec74b5f04581d090658d90b3e, type: 3}
@@ -2653,6 +2943,81 @@ MonoBehaviour:
  - {fileID: 590262795}
  correctLive: {fileID: -773788709396254583, guid: 128ff2869c2580344a74d2dc6b9d9e6a, type: 3}
  wrongLive: {fileID: -8337258921713572487, guid: 128ff2869c2580344a74d2dc6b9d9e6a, type: 3}
--- !u!1 &1639838824
GameObject:
  m_ObjectHideFlags: 0
  m_CorrespondingSourceObject: {fileID: 0}
  m_PrefabInstance: {fileID: 0}
  m_PrefabAsset: {fileID: 0}
  serializedVersion: 6
  m_Component:
  - component: {fileID: 1639838825}
  - component: {fileID: 1639838827}
  - component: {fileID: 1639838826}
  m_Layer: 5
  m_Name: Background
  m_TagString: Untagged
  m_Icon: {fileID: 0}
  m_NavMeshLayer: 0
  m_StaticEditorFlags: 0
  m_IsActive: 1
--- !u!224 &1639838825
RectTransform:
  m_ObjectHideFlags: 0
  m_CorrespondingSourceObject: {fileID: 0}
  m_PrefabInstance: {fileID: 0}
  m_PrefabAsset: {fileID: 0}
  m_GameObject: {fileID: 1639838824}
  m_LocalRotation: {x: -0, y: -0, z: -0, w: 1}
  m_LocalPosition: {x: 0, y: 0, z: 0}
  m_LocalScale: {x: 1, y: 1, z: 1}
  m_ConstrainProportionsScale: 0
  m_Children: []
  m_Father: {fileID: 1540972244}
  m_LocalEulerAnglesHint: {x: 0, y: 0, z: 0}
  m_AnchorMin: {x: 0, y: 1}
  m_AnchorMax: {x: 0, y: 1}
  m_AnchoredPosition: {x: 343, y: -68}
  m_SizeDelta: {x: 17, y: 180}
  m_Pivot: {x: 0, y: 1}
--- !u!114 &1639838826
MonoBehaviour:
  m_ObjectHideFlags: 0
  m_CorrespondingSourceObject: {fileID: 0}
  m_PrefabInstance: {fileID: 0}
  m_PrefabAsset: {fileID: 0}
  m_GameObject: {fileID: 1639838824}
  m_Enabled: 1
  m_EditorHideFlags: 0
  m_Script: {fileID: 11500000, guid: fe87c0e1cc204ed48ad3b37840f39efc, type: 3}
  m_Name: 
  m_EditorClassIdentifier: 
  m_Material: {fileID: 0}
  m_Color: {r: 1, g: 1, b: 1, a: 1}
  m_RaycastTarget: 1
  m_RaycastPadding: {x: 0, y: 0, z: 0, w: 0}
  m_Maskable: 1
  m_OnCullStateChanged:
    m_PersistentCalls:
      m_Calls: []
  m_Sprite: {fileID: 21300000, guid: 444382c0ca76cb04abc6852587ee6fb0, type: 3}
  m_Type: 1
  m_PreserveAspect: 0
  m_FillCenter: 1
  m_FillMethod: 4
  m_FillAmount: 1
  m_FillClockwise: 1
  m_FillOrigin: 0
  m_UseSpriteMesh: 0
  m_PixelsPerUnitMultiplier: 6.25
--- !u!222 &1639838827
CanvasRenderer:
  m_ObjectHideFlags: 0
  m_CorrespondingSourceObject: {fileID: 0}
  m_PrefabInstance: {fileID: 0}
  m_PrefabAsset: {fileID: 0}
  m_GameObject: {fileID: 1639838824}
  m_CullTransparentMesh: 1
--- !u!1 &1717907689
GameObject:
  m_ObjectHideFlags: 0
@@ -3645,4 +4010,5 @@ SceneRoots:
  - {fileID: 1741129169}
  - {fileID: 23275630}
  - {fileID: 1637937848}
  - {fileID: 1352489080}
  - {fileID: 1198578939}
+3 −2
Original line number Diff line number Diff line
fileFormatVersion: 2
guid: 709db39fbdad6d64d913304ea83d7f85
TextScriptImporter:
guid: 1f323de77930b4a45a85c388886626ee
folderAsset: yes
DefaultImporter:
  externalObjects: {}
  userData: 
  assetBundleName: 
+294 −0
Original line number Diff line number Diff line
using System;
using System.Collections;
using UnityEngine;

namespace Audio {
	public class AudioManager : MonoBehaviour {

		public static AudioManager instance;

		[SerializeField] private Sounds soundList;

		private void Awake() {
			if (instance == null) {
				instance = this;
			} else {
				Destroy(gameObject);
				return;
			}
			DontDestroyOnLoad(gameObject);

			foreach (Sounds.List s in soundList.sounds) {
				if (string.IsNullOrEmpty(s.name) || string.IsNullOrWhiteSpace(s.name)) {
					Debug.LogWarning("The name one sound is empty");
					continue;
				}

				GameObject sound = new(s.name) {
					transform = {
						parent = transform
					}
				};
				s.source = sound.AddComponent<AudioSource>();

				if (soundList.mainMixer && soundList.sfxMixer) {
					s.source.outputAudioMixerGroup = s.type == Sounds.List.Type.Music ? soundList.mainMixer : soundList.sfxMixer;
				}

				s.source.clip = s.clip;

				s.source.volume = s.volume;
				s.source.pitch = s.pitch;
				s.source.loop = s.loop;
			}
		}

		#region Play
		/// <summary>Use this to play a sound with a specific name
		/// <para>It has to be in the Sound asset referenced in the AudioManager instance</para>
		/// </summary>
		/// <param name="name" type="string">The name of the sound</param>
		public void Play(string name) {
			Sounds.List s = Array.Find(soundList.sounds, sound => sound.name == name);
			if (s == null) {
				Debug.LogWarning("Sound: " + name + " not found!");
				return;
			}
			s.source.pitch = s.RandomPitch;
			s.source.volume = s.RandomVolume;
			s.source.Play();
		}
		/// <summary>Use this to play a sound with a specific name and with a certain delay
		/// <para>It has to be in the Sound asset referenced in the AudioManager instance</para>
		/// </summary>
		/// <param name="name" type="string">The name of the sound</param>
		/// <param name="delay" type="float">The delay in seconds</param>
		public void Play(string name, float delay) {
			Sounds.List s = Array.Find(soundList.sounds, sound => sound.name == name);
			if (s == null) {
				Debug.LogWarning("Sound: " + name + " not found!");
				return;
			}
			s.source.pitch = s.RandomPitch;
			s.source.volume = s.RandomVolume;
			s.source.PlayDelayed(delay);
		}
		/// <summary>Use this to play one shot of a sound with a specific name
		/// <para>It has to be in the Sound asset referenced in the AudioManager instance</para>
		/// </summary>
		/// <param name="name" type="string">The name of the sound</param>
		public void PlayOneShot(string name) {
			Sounds.List s = Array.Find(soundList.sounds, sound => sound.name == name);
			if (s == null) {
				Debug.LogWarning("Sound: " + name + " not found!");
				return;
			}
			s.source.pitch = s.RandomPitch;
			s.source.volume = s.RandomVolume;
			s.source.PlayOneShot(s.clip);
		}
		/// <summary>Use this to play an intro song and then start playing the song loop
		/// <para>It has to be in the Sound asset referenced in the AudioManager instance</para>
		/// </summary>
		/// <param name="intro" type="string">The name of the intro song</param>
		/// <param name="song" type="string">The name of the song loop</param>
		public void PlayWithIntro(string intro, string song) {
			Sounds.List s = Array.Find(soundList.sounds, sound => sound.name == intro);
			if (s == null) {
				Debug.LogWarning("Sound: " + intro + " not found!");
				return;
			}
			s.source.pitch = s.RandomPitch;
			s.source.volume = s.RandomVolume;
			s.source.Play();

			float introDuration = s.clip.length;
			Play(song, introDuration);
		}
		/// <summary>Use this to play one shot of a random sound within a list
		/// <para>They have to be in the Sound asset referenced in the AudioManager instance</para>
		/// </summary>
		/// <param name="names" type="string[]">The names of the sounds</param>
		public void PlayRandomSound(params string[] names) {
			int random = UnityEngine.Random.Range(0, names.Length);
			PlayOneShot(names[random]);
		}
		#endregion
		#region Pause
		/// <summary>Use this to pause a sound with a specific name
		/// <para>It has to be in the Sound asset referenced in the AudioManager instance</para>
		/// </summary>
		/// <param name="name" type="string">The name of the sound</param>
		public void Pause(string name) {
			Sounds.List s = Array.Find(soundList.sounds, sound => sound.name == name);
			if (s == null) {
				Debug.LogWarning("Sound: " + name + " not found!");
				return;
			}
			s.source.pitch = s.RandomPitch;
			s.source.volume = s.RandomVolume;
			s.source.Pause();
		}
		/// <summary>Use this to unpause a sound with a specific name
		/// <para>It has to be in the Sound asset referenced in the AudioManager instance</para>
		/// </summary>
		/// <param name="name" type="string">The name of the sound</param>
		public void UnPause(string name) {
			Sounds.List s = Array.Find(soundList.sounds, sound => sound.name == name);
			if (s == null) {
				Debug.LogWarning("Sound: " + name + " not found!");
				return;
			}
			s.source.pitch = s.RandomPitch;
			s.source.volume = s.RandomVolume;
			s.source.UnPause();
		}
		#endregion
		#region Stop
		/// <summary>Use this to stop a sound with a specific name
		/// <para>It has to be in the Sound asset referenced in the AudioManager instance</para>
		/// </summary>
		/// <param name="name" type="string">The name of the sound</param>
		public void Stop(string name) {
			Sounds.List s = Array.Find(soundList.sounds, sound => sound.name == name);
			if (s == null) {
				Debug.LogWarning("Sound: " + name + " not found!");
				return;
			}
			s.source.pitch = s.RandomPitch;
			s.source.volume = s.RandomVolume;
			s.source.Stop();
		}
		/// <summary>Use this to stop all the sounds
		/// <para>It has to be in the Sound asset referenced in the AudioManager instance</para>
		/// </summary>
		public void StopAll() {
			foreach (Sounds.List s in soundList.sounds) {
				if (s.source) {
					s.source.Stop();
				}
			}
		}
		#endregion
		/// <summary>This function returns the AudioSource that contains a specific sound
		/// <para>It has to be in the Sound asset referenced in the AudioManager instance</para>
		/// </summary>
		/// <param name="name">The name of the sound</param>
		/// <returns>The AudioSource in the scene</returns>
		public AudioSource GetSource(string name) {
			Sounds.List s = Array.Find(soundList.sounds, sound => sound.name == name);
			if (s == null) {
				Debug.LogWarning("Sound: " + name + " not found!");
				return null;
			}
			return s.source;
		}
		#region Fades
		/// <summary>Use this to start playing a sound with a fade in
		/// <para>It has to be in the Sound asset referenced in the AudioManager instance</para>
		/// </summary>
		/// <param name="name" type="string">The name of the sound</param>
		/// <param name="duration" type="float">The duration of the fade in</param>
		public void FadeIn(string name, float duration) {
			StartCoroutine(FadeInCoroutine(name, duration));
		}
		IEnumerator FadeInCoroutine(string name, float fadeTime) {
			AudioSource audioSource = GetSource(name);
			if (audioSource != null && !audioSource.isPlaying) {
				float volume = audioSource.volume;
				audioSource.volume = 0;
				audioSource.Play();
				while (audioSource.volume < volume) {
					audioSource.volume += Time.deltaTime / fadeTime;
					yield return null;
				}

				audioSource.volume = volume;
			}
		}
		/// <summary>Use this to stop playing a sound with a fade out
		/// <para>It has to be in the Sound asset referenced in the AudioManager instance</para>
		/// </summary>
		/// <param name="name" type="string">The name of the sound</param>
		/// <param name="duration" type="float">The duration of the fade out</param>
		public void FadeOut(string name, float duration) {
			StartCoroutine(FadeOutCoroutine(name, duration));
		}

		private IEnumerator FadeOutCoroutine(string name, float fadeTime) {
			AudioSource audioSource = GetSource(name);

			if (!audioSource || !audioSource.isPlaying) yield break;
			float startVolume = audioSource.volume;

			while (audioSource.volume > 0) {
				audioSource.volume -= startVolume * Time.deltaTime / fadeTime;
				yield return null;
			}

			audioSource.Stop();
			audioSource.volume = startVolume;
		}

		/// <summary>Use this to start playing a sound muted
		/// <para>It has to be in the Sound asset referenced in the AudioManager instance</para>
		/// </summary>
		/// <param name="name" type="string">The name of the sound</param>
		public void PlayMuted(string name) {
			Sounds.List s = Array.Find(soundList.sounds, sound => sound.name == name);
			if (s == null) {
				Debug.LogWarning("Sound: " + name + " not found!");
				return;
			}
			s.source.pitch = s.RandomPitch;
			s.source.volume = 0f;
			s.source.Play();
		}
		/// <summary>Use this to fade in a sound that is currently muted
		/// <para>It has to be in the Sound asset referenced in the AudioManager instance</para>
		/// <para>WARNING: If the PlayMuted hasn't been called before, this function won't work</para>
		/// </summary>
		/// <param name="name" type="string">The name of the sound</param>
		/// <param name="duration">The duration of the fade in</param>
		public void FadeMutedIn(string name, float duration) {
			StartCoroutine(FadeMutedInCoroutine(name, duration));
		}

		private IEnumerator FadeMutedInCoroutine(string name, float fadeTime) {
			Sounds.List s = Array.Find(soundList.sounds, sound => sound.name == name);
			if (s == null) {
				Debug.LogWarning("Sound: " + name + " not found!");
				yield break;
			}

			while (s.source.volume < s.volume) {
				s.source.volume += Time.deltaTime / fadeTime;
				yield return null;
			}
			s.source.volume = s.volume;
		}
		/// <summary>Use this to fade out a sound and keep playing that muted
		/// <para>It has to be in the Sound asset referenced in the AudioManager instance</para>
		/// </summary>
		/// <param name="name" type="string">The name of the sound</param>
		/// <param name="duration">The duration of the fade out</param>
		public void FadeMutedOut(string name, float duration) {
			StartCoroutine(FadeMutedOutCoroutine(name, duration));
		}

		private IEnumerator FadeMutedOutCoroutine(string name, float fadeTime) {
			Sounds.List s = Array.Find(soundList.sounds, sound => sound.name == name);
			if (s == null) {
				Debug.LogWarning("Sound: " + name + " not found!");
				yield break;
			}

			while (s.source.volume > 0) {
				s.source.volume -= Time.deltaTime / fadeTime;
				yield return null;
			}
			s.source.volume = 0;
		}
		#endregion
	}
}
 No newline at end of file
+11 −0
Original line number Diff line number Diff line
fileFormatVersion: 2
guid: b824c4cae81b29e41acd7881949b2789
MonoImporter:
  externalObjects: {}
  serializedVersion: 2
  defaultReferences: []
  executionOrder: 0
  icon: {instanceID: 0}
  userData: 
  assetBundleName: 
  assetBundleVariant: 
Loading