-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathMusicPlayer.cs
46 lines (40 loc) · 1.06 KB
/
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
using UnityEngine;
using System.Collections;
public class MusicPlayer : MonoBehaviour {
static MusicPlayer instance = null;
public AudioClip startClip;
public AudioClip gameClip;
public AudioClip endClip;
private AudioSource music;
void Start () {
if (instance != null && instance != this) {
Destroy (gameObject);
print ("Duplicate music player self-destructing!");
} else {
instance = this;
GameObject.DontDestroyOnLoad(gameObject);
music = GetComponent<AudioSource>();
music.clip = startClip;
music.loop = true;
music.Play();
}
}
private void OnLevelWasLoaded(int level)
{
Debug.Log("Musicplayer loaded level: " + level);
music.Stop();
if (level == 0) {
music.clip = startClip;
}
if (level == 1)
{
music.clip = gameClip;
}
if (level == 2 )
{
music.clip = endClip;
}
music.loop = true;
music.Play();
}
}