Skip to content

Instantly share code, notes, and snippets.

@JonathanYin
Last active July 19, 2017 17:41
Show Gist options
  • Save JonathanYin/c877f343afe8081413e930229d15e4a0 to your computer and use it in GitHub Desktop.
Save JonathanYin/c877f343afe8081413e930229d15e4a0 to your computer and use it in GitHub Desktop.
using System.Collections.Generic;
using UnityEngine;
public class Music : MonoBehaviour
{
public List<AudioSource> musicClips = new List<AudioSource>();
public GameObject music1;
public GameObject music2;
public GameObject music3;
public Texture2D icon;
void Start()
{
}
void OnGUI()
{
if (GUI.Button(new Rect(60, 75, 30, 30), icon))
{
/*for (int i = 0; i < musicClips.Count; i++)
{
if (musicClips[i].isPlaying && i != musicClips.Count - 1)
{
musicClips[i].Stop();
musicClips[i + 1].Play();
}
else if (musicClips[musicClips.Count - 1].isPlaying)
{
musicClips[musicClips.Count - 1].Stop();
musicClips[0].Play();
}
}*/
if (music1.activeSelf && music2.activeSelf == false && music3.activeSelf == false)
{
//Debug.Log("First audio track is playing");
music1.gameObject.SetActive(false);
music2.gameObject.SetActive(true);
}
else if (music1.activeSelf == false && music2.activeSelf && music3.activeSelf == false)
{
//Debug.Log("Second audio track is playing");
music2.gameObject.SetActive(false);
music3.gameObject.SetActive(true);
}
else if (music1.activeSelf == false && music2.activeSelf == false && music3.activeSelf)
{
//Debug.Log("Third audio track is playing");
music3.gameObject.SetActive(false);
music1.gameObject.SetActive(true);
}
/*if (!musicClips[0].isPlaying && !musicClips[1].isPlaying && !musicClips[2].isPlaying)
{
musicClips[0].Play();
}
if (musicClips[0].isPlaying)
{
musicClips[0].Stop();
musicClips[1].Play();
}
else if (musicClips[1].isPlaying)
{
musicClips[1].Stop();
musicClips[2].Play();
}
else if (musicClips[2].isPlaying)
{
musicClips[2].Stop();
musicClips[0].Play();
}
*/
}
}
}
@JonathanYin
Copy link
Author

JonathanYin commented Jul 17, 2017

This script is used to play music in-game, and is attached to the Main Camera. It creates a GUI button in-game, and shows a music icon on it, which the player can click. This allows them to go through all the songs I have added to my game, before looping back to the first song (much like my BackgroundChanger script).

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment