mirror of
https://github.com/appen-isen/jeu-sans-image.git
synced 2026-03-18 21:50:42 +01:00
Add: SoundManager
The sound manager is responsible for playing short-lived sounds on the scene.
This commit is contained in:
8
Assets/Resources/Prefabs/Sound.meta
Normal file
8
Assets/Resources/Prefabs/Sound.meta
Normal file
@@ -0,0 +1,8 @@
|
|||||||
|
fileFormatVersion: 2
|
||||||
|
guid: 48dcd50c6eacab640bcf38649d7f3a7d
|
||||||
|
folderAsset: yes
|
||||||
|
DefaultImporter:
|
||||||
|
externalObjects: {}
|
||||||
|
userData:
|
||||||
|
assetBundleName:
|
||||||
|
assetBundleVariant:
|
||||||
131
Assets/Resources/Prefabs/Sound/Audio Source.prefab
Normal file
131
Assets/Resources/Prefabs/Sound/Audio Source.prefab
Normal file
@@ -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
|
||||||
7
Assets/Resources/Prefabs/Sound/Audio Source.prefab.meta
Normal file
7
Assets/Resources/Prefabs/Sound/Audio Source.prefab.meta
Normal file
@@ -0,0 +1,7 @@
|
|||||||
|
fileFormatVersion: 2
|
||||||
|
guid: 3758df6f658743a458b1e3406342e7de
|
||||||
|
PrefabImporter:
|
||||||
|
externalObjects: {}
|
||||||
|
userData:
|
||||||
|
assetBundleName:
|
||||||
|
assetBundleVariant:
|
||||||
BIN
Assets/Resources/Sounds/Test-bruit-court.wav
Normal file
BIN
Assets/Resources/Sounds/Test-bruit-court.wav
Normal file
Binary file not shown.
23
Assets/Resources/Sounds/Test-bruit-court.wav.meta
Normal file
23
Assets/Resources/Sounds/Test-bruit-court.wav.meta
Normal file
@@ -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:
|
||||||
@@ -311,6 +311,52 @@ Rigidbody:
|
|||||||
m_Interpolate: 0
|
m_Interpolate: 0
|
||||||
m_Constraints: 112
|
m_Constraints: 112
|
||||||
m_CollisionDetection: 0
|
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
|
--- !u!1 &272243392
|
||||||
GameObject:
|
GameObject:
|
||||||
m_ObjectHideFlags: 0
|
m_ObjectHideFlags: 0
|
||||||
@@ -1015,7 +1061,7 @@ AudioSource:
|
|||||||
m_PrefabInstance: {fileID: 0}
|
m_PrefabInstance: {fileID: 0}
|
||||||
m_PrefabAsset: {fileID: 0}
|
m_PrefabAsset: {fileID: 0}
|
||||||
m_GameObject: {fileID: 1883178833}
|
m_GameObject: {fileID: 1883178833}
|
||||||
m_Enabled: 1
|
m_Enabled: 0
|
||||||
serializedVersion: 4
|
serializedVersion: 4
|
||||||
OutputAudioMixerGroup: {fileID: 0}
|
OutputAudioMixerGroup: {fileID: 0}
|
||||||
m_audioClip: {fileID: 0}
|
m_audioClip: {fileID: 0}
|
||||||
@@ -1187,3 +1233,4 @@ SceneRoots:
|
|||||||
- {fileID: 99782795}
|
- {fileID: 99782795}
|
||||||
- {fileID: 1697224671}
|
- {fileID: 1697224671}
|
||||||
- {fileID: 90518572}
|
- {fileID: 90518572}
|
||||||
|
- {fileID: 266152667}
|
||||||
|
|||||||
8
Assets/Scripts/Sound.meta
Normal file
8
Assets/Scripts/Sound.meta
Normal file
@@ -0,0 +1,8 @@
|
|||||||
|
fileFormatVersion: 2
|
||||||
|
guid: 7f85cfcdf276f164e886755fa9ee019e
|
||||||
|
folderAsset: yes
|
||||||
|
DefaultImporter:
|
||||||
|
externalObjects: {}
|
||||||
|
userData:
|
||||||
|
assetBundleName:
|
||||||
|
assetBundleVariant:
|
||||||
83
Assets/Scripts/Sound/SoundManager.cs
Normal file
83
Assets/Scripts/Sound/SoundManager.cs
Normal file
@@ -0,0 +1,83 @@
|
|||||||
|
using System.Collections;
|
||||||
|
using System.Collections.Generic;
|
||||||
|
using UnityEngine;
|
||||||
|
|
||||||
|
/// <summary>
|
||||||
|
/// 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.
|
||||||
|
/// </summary>
|
||||||
|
public class SoundManager : MonoBehaviour
|
||||||
|
{
|
||||||
|
[SerializeField] private int initialPoolSize = 10;
|
||||||
|
[SerializeField] private GameObject audioSourcePrefab;
|
||||||
|
|
||||||
|
private List<AudioSource> audioPool;
|
||||||
|
private Queue<AudioSource> 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<AudioSource>(initialPoolSize);
|
||||||
|
availableAudios = new Queue<AudioSource>(initialPoolSize);
|
||||||
|
|
||||||
|
// For each initially pooled audio
|
||||||
|
for (int i=0; i<initialPoolSize; i++)
|
||||||
|
{
|
||||||
|
// Create a new pooled audio and mark it as available
|
||||||
|
availableAudios.Enqueue(ExtendPool());
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
private AudioSource ExtendPool()
|
||||||
|
{
|
||||||
|
AudioSource source = Instantiate(
|
||||||
|
audioSourcePrefab,
|
||||||
|
transform
|
||||||
|
).GetComponent<AudioSource>();
|
||||||
|
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));
|
||||||
|
}
|
||||||
|
}
|
||||||
2
Assets/Scripts/Sound/SoundManager.cs.meta
Normal file
2
Assets/Scripts/Sound/SoundManager.cs.meta
Normal file
@@ -0,0 +1,2 @@
|
|||||||
|
fileFormatVersion: 2
|
||||||
|
guid: cc230848cf9fffe40aed130aeee564f8
|
||||||
16
ProjectSettings/TimelineSettings.asset
Normal file
16
ProjectSettings/TimelineSettings.asset
Normal file
@@ -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
|
||||||
Reference in New Issue
Block a user