-
Notifications
You must be signed in to change notification settings - Fork 2
/
MusicPlayer.cs
47 lines (38 loc) · 837 Bytes
/
MusicPlayer.cs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class MusicPlayer : MonoBehaviour {
public AudioClip[] songs;
private AudioSource audioSource;
private int index;
private static MusicPlayer musicPlayer;
// Use this for initialization
void Awake () {
if(musicPlayer == null)
{
musicPlayer = this;
}
else if(musicPlayer != this)
{
Destroy(gameObject);
}
DontDestroyOnLoad(gameObject);
audioSource = GetComponent<AudioSource>();
}
private void Start()
{
index = Random.Range(0, songs.Length);
StartMusicPlayer();
}
void StartMusicPlayer()
{
audioSource.clip = songs[index];
index++;
if(index >= songs.Length)
{
index = 0;
}
audioSource.Play();
Invoke("StartMusicPlayer", audioSource.clip.length + 0.5f);
}
}