starOfWords/Assets/Scripts/3.5/MusicManager.cs

41 lines
1 KiB
C#
Raw Permalink Normal View History

using UnityEngine;
public class MusicManager : MonoBehaviour
{
[Header("━━━ MÜZIKLER (25 MP3) ━━━━━━━━━━━━━━━━━")]
public AudioClip[] songs;
[Header("━━━ AUDIO SOURCE ━━━━━━━━━━━━━━━━━━━━━━")]
public AudioSource audioSource;
private int lastIndex = -1;
// ============================================================
void Start()
{
PlayRandom();
}
void Update()
{
if (!audioSource.isPlaying)
PlayRandom();
}
// ============================================================
void PlayRandom()
{
if (songs == null || songs.Length == 0) return;
int index;
do
{
index = Random.Range(0, songs.Length);
}
while (index == lastIndex && songs.Length > 1); // aynı şarkı üst üste gelmesin
lastIndex = index;
audioSource.clip = songs[index];
audioSource.Play();
}
}