init
This commit is contained in:
commit
78b901484a
323 changed files with 109774 additions and 0 deletions
61
Assets/Scripts/AudioManager.cs
Normal file
61
Assets/Scripts/AudioManager.cs
Normal file
|
@ -0,0 +1,61 @@
|
|||
using System;
|
||||
using UnityEngine;
|
||||
using UnityEngine.Audio;
|
||||
|
||||
public class AudioManager : MonoBehaviour{
|
||||
|
||||
[SerializeField] Sound[] sounds;
|
||||
|
||||
public static AudioManager instance;
|
||||
|
||||
void Awake(){
|
||||
if(instance == null){
|
||||
instance = this;
|
||||
}else{
|
||||
Destroy(gameObject);
|
||||
return;
|
||||
}
|
||||
DontDestroyOnLoad(gameObject);
|
||||
|
||||
foreach (Sound s in sounds){
|
||||
s.source = gameObject.AddComponent<AudioSource>();
|
||||
s.source.clip = s.clip;
|
||||
|
||||
s.source.volume = s.volume;
|
||||
s.source.pitch = s.pitch;
|
||||
s.source.loop = s.loop;
|
||||
}
|
||||
}
|
||||
|
||||
public void Play(string name, bool randomPitch){
|
||||
Sound s = Array.Find(sounds, sound => sound.name == name);
|
||||
|
||||
if (s == null){
|
||||
Debug.LogWarning("Sound: " + name + " not found!");
|
||||
return;
|
||||
}
|
||||
|
||||
if (randomPitch){
|
||||
s.source.volume = s.volume * (1f + UnityEngine.Random.Range(-s.volumeVariance / 2f, s.volumeVariance / 2f));
|
||||
s.source.pitch = s.pitch * (1f + UnityEngine.Random.Range(-s.pitchVariance / 2f, s.pitchVariance / 2f));
|
||||
}
|
||||
|
||||
s.source.Play();
|
||||
}
|
||||
|
||||
public void PlayOneShot(string name, bool randomPitch){
|
||||
Sound s = Array.Find(sounds, sound => sound.name == name);
|
||||
|
||||
if (s == null){
|
||||
Debug.LogWarning("Sound: " + name + " not found!");
|
||||
return;
|
||||
}
|
||||
|
||||
if (randomPitch){
|
||||
s.source.volume = s.volume * (1f + UnityEngine.Random.Range(-s.volumeVariance / 2f, s.volumeVariance / 2f));
|
||||
s.source.pitch = s.pitch * (1f + UnityEngine.Random.Range(-s.pitchVariance / 2f, s.pitchVariance / 2f));
|
||||
}
|
||||
|
||||
s.source.PlayOneShot(s.clip);
|
||||
}
|
||||
}
|
Loading…
Add table
Add a link
Reference in a new issue