Sound manager
This commit is contained in:
parent
17c95b8f01
commit
2657ca3d20
142
Assets/AudioMixer.mixer
Normal file
142
Assets/AudioMixer.mixer
Normal file
@ -0,0 +1,142 @@
|
||||
%YAML 1.1
|
||||
%TAG !u! tag:unity3d.com,2011:
|
||||
--- !u!243 &-6704577497106887138
|
||||
AudioMixerGroupController:
|
||||
m_ObjectHideFlags: 0
|
||||
m_CorrespondingSourceObject: {fileID: 0}
|
||||
m_PrefabInstance: {fileID: 0}
|
||||
m_PrefabAsset: {fileID: 0}
|
||||
m_Name: Music
|
||||
m_AudioMixer: {fileID: 24100000}
|
||||
m_GroupID: 2b4c42f9eab9b464ca88654c4cf162a4
|
||||
m_Children: []
|
||||
m_Volume: b4c728c2be83f854eb4a09dfd2b94843
|
||||
m_Pitch: bffc91c4867f9264f95e8ba528a7d5d3
|
||||
m_Send: 00000000000000000000000000000000
|
||||
m_Effects:
|
||||
- {fileID: 6584737688281786359}
|
||||
m_UserColorIndex: 0
|
||||
m_Mute: 0
|
||||
m_Solo: 0
|
||||
m_BypassEffects: 0
|
||||
--- !u!243 &-12631357001976263
|
||||
AudioMixerGroupController:
|
||||
m_ObjectHideFlags: 0
|
||||
m_CorrespondingSourceObject: {fileID: 0}
|
||||
m_PrefabInstance: {fileID: 0}
|
||||
m_PrefabAsset: {fileID: 0}
|
||||
m_Name: SFX
|
||||
m_AudioMixer: {fileID: 24100000}
|
||||
m_GroupID: 6af7e97bf366a43419f05c642a2cc868
|
||||
m_Children: []
|
||||
m_Volume: eba37aa9cd8baf64fbdcd36b73b938aa
|
||||
m_Pitch: d74ad44e5b7391746aa82e7beab55c3e
|
||||
m_Send: 00000000000000000000000000000000
|
||||
m_Effects:
|
||||
- {fileID: 558502888161360382}
|
||||
m_UserColorIndex: 0
|
||||
m_Mute: 0
|
||||
m_Solo: 0
|
||||
m_BypassEffects: 0
|
||||
--- !u!241 &24100000
|
||||
AudioMixerController:
|
||||
m_ObjectHideFlags: 0
|
||||
m_CorrespondingSourceObject: {fileID: 0}
|
||||
m_PrefabInstance: {fileID: 0}
|
||||
m_PrefabAsset: {fileID: 0}
|
||||
m_Name: AudioMixer
|
||||
m_OutputGroup: {fileID: 0}
|
||||
m_MasterGroup: {fileID: 24300002}
|
||||
m_Snapshots:
|
||||
- {fileID: 24500006}
|
||||
m_StartSnapshot: {fileID: 24500006}
|
||||
m_SuspendThreshold: -80
|
||||
m_EnableSuspend: 1
|
||||
m_UpdateMode: 0
|
||||
m_ExposedParameters:
|
||||
- guid: b4c728c2be83f854eb4a09dfd2b94843
|
||||
name: MusicVolume
|
||||
- guid: eba37aa9cd8baf64fbdcd36b73b938aa
|
||||
name: SFXVolume
|
||||
m_AudioMixerGroupViews:
|
||||
- guids:
|
||||
- 1bcdfb6ff16a3e74aaa7e1054294d03c
|
||||
- 2b4c42f9eab9b464ca88654c4cf162a4
|
||||
- 6af7e97bf366a43419f05c642a2cc868
|
||||
name: View
|
||||
m_CurrentViewIndex: 0
|
||||
m_TargetSnapshot: {fileID: 24500006}
|
||||
--- !u!243 &24300002
|
||||
AudioMixerGroupController:
|
||||
m_ObjectHideFlags: 0
|
||||
m_CorrespondingSourceObject: {fileID: 0}
|
||||
m_PrefabInstance: {fileID: 0}
|
||||
m_PrefabAsset: {fileID: 0}
|
||||
m_Name: Master
|
||||
m_AudioMixer: {fileID: 24100000}
|
||||
m_GroupID: 1bcdfb6ff16a3e74aaa7e1054294d03c
|
||||
m_Children:
|
||||
- {fileID: -6704577497106887138}
|
||||
- {fileID: -12631357001976263}
|
||||
m_Volume: 62f505468629cc94787f4d287871bf5d
|
||||
m_Pitch: dcad77815d59c1e4fb262ef58f710a58
|
||||
m_Send: 00000000000000000000000000000000
|
||||
m_Effects:
|
||||
- {fileID: 24400004}
|
||||
m_UserColorIndex: 0
|
||||
m_Mute: 0
|
||||
m_Solo: 0
|
||||
m_BypassEffects: 0
|
||||
--- !u!244 &24400004
|
||||
AudioMixerEffectController:
|
||||
m_ObjectHideFlags: 3
|
||||
m_CorrespondingSourceObject: {fileID: 0}
|
||||
m_PrefabInstance: {fileID: 0}
|
||||
m_PrefabAsset: {fileID: 0}
|
||||
m_Name:
|
||||
m_EffectID: da8fa56c13830e14d906c29b1fbdd5d0
|
||||
m_EffectName: Attenuation
|
||||
m_MixLevel: 972340951d6bd7f479f4611da6b60851
|
||||
m_Parameters: []
|
||||
m_SendTarget: {fileID: 0}
|
||||
m_EnableWetMix: 0
|
||||
m_Bypass: 0
|
||||
--- !u!245 &24500006
|
||||
AudioMixerSnapshotController:
|
||||
m_ObjectHideFlags: 0
|
||||
m_CorrespondingSourceObject: {fileID: 0}
|
||||
m_PrefabInstance: {fileID: 0}
|
||||
m_PrefabAsset: {fileID: 0}
|
||||
m_Name: Snapshot
|
||||
m_AudioMixer: {fileID: 24100000}
|
||||
m_SnapshotID: a2cffb0e0215659469b3420cc78aec45
|
||||
m_FloatValues: {}
|
||||
m_TransitionOverrides: {}
|
||||
--- !u!244 &558502888161360382
|
||||
AudioMixerEffectController:
|
||||
m_ObjectHideFlags: 3
|
||||
m_CorrespondingSourceObject: {fileID: 0}
|
||||
m_PrefabInstance: {fileID: 0}
|
||||
m_PrefabAsset: {fileID: 0}
|
||||
m_Name:
|
||||
m_EffectID: 356e654c6643afc4790ef47b53026af3
|
||||
m_EffectName: Attenuation
|
||||
m_MixLevel: 539323a2e3d5311469c9a3e1eab0eed7
|
||||
m_Parameters: []
|
||||
m_SendTarget: {fileID: 0}
|
||||
m_EnableWetMix: 0
|
||||
m_Bypass: 0
|
||||
--- !u!244 &6584737688281786359
|
||||
AudioMixerEffectController:
|
||||
m_ObjectHideFlags: 3
|
||||
m_CorrespondingSourceObject: {fileID: 0}
|
||||
m_PrefabInstance: {fileID: 0}
|
||||
m_PrefabAsset: {fileID: 0}
|
||||
m_Name:
|
||||
m_EffectID: e8a7b368cabf5ef438a6e9ad5c19c3a3
|
||||
m_EffectName: Attenuation
|
||||
m_MixLevel: f179c2750a1e35949ae2ab23632480f1
|
||||
m_Parameters: []
|
||||
m_SendTarget: {fileID: 0}
|
||||
m_EnableWetMix: 0
|
||||
m_Bypass: 0
|
||||
8
Assets/AudioMixer.mixer.meta
Normal file
8
Assets/AudioMixer.mixer.meta
Normal file
@ -0,0 +1,8 @@
|
||||
fileFormatVersion: 2
|
||||
guid: 06ed4a4573fabf84580596dba3e0c116
|
||||
NativeFormatImporter:
|
||||
externalObjects: {}
|
||||
mainObjectFileID: 24100000
|
||||
userData:
|
||||
assetBundleName:
|
||||
assetBundleVariant:
|
||||
@ -273,6 +273,52 @@ Transform:
|
||||
m_Father: {fileID: 0}
|
||||
m_RootOrder: 1
|
||||
m_LocalEulerAnglesHint: {x: 0, y: 0, z: 0}
|
||||
--- !u!1 &463130994
|
||||
GameObject:
|
||||
m_ObjectHideFlags: 0
|
||||
m_CorrespondingSourceObject: {fileID: 0}
|
||||
m_PrefabInstance: {fileID: 0}
|
||||
m_PrefabAsset: {fileID: 0}
|
||||
serializedVersion: 6
|
||||
m_Component:
|
||||
- component: {fileID: 463130996}
|
||||
- component: {fileID: 463130995}
|
||||
m_Layer: 0
|
||||
m_Name: EnemySpawner
|
||||
m_TagString: Untagged
|
||||
m_Icon: {fileID: 0}
|
||||
m_NavMeshLayer: 0
|
||||
m_StaticEditorFlags: 0
|
||||
m_IsActive: 1
|
||||
--- !u!114 &463130995
|
||||
MonoBehaviour:
|
||||
m_ObjectHideFlags: 0
|
||||
m_CorrespondingSourceObject: {fileID: 0}
|
||||
m_PrefabInstance: {fileID: 0}
|
||||
m_PrefabAsset: {fileID: 0}
|
||||
m_GameObject: {fileID: 463130994}
|
||||
m_Enabled: 1
|
||||
m_EditorHideFlags: 0
|
||||
m_Script: {fileID: 11500000, guid: 980fcc6b06b84694080efd936a8fbcf0, type: 3}
|
||||
m_Name:
|
||||
m_EditorClassIdentifier:
|
||||
delay: 5
|
||||
enemyPrefabs: []
|
||||
spawnPoints: []
|
||||
--- !u!4 &463130996
|
||||
Transform:
|
||||
m_ObjectHideFlags: 0
|
||||
m_CorrespondingSourceObject: {fileID: 0}
|
||||
m_PrefabInstance: {fileID: 0}
|
||||
m_PrefabAsset: {fileID: 0}
|
||||
m_GameObject: {fileID: 463130994}
|
||||
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_Children: []
|
||||
m_Father: {fileID: 0}
|
||||
m_RootOrder: 3
|
||||
m_LocalEulerAnglesHint: {x: 0, y: 0, z: 0}
|
||||
--- !u!1 &1302468886
|
||||
GameObject:
|
||||
m_ObjectHideFlags: 0
|
||||
|
||||
114
Assets/Scripts/SoundManager.cs
Normal file
114
Assets/Scripts/SoundManager.cs
Normal file
@ -0,0 +1,114 @@
|
||||
using System.Collections;
|
||||
using System.Collections.Generic;
|
||||
using UnityEngine;
|
||||
using UnityEngine.Audio;
|
||||
|
||||
public class SoundManager : MonoBehaviour {
|
||||
|
||||
public AudioMixer audioMixer;
|
||||
|
||||
float musicVol;
|
||||
float SFXVol;
|
||||
|
||||
|
||||
void Awake() {
|
||||
musicVol = PlayerPrefs.GetFloat("MusicVolume", 1f);
|
||||
SFXVol = PlayerPrefs.GetFloat("SFXVolume", 1f);
|
||||
}
|
||||
|
||||
void Start() {
|
||||
SetMusicVolume(musicVol);
|
||||
SetSFXVolume(SFXVol);
|
||||
}
|
||||
|
||||
public void PlaySound(AudioSource source, bool randomPitch=false, bool createTempSourceIfBusy=true, bool createTempSource = false, float delay = 0f) {
|
||||
if (source == null || !source.gameObject.activeInHierarchy) {
|
||||
return;
|
||||
}
|
||||
|
||||
if (source.isPlaying) {
|
||||
if (createTempSourceIfBusy) {
|
||||
if (randomPitch) {
|
||||
source.pitch = Random.Range(0.9f, 1.1f);
|
||||
}
|
||||
StartCoroutine(CreateTempSource(source, delay:delay));
|
||||
}
|
||||
} else {
|
||||
if (randomPitch) {
|
||||
source.pitch = Random.Range(0.9f, 1.1f);
|
||||
}
|
||||
|
||||
if(createTempSource) {
|
||||
StartCoroutine(CreateTempSource(source, delay: delay));
|
||||
} else {
|
||||
if(delay > 0f) {
|
||||
StartCoroutine(DelayedPlaySound(source, delay));
|
||||
} else {
|
||||
source.Play();
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
public void PlaySound(AudioSource source, AudioClip[] possibleClips, bool randomPitch=false, bool createTempSourceIfBusy=true, bool createTempSource=false, float delay=0f) {
|
||||
if(source == null || ! source.gameObject.activeInHierarchy || possibleClips.Length == 0) {
|
||||
return;
|
||||
}
|
||||
source.clip = possibleClips[Random.Range(0, possibleClips.Length)];
|
||||
PlaySound(source, randomPitch, createTempSourceIfBusy, createTempSource, delay);
|
||||
}
|
||||
|
||||
IEnumerator CreateTempSource(AudioSource refSource, float delay=0f) {
|
||||
AudioSource newSource = gameObject.AddComponent<AudioSource>();
|
||||
newSource.volume = refSource.volume;
|
||||
newSource.clip = refSource.clip;
|
||||
|
||||
PlaySound(newSource, delay:delay);
|
||||
|
||||
yield return new WaitForSeconds(delay);
|
||||
|
||||
// yield return new WaitForSeconds(1f);
|
||||
// Destroy(newSource);
|
||||
|
||||
for(int i=0; i<100; ++i) {
|
||||
yield return new WaitForSeconds(1f);
|
||||
if(! newSource.isPlaying) {
|
||||
Destroy(newSource);
|
||||
break;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
IEnumerator DelayedPlaySound(AudioSource source, float delay) {
|
||||
yield return new WaitForSeconds(delay);
|
||||
|
||||
source.Play();
|
||||
}
|
||||
|
||||
|
||||
public void SetMusicVolume(float value) {
|
||||
// Slider should go from 0.0001 to 1
|
||||
|
||||
musicVol = value;
|
||||
audioMixer.SetFloat("MusicVolume", Mathf.Log10(musicVol) * 20);
|
||||
PlayerPrefs.SetFloat("MusicVolume", musicVol);
|
||||
}
|
||||
|
||||
public void SetSFXVolume(float value) {
|
||||
// Slider should go from 0.0001 to 1
|
||||
|
||||
SFXVol = value;
|
||||
audioMixer.SetFloat("SFXVolume", Mathf.Log10(SFXVol) * 20);
|
||||
PlayerPrefs.SetFloat("SFXVolume", SFXVol);
|
||||
}
|
||||
|
||||
public float GetMusicVolume() {
|
||||
return musicVol;
|
||||
}
|
||||
|
||||
public float GetSFXVolume() {
|
||||
return SFXVol;
|
||||
}
|
||||
|
||||
}
|
||||
11
Assets/Scripts/SoundManager.cs.meta
Normal file
11
Assets/Scripts/SoundManager.cs.meta
Normal file
@ -0,0 +1,11 @@
|
||||
fileFormatVersion: 2
|
||||
guid: b2771a5310833e64798c08aa5371e48b
|
||||
MonoImporter:
|
||||
externalObjects: {}
|
||||
serializedVersion: 2
|
||||
defaultReferences: []
|
||||
executionOrder: 0
|
||||
icon: {instanceID: 0}
|
||||
userData:
|
||||
assetBundleName:
|
||||
assetBundleVariant:
|
||||
Loading…
x
Reference in New Issue
Block a user