Created
August 18, 2013 17:42
-
-
Save SteveSwink/6262920 to your computer and use it in GitHub Desktop.
Mike Stevenson's modified MusicManager from Gravity Ghost
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
using UnityEngine; | |
using System.Collections; | |
[System.Serializable] | |
public class MusicTrack | |
{ | |
public string name = ""; | |
public AudioClip clip; | |
public AudioClip clipSecondary; | |
} | |
public class MusicManager : MonoBehaviour | |
{ | |
public MusicTrack[] tracks; | |
public float pauseBetweenTracks; | |
ShuffleBag<MusicTrack> randomTracks; | |
private float _crossFade; | |
public float CrossFade { | |
get { | |
return _crossFade; | |
} | |
set { | |
source.volume = Mathf.Clamp01 (1 - value); | |
source2.volume = Mathf.Clamp01 (value); | |
_crossFade = value; | |
} | |
} | |
AudioSource source; | |
AudioSource source2; | |
bool MutedInEditor { | |
get { | |
return PlayerPrefs.GetInt ("MuteMusic", 0) == 1; | |
} | |
} | |
void Start () | |
{ | |
if (Application.isEditor && MutedInEditor) { | |
return; | |
} | |
GameObject music = new GameObject ("_Music"); | |
music.transform.parent = transform; | |
DontDestroyOnLoad (music); | |
source = music.AddComponent<AudioSource> (); | |
source2 = music.AddComponent<AudioSource> (); | |
CrossFade = 0; | |
randomTracks = new ShuffleBag<MusicTrack> (tracks); | |
randomTracks.Next (); | |
StartCoroutine ("PlayTracks"); | |
} | |
public void Play (MusicTrack track) | |
{ | |
if (Application.isEditor && MutedInEditor) | |
return; | |
source.Stop (); | |
source2.Stop (); | |
source.clip = track.clip; | |
source2.clip = track.clipSecondary; | |
source.Play (); | |
source2.Play (); | |
} | |
IEnumerator PlayTracks () | |
{ | |
while (true) { | |
while (source.isPlaying) { | |
yield return null; | |
} | |
yield return new WaitForSeconds (pauseBetweenTracks); | |
Play (randomTracks.Next ()); | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment