Skip to content
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
31 changes: 20 additions & 11 deletions Assets/Scripts/Game/Audio/Music.cs
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using System.Linq;

public class Music : MonoBehaviour
{
Expand Down Expand Up @@ -33,27 +33,36 @@ void Awake()

void Init()
{
playOrder = Seb.ArrayHelper.CreateIndexArray(tracks.Length);
tracks = tracks.Where(track => track != null).ToArray();
if (shuffleTracksOnStart)
{
Seb.ArrayHelper.ShuffleArray(playOrder, new System.Random());
Seb.ArrayHelper.ShuffleArray(tracks, new System.Random());
}
nextTrackIndex = 0;
if (tracks.Length == 0) {
Destroy(gameObject);
}
}

void Update()
{
if (Time.time > nextTrackStartTime)
{
if (tracks[nextTrackIndex] != null)
{
source.Stop();
source.clip = tracks[nextTrackIndex];
source.Play();
nextTrackStartTime = Time.time + source.clip.length;
nextTrackIndex = (nextTrackIndex + 1) % tracks.Length;
}
StartCoroutine(PlayNextTrack());
}
}

IEnumerator PlayNextTrack()
{
if (tracks[nextTrackIndex] != null)
{
source.Stop();
source.clip = tracks[nextTrackIndex];
source.Play();
nextTrackStartTime = Time.time + source.clip.length;
nextTrackIndex = (nextTrackIndex + 1) % tracks.Length;
}
yield return null;
}

[RuntimeInitializeOnLoadMethod(RuntimeInitializeLoadType.SubsystemRegistration)]
Expand Down