diff --git a/Assets/Resources/Prefabs/Sound.meta b/Assets/Resources/Prefabs/Sound.meta new file mode 100644 index 0000000..b1b7ab6 --- /dev/null +++ b/Assets/Resources/Prefabs/Sound.meta @@ -0,0 +1,8 @@ +fileFormatVersion: 2 +guid: 48dcd50c6eacab640bcf38649d7f3a7d +folderAsset: yes +DefaultImporter: + externalObjects: {} + userData: + assetBundleName: + assetBundleVariant: diff --git a/Assets/Resources/Prefabs/Sound/Audio Source.prefab b/Assets/Resources/Prefabs/Sound/Audio Source.prefab new file mode 100644 index 0000000..64ff56d --- /dev/null +++ b/Assets/Resources/Prefabs/Sound/Audio Source.prefab @@ -0,0 +1,131 @@ +%YAML 1.1 +%TAG !u! tag:unity3d.com,2011: +--- !u!1 &7070934177775682985 +GameObject: + m_ObjectHideFlags: 0 + m_CorrespondingSourceObject: {fileID: 0} + m_PrefabInstance: {fileID: 0} + m_PrefabAsset: {fileID: 0} + serializedVersion: 6 + m_Component: + - component: {fileID: 944501444550833281} + - component: {fileID: 3317408524915037535} + m_Layer: 0 + m_Name: Audio Source + m_TagString: Untagged + m_Icon: {fileID: 0} + m_NavMeshLayer: 0 + m_StaticEditorFlags: 0 + m_IsActive: 1 +--- !u!4 &944501444550833281 +Transform: + m_ObjectHideFlags: 0 + m_CorrespondingSourceObject: {fileID: 0} + m_PrefabInstance: {fileID: 0} + m_PrefabAsset: {fileID: 0} + m_GameObject: {fileID: 7070934177775682985} + serializedVersion: 2 + 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: 0} + m_LocalEulerAnglesHint: {x: 0, y: 0, z: 0} +--- !u!82 &3317408524915037535 +AudioSource: + m_ObjectHideFlags: 0 + m_CorrespondingSourceObject: {fileID: 0} + m_PrefabInstance: {fileID: 0} + m_PrefabAsset: {fileID: 0} + m_GameObject: {fileID: 7070934177775682985} + m_Enabled: 1 + serializedVersion: 4 + OutputAudioMixerGroup: {fileID: 0} + m_audioClip: {fileID: 0} + m_Resource: {fileID: 0} + m_PlayOnAwake: 1 + m_Volume: 1 + m_Pitch: 1 + Loop: 0 + Mute: 0 + Spatialize: 0 + SpatializePostEffects: 0 + Priority: 128 + DopplerLevel: 1 + MinDistance: 1 + MaxDistance: 500 + Pan2D: 0 + rolloffMode: 0 + BypassEffects: 0 + BypassListenerEffects: 0 + BypassReverbZones: 0 + rolloffCustomCurve: + serializedVersion: 2 + m_Curve: + - serializedVersion: 3 + time: 0 + value: 1 + inSlope: 0 + outSlope: 0 + tangentMode: 0 + weightedMode: 0 + inWeight: 0.33333334 + outWeight: 0.33333334 + - serializedVersion: 3 + time: 1 + value: 0 + inSlope: 0 + outSlope: 0 + tangentMode: 0 + weightedMode: 0 + inWeight: 0.33333334 + outWeight: 0.33333334 + m_PreInfinity: 2 + m_PostInfinity: 2 + m_RotationOrder: 4 + panLevelCustomCurve: + serializedVersion: 2 + m_Curve: + - serializedVersion: 3 + time: 0 + value: 0 + inSlope: 0 + outSlope: 0 + tangentMode: 0 + weightedMode: 0 + inWeight: 0.33333334 + outWeight: 0.33333334 + m_PreInfinity: 2 + m_PostInfinity: 2 + m_RotationOrder: 4 + spreadCustomCurve: + serializedVersion: 2 + m_Curve: + - serializedVersion: 3 + time: 0 + value: 0 + inSlope: 0 + outSlope: 0 + tangentMode: 0 + weightedMode: 0 + inWeight: 0.33333334 + outWeight: 0.33333334 + m_PreInfinity: 2 + m_PostInfinity: 2 + m_RotationOrder: 4 + reverbZoneMixCustomCurve: + serializedVersion: 2 + m_Curve: + - serializedVersion: 3 + time: 0 + value: 1 + inSlope: 0 + outSlope: 0 + tangentMode: 0 + weightedMode: 0 + inWeight: 0.33333334 + outWeight: 0.33333334 + m_PreInfinity: 2 + m_PostInfinity: 2 + m_RotationOrder: 4 diff --git a/Assets/Resources/Prefabs/Sound/Audio Source.prefab.meta b/Assets/Resources/Prefabs/Sound/Audio Source.prefab.meta new file mode 100644 index 0000000..8ba84c9 --- /dev/null +++ b/Assets/Resources/Prefabs/Sound/Audio Source.prefab.meta @@ -0,0 +1,7 @@ +fileFormatVersion: 2 +guid: 3758df6f658743a458b1e3406342e7de +PrefabImporter: + externalObjects: {} + userData: + assetBundleName: + assetBundleVariant: diff --git a/Assets/Resources/Sounds/Test-bruit-court.wav b/Assets/Resources/Sounds/Test-bruit-court.wav new file mode 100644 index 0000000..6b29e0e Binary files /dev/null and b/Assets/Resources/Sounds/Test-bruit-court.wav differ diff --git a/Assets/Resources/Sounds/Test-bruit-court.wav.meta b/Assets/Resources/Sounds/Test-bruit-court.wav.meta new file mode 100644 index 0000000..10e2351 --- /dev/null +++ b/Assets/Resources/Sounds/Test-bruit-court.wav.meta @@ -0,0 +1,23 @@ +fileFormatVersion: 2 +guid: 2e41a111eebf71843a1cbf93be5a9e1c +AudioImporter: + externalObjects: {} + serializedVersion: 8 + defaultSettings: + serializedVersion: 2 + loadType: 0 + sampleRateSetting: 0 + sampleRateOverride: 44100 + compressionFormat: 1 + quality: 1 + conversionMode: 0 + preloadAudioData: 0 + platformSettingOverrides: {} + forceToMono: 0 + normalize: 1 + loadInBackground: 0 + ambisonic: 0 + 3D: 1 + userData: + assetBundleName: + assetBundleVariant: diff --git a/Assets/Scenes/SampleScene.unity b/Assets/Scenes/SampleScene.unity index 0cb206a..90d452f 100644 --- a/Assets/Scenes/SampleScene.unity +++ b/Assets/Scenes/SampleScene.unity @@ -311,6 +311,52 @@ Rigidbody: m_Interpolate: 0 m_Constraints: 112 m_CollisionDetection: 0 +--- !u!1 &266152666 +GameObject: + m_ObjectHideFlags: 0 + m_CorrespondingSourceObject: {fileID: 0} + m_PrefabInstance: {fileID: 0} + m_PrefabAsset: {fileID: 0} + serializedVersion: 6 + m_Component: + - component: {fileID: 266152667} + - component: {fileID: 266152668} + m_Layer: 0 + m_Name: SoundManager + m_TagString: Untagged + m_Icon: {fileID: 0} + m_NavMeshLayer: 0 + m_StaticEditorFlags: 0 + m_IsActive: 1 +--- !u!4 &266152667 +Transform: + m_ObjectHideFlags: 0 + m_CorrespondingSourceObject: {fileID: 0} + m_PrefabInstance: {fileID: 0} + m_PrefabAsset: {fileID: 0} + m_GameObject: {fileID: 266152666} + serializedVersion: 2 + 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: 0} + m_LocalEulerAnglesHint: {x: 0, y: 0, z: 0} +--- !u!114 &266152668 +MonoBehaviour: + m_ObjectHideFlags: 0 + m_CorrespondingSourceObject: {fileID: 0} + m_PrefabInstance: {fileID: 0} + m_PrefabAsset: {fileID: 0} + m_GameObject: {fileID: 266152666} + m_Enabled: 1 + m_EditorHideFlags: 0 + m_Script: {fileID: 11500000, guid: cc230848cf9fffe40aed130aeee564f8, type: 3} + m_Name: + m_EditorClassIdentifier: + initialPoolSize: 10 + audioSourcePrefab: {fileID: 7070934177775682985, guid: 3758df6f658743a458b1e3406342e7de, type: 3} --- !u!1 &272243392 GameObject: m_ObjectHideFlags: 0 @@ -1015,7 +1061,7 @@ AudioSource: m_PrefabInstance: {fileID: 0} m_PrefabAsset: {fileID: 0} m_GameObject: {fileID: 1883178833} - m_Enabled: 1 + m_Enabled: 0 serializedVersion: 4 OutputAudioMixerGroup: {fileID: 0} m_audioClip: {fileID: 0} @@ -1187,3 +1233,4 @@ SceneRoots: - {fileID: 99782795} - {fileID: 1697224671} - {fileID: 90518572} + - {fileID: 266152667} diff --git a/Assets/Scripts/Sound.meta b/Assets/Scripts/Sound.meta new file mode 100644 index 0000000..6dafa36 --- /dev/null +++ b/Assets/Scripts/Sound.meta @@ -0,0 +1,8 @@ +fileFormatVersion: 2 +guid: 7f85cfcdf276f164e886755fa9ee019e +folderAsset: yes +DefaultImporter: + externalObjects: {} + userData: + assetBundleName: + assetBundleVariant: diff --git a/Assets/Scripts/Sound/SoundManager.cs b/Assets/Scripts/Sound/SoundManager.cs new file mode 100644 index 0000000..7271bf2 --- /dev/null +++ b/Assets/Scripts/Sound/SoundManager.cs @@ -0,0 +1,83 @@ +using System.Collections; +using System.Collections.Generic; +using UnityEngine; + +/// +/// The sound manager is responsible for handling short lived sounds. +/// Those are sounds that should not loop. +/// If you want to manage a looped sound, use the MusicManager or place +/// it in the scene "as-is" instead. +/// +public class SoundManager : MonoBehaviour +{ + [SerializeField] private int initialPoolSize = 10; + [SerializeField] private GameObject audioSourcePrefab; + + private List audioPool; + private Queue availableAudios; + + // Singleton pattern + [System.NonSerialized] // Do not show it in editor and do not save its value + public static SoundManager Instance = null; + + private void Awake() + { + // Singleton pattern + if (Instance != null && Instance != this) + { + Destroy(gameObject); + return; + } + + Instance = this; + DontDestroyOnLoad(gameObject); + } + + private void Start() + { + if (audioSourcePrefab == null) + { + Debug.LogWarning("Cannot create a pool of audio sources: audio source prefab not set"); + } + + audioPool = new List(initialPoolSize); + availableAudios = new Queue(initialPoolSize); + + // For each initially pooled audio + for (int i=0; i(); + audioPool.Add(source); + return source; + } + + private IEnumerator DelayedBackToPoolCoroutine(AudioSource source) + { + yield return new WaitForSeconds(source.clip.length); + availableAudios.Enqueue(source); + } + + public void PlaySoundAt(Vector3 position, AudioClip sound) + { + // Take audio from pool if possible, extend pool otherwise + AudioSource source = Instance.availableAudios.Count == 0 ? + Instance.ExtendPool() : + Instance.availableAudios.Dequeue(); + + source.transform.position = position; + source.clip = sound; + source.loop = false; + source.Play(); + StartCoroutine(Instance.DelayedBackToPoolCoroutine(source)); + } +} diff --git a/Assets/Scripts/Sound/SoundManager.cs.meta b/Assets/Scripts/Sound/SoundManager.cs.meta new file mode 100644 index 0000000..b92b5e4 --- /dev/null +++ b/Assets/Scripts/Sound/SoundManager.cs.meta @@ -0,0 +1,2 @@ +fileFormatVersion: 2 +guid: cc230848cf9fffe40aed130aeee564f8 \ No newline at end of file diff --git a/ProjectSettings/TimelineSettings.asset b/ProjectSettings/TimelineSettings.asset new file mode 100644 index 0000000..b21943a --- /dev/null +++ b/ProjectSettings/TimelineSettings.asset @@ -0,0 +1,16 @@ +%YAML 1.1 +%TAG !u! tag:unity3d.com,2011: +--- !u!114 &1 +MonoBehaviour: + m_ObjectHideFlags: 53 + m_CorrespondingSourceObject: {fileID: 0} + m_PrefabInstance: {fileID: 0} + m_PrefabAsset: {fileID: 0} + m_GameObject: {fileID: 0} + m_Enabled: 1 + m_EditorHideFlags: 0 + m_Script: {fileID: 11500000, guid: a287be6c49135cd4f9b2b8666c39d999, type: 3} + m_Name: + m_EditorClassIdentifier: + assetDefaultFramerate: 60 + m_DefaultFrameRate: 60