82 lines
1.8 KiB
C#
82 lines
1.8 KiB
C#
using UnityEngine.Audio;
|
|
using System;
|
|
using UnityEngine;
|
|
|
|
public class AudioManager : MonoBehaviour
|
|
{
|
|
|
|
public static AudioManager instance;
|
|
|
|
public AudioMixerGroup mixerGroup;
|
|
|
|
public Sound[] sounds;
|
|
int hitNo = 0;
|
|
void Awake()
|
|
{
|
|
if (instance != null)
|
|
{
|
|
Destroy(gameObject);
|
|
}
|
|
else
|
|
{
|
|
instance = this;
|
|
DontDestroyOnLoad(gameObject);
|
|
}
|
|
|
|
foreach (Sound s in sounds)
|
|
{
|
|
s.source = gameObject.AddComponent<AudioSource>();
|
|
s.source.clip = s.clip;
|
|
s.source.loop = s.loop;
|
|
|
|
s.source.outputAudioMixerGroup = mixerGroup;
|
|
}
|
|
}
|
|
|
|
private void Start()
|
|
{
|
|
|
|
|
|
}
|
|
public void HitMethod()
|
|
{
|
|
Play("hit" + hitNo);
|
|
if(hitNo<=4)
|
|
{
|
|
hitNo++;
|
|
}else
|
|
{
|
|
hitNo = 0;
|
|
}
|
|
}
|
|
public void Play(string sound)
|
|
{
|
|
Sound s = Array.Find(sounds, item => item.name == sound);
|
|
if (s == null)
|
|
{
|
|
Debug.LogWarning("Sound: " + name + " not found!");
|
|
return;
|
|
}
|
|
|
|
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 Pause(string sound)
|
|
{
|
|
Sound s = Array.Find(sounds, item => item.name == sound);
|
|
if (s == null)
|
|
{
|
|
Debug.LogWarning("Sound: " + name + " not found!");
|
|
return;
|
|
}
|
|
|
|
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.Pause();
|
|
}
|
|
|
|
}
|