2026-07-25 20:42:09 -04:00
|
|
|
using UnityEngine;
|
|
|
|
|
using System.Collections.Generic;
|
|
|
|
|
|
2026-07-26 00:59:17 -04:00
|
|
|
// Idea from https://discussions.unity.com/t/play-audio-in-queue/239402/2
|
2026-07-25 20:42:09 -04:00
|
|
|
public class MusicManager : MonoBehaviour
|
|
|
|
|
{
|
|
|
|
|
public AudioClip intro;
|
|
|
|
|
|
|
|
|
|
public AudioClip loop;
|
|
|
|
|
|
|
|
|
|
public AudioClip outro;
|
|
|
|
|
|
2026-08-25 22:24:07 -04:00
|
|
|
public AudioClip victory;
|
|
|
|
|
|
2026-07-25 20:42:09 -04:00
|
|
|
private AudioSource audioSource;
|
|
|
|
|
|
|
|
|
|
private Queue<AudioClip> clipsQueue;
|
|
|
|
|
|
2026-07-28 00:44:11 -04:00
|
|
|
private bool isPaused = false;
|
|
|
|
|
|
2026-08-25 22:24:07 -04:00
|
|
|
private bool needsReset = false;
|
|
|
|
|
|
2026-07-25 20:42:09 -04:00
|
|
|
// Start is called once before the first execution of Update after the MonoBehaviour is created
|
|
|
|
|
void Start()
|
|
|
|
|
{
|
|
|
|
|
audioSource = GetComponent<AudioSource>();
|
|
|
|
|
clipsQueue = new Queue<AudioClip>();
|
|
|
|
|
|
|
|
|
|
// Intro is only played once, so we enqueue it first.
|
|
|
|
|
clipsQueue.Enqueue(intro);
|
|
|
|
|
ResetQueue();
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// Update is called once per frame
|
|
|
|
|
void FixedUpdate()
|
|
|
|
|
{
|
2026-07-28 00:44:11 -04:00
|
|
|
if (audioSource.isPlaying == false && !isPaused)
|
2026-07-25 20:42:09 -04:00
|
|
|
{
|
|
|
|
|
audioSource.clip = clipsQueue.Dequeue();
|
|
|
|
|
audioSource.Play();
|
|
|
|
|
|
|
|
|
|
if(clipsQueue.Count == 0)
|
|
|
|
|
{
|
|
|
|
|
ResetQueue();
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
private void ResetQueue()
|
|
|
|
|
{
|
|
|
|
|
clipsQueue.Enqueue(loop);
|
|
|
|
|
clipsQueue.Enqueue(loop);
|
|
|
|
|
clipsQueue.Enqueue(loop);
|
|
|
|
|
clipsQueue.Enqueue(loop);
|
|
|
|
|
clipsQueue.Enqueue(outro);
|
|
|
|
|
}
|
2026-07-28 00:44:11 -04:00
|
|
|
|
2026-08-25 22:24:07 -04:00
|
|
|
public void PauseMusic()
|
2026-07-28 00:44:11 -04:00
|
|
|
{
|
|
|
|
|
audioSource.Pause();
|
|
|
|
|
isPaused = true;
|
|
|
|
|
}
|
|
|
|
|
|
2026-08-25 22:24:07 -04:00
|
|
|
public void ResumeMusic()
|
2026-07-28 00:44:11 -04:00
|
|
|
{
|
2026-08-25 22:24:07 -04:00
|
|
|
if (needsReset)
|
|
|
|
|
{
|
2026-08-27 17:16:50 -04:00
|
|
|
audioSource.Stop();
|
2026-08-27 15:34:06 -04:00
|
|
|
audioSource.clip = null;
|
2026-08-25 22:24:07 -04:00
|
|
|
clipsQueue.Clear();
|
|
|
|
|
ResetQueue();
|
|
|
|
|
needsReset = false;
|
|
|
|
|
}
|
|
|
|
|
|
2026-08-27 15:34:06 -04:00
|
|
|
if (audioSource.clip != null && !audioSource.isPlaying)
|
|
|
|
|
{
|
|
|
|
|
audioSource.UnPause();
|
|
|
|
|
}
|
|
|
|
|
|
2026-07-28 00:44:11 -04:00
|
|
|
isPaused = false;
|
|
|
|
|
}
|
2026-08-25 22:24:07 -04:00
|
|
|
|
|
|
|
|
public void PlayVictoryMusic()
|
|
|
|
|
{
|
|
|
|
|
audioSource.Stop();
|
|
|
|
|
audioSource.PlayOneShot(victory);
|
|
|
|
|
needsReset = true;
|
|
|
|
|
}
|
2026-07-25 20:42:09 -04:00
|
|
|
}
|