1
0
Fork 0
Bildschirmflausch-LD41/Assets/Scripts/AudioControl.cs

103 lines
2.3 KiB
C#
Raw Permalink Normal View History

2018-04-23 00:29:44 +02:00

using UnityEngine;
using System.Collections;
using UnityEngine.Audio;
public class AudioControl : MonoBehaviour
{
public AudioMixer mixer;
2018-04-23 02:37:57 +02:00
public AudioSource maintheme;
2018-04-23 14:23:48 +02:00
public AudioSource menutheme;
2018-04-23 00:29:44 +02:00
public AudioSource gameovers;
public AudioSource[] soundeffects;
2018-04-23 21:46:01 +02:00
public enum Sfx { shoot, explosion, mobattack, door, faster, slower, driving, slowdriving, hitmob, hitplayer };
2018-04-23 00:29:44 +02:00
2018-04-23 19:47:00 +02:00
private const float lvlbgm = 5.725f;
2018-04-23 23:55:08 +02:00
private const float gobgm = 14.157f;
2018-04-23 19:47:00 +02:00
private const float loopdelay = 0.025f;
2018-04-24 19:55:35 +02:00
private float prevVolume = -80f;
2018-04-23 00:29:44 +02:00
// Use this for initialization
void Start()
{
2018-04-24 03:15:58 +02:00
Debug.Log(mixer.name);
2018-04-23 02:37:57 +02:00
maintheme.loop = true;
2018-04-23 14:23:48 +02:00
menutheme.loop = true;
2018-04-23 02:37:57 +02:00
gameovers.loop = true;
}
private void Update()
{
2018-04-23 03:13:42 +02:00
if (maintheme.time >= maintheme.clip.length - loopdelay)
2018-04-23 02:37:57 +02:00
maintheme.time = lvlbgm;
2018-04-23 03:13:42 +02:00
if (gameovers.time >= gameovers.clip.length - loopdelay)
gameovers.time = gobgm;
2018-04-23 02:37:57 +02:00
}
public void GameOverBgm()
{
maintheme.Stop();
gameovers.Play();
}
public void MenuBgm()
{
if (gameovers.isPlaying)
gameovers.Stop();
2018-04-23 14:23:48 +02:00
menutheme.Play();
2018-04-23 02:37:57 +02:00
}
public void LevelBgm()
{
2018-04-23 15:11:54 +02:00
maintheme.Play();
2018-04-23 00:29:44 +02:00
}
public void SfxPlay(int sound)
{
soundeffects[sound].Play();
}
2018-04-23 18:52:21 +02:00
public void SfxPlay(Sfx sound) {
Debug.Log("Playing " + sound + " " + (int)sound);
SfxPlay((int)sound);
}
public void SfxStop(int sound)
2018-04-23 00:29:44 +02:00
{
soundeffects[sound].Stop();
}
2018-04-23 18:52:21 +02:00
2018-04-23 23:14:41 +02:00
public void SfxStop(Sfx sound) {
SfxStop((int)sound);
}
2018-04-23 18:52:21 +02:00
2018-04-23 00:29:44 +02:00
public bool SfxPlaying(int sound)
{
return soundeffects[sound].isPlaying;
}
2018-04-23 23:14:41 +02:00
2018-04-23 00:29:44 +02:00
public void SetMasterVolume(float nvol)
{
mixer.SetFloat("masterVolume", Mathf.Clamp(nvol, -80f, 20f));
}
2018-04-23 02:37:57 +02:00
public void SetBgmVolume(float nvol)
{
mixer.SetFloat("bgmVolume", Mathf.Clamp(nvol, -80f, 20f));
}
public void SetSfxVolume(float nvol)
{
mixer.SetFloat("sfxVolume", Mathf.Clamp(nvol, -80f, 20f));
}
2018-04-24 19:55:35 +02:00
public void ToggleMuteMaster() {
float t = 0f;
mixer.GetFloat("masterVolume", out t);
SetMasterVolume(prevVolume);
prevVolume = t;
}
2018-04-23 00:29:44 +02:00
}
2018-04-23 02:37:57 +02:00