80 lines
2.5 KiB
C#
80 lines
2.5 KiB
C#
using System.Collections;
|
|
using System.Collections.Generic;
|
|
using UnityEngine;
|
|
|
|
public class AudioManager : MonoBehaviour
|
|
{
|
|
public static AudioManager instance { get; private set;}
|
|
public static bool isMute {get; private set;}
|
|
void Awake(){
|
|
if(instance != null){Destroy(gameObject);return;}
|
|
instance =this;
|
|
|
|
if(PlayerPrefs.HasKey("mute")){
|
|
isMute = PlayerPrefs.GetInt("mute") == 1;
|
|
}else{
|
|
isMute=false;
|
|
}
|
|
Refresh();
|
|
}
|
|
[SerializeField]private AudioSource sfxSource;
|
|
[SerializeField]private AudioSource dropSfxSource;
|
|
[SerializeField]private AudioClip[] lowHits, midHits, hardHits;
|
|
[SerializeField]private AudioClip[] drop;
|
|
[SerializeField]private float dropMinPitch = 0.5f;
|
|
[SerializeField]private float dropMaxPitch = 1.5f;
|
|
public void PlaySFX(AudioClip clip){
|
|
sfxSource.PlayOneShot(clip);
|
|
}
|
|
|
|
public static void PlaySfx(AudioClip clip){
|
|
instance.PlaySFX(clip);
|
|
}
|
|
|
|
|
|
public static void HitSfx(float magnitude){
|
|
instance.hitSfx(magnitude);
|
|
}
|
|
public static void DropSfx(float magnitude){
|
|
instance.dropSfx(magnitude);
|
|
}
|
|
|
|
public static bool ToggleMute(){
|
|
isMute = !isMute;
|
|
Refresh();
|
|
PlayerPrefs.SetInt("mute", isMute ? 1 : 0);
|
|
PlayerPrefs.Save();
|
|
return isMute;
|
|
}
|
|
|
|
public static void Refresh(){
|
|
if(instance!=null){
|
|
instance.sfxSource.volume = isMute ? 0 : 1;
|
|
instance.dropSfxSource.volume = isMute ? 0 : 1;
|
|
}
|
|
}
|
|
|
|
void hitSfx(float magnitude){
|
|
AudioClip selectedClip;
|
|
|
|
if(magnitude < 0.5f){
|
|
selectedClip = lowHits[Random.Range(0,lowHits.Length)];
|
|
}else if(magnitude < 1){
|
|
selectedClip = midHits[Random.Range(0,midHits.Length)];
|
|
}else{
|
|
selectedClip = hardHits[Random.Range(0,hardHits.Length)];
|
|
}
|
|
Debug.Log("Playing hit sfx for " + magnitude);
|
|
sfxSource.PlayOneShot(selectedClip);
|
|
}
|
|
|
|
void dropSfx(float magnitude){
|
|
float diff = dropMaxPitch - dropMinPitch;
|
|
float mult = magnitude / 10f;
|
|
dropSfxSource.pitch = dropMinPitch + (diff * mult);
|
|
dropSfxSource.volume = mult;
|
|
Debug.Log($"Playing drop sfx for { magnitude } : {mult}, pitch: {dropSfxSource.pitch}, volume: {dropSfxSource.volume}");
|
|
dropSfxSource.Play();
|
|
}
|
|
}
|