Created
May 23, 2016 21:45
-
-
Save grgpl3/d5787e709ca6d61bccf526c445c4134c to your computer and use it in GitHub Desktop.
Scripts to control onset of sound and crossfading between music
This file contains 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
#pragma strict | |
public var MusicClips : AudioClip[]; | |
public var MusicOnAwake : int = -1; | |
public var MusicOnAwakeLoop : boolean = true; | |
public var MusicOnAwakeVolume : float = 1.0; | |
public var ChannelCount : int = 2; | |
public var CrossFadeSeconds : float = 3.0; | |
private var channels : GameObject[]; | |
private var channelIndex : int = 0; | |
private var channelIndexRev : boolean = false; | |
private var musicClipLoop : boolean; | |
private var musicClipVolume : float; | |
private var crossFading : boolean = false; | |
private var caIn : AudioSource; | |
private var caOut : AudioSource; | |
private var fadeInMaxVolume : float; | |
private var fadeOutMaxVolume : float; | |
function Start () { | |
channels = new GameObject[ChannelCount]; | |
for (var i : int = 0; i < ChannelCount; i++) { | |
channels[i] = GameObject.Find(name + "/Channel" + (i + 1).ToString()); | |
} | |
SetMusicClipLoop(MusicOnAwakeLoop); | |
SetMusicClipVolume(MusicOnAwakeVolume); | |
PlayMusicClip(MusicOnAwake); | |
} | |
function SetMusicClipLoop (loop : boolean) { | |
musicClipLoop = loop; | |
} | |
function SetMusicClipVolume (volume : float) { | |
musicClipVolume = volume; | |
} | |
function PlayMusicClip (clipIndex : int) { | |
if (clipIndex < 0) | |
return false; | |
var caCurrent : AudioSource = channels[channelIndex].GetComponent.<AudioSource>(); | |
if (caCurrent.clip != null && caCurrent.isPlaying) { | |
if (caCurrent.clip == MusicClips[clipIndex]) { | |
caCurrent.loop = musicClipLoop; | |
return true; | |
} | |
if (crossFading) { | |
if (caOut.clip != MusicClips[clipIndex]) { | |
caOut.volume = 0; | |
caOut.Stop(); | |
} | |
else { | |
caCurrent = caOut; | |
caOut = caIn; | |
caIn = caCurrent; | |
fadeOutMaxVolume = fadeInMaxVolume; | |
fadeInMaxVolume = musicClipVolume; | |
caIn.loop = musicClipLoop; | |
channelIndexRev = !channelIndexRev; | |
channelIndex = getNewChannelIndex(); | |
return true; | |
} | |
} | |
var caNew : AudioSource = channels[getNewChannelIndex()].GetComponent.<AudioSource>(); | |
caNew.clip = MusicClips[clipIndex]; | |
caNew.loop = musicClipLoop; | |
caNew.volume = 0; | |
caNew.Play(); | |
crossFading = true; | |
caIn = caNew; | |
caOut = caCurrent; | |
fadeInMaxVolume = musicClipVolume; | |
fadeOutMaxVolume = caCurrent.volume; | |
channelIndex = getNewChannelIndex(); | |
} | |
else { | |
caCurrent.clip = MusicClips[clipIndex]; | |
caCurrent.loop = musicClipLoop; | |
caCurrent.volume = musicClipVolume; | |
caCurrent.Play(); | |
} | |
} | |
function Update () { | |
if (crossFading) { | |
caIn.volume = fadeInMaxVolume * Mathf.Clamp01(caIn.volume / fadeInMaxVolume + ((CrossFadeSeconds / 10) * Time.deltaTime)); | |
caOut.volume = fadeOutMaxVolume * Mathf.Clamp01(caOut.volume / fadeOutMaxVolume - ((CrossFadeSeconds / 10) * Time.deltaTime)); | |
if (caIn.volume == fadeInMaxVolume && caOut.volume == 0) { | |
caOut.Stop(); | |
crossFading = false; | |
} | |
} | |
} | |
function getNewChannelIndex () { | |
return (channelIndex + (channelIndexRev ? -1 : 1) + ChannelCount) % ChannelCount; | |
} |
This file contains 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; | |
public class SoundTriggerEnter : MonoBehaviour | |
{ | |
public GameObject AudioMusicManager; | |
public int MusicOnTriggerEnter = -1; | |
public bool MusicClipLoop = true; | |
public float MusicClipVolume = 1.0f; | |
void OnTriggerEnter2D(Collider2D col) | |
{ | |
if (col.gameObject.tag == "wall") | |
{ | |
AudioMusicManager.SendMessage("SetMusicClipLoop", MusicClipLoop); | |
AudioMusicManager.SendMessage("SetMusicClipVolume", MusicClipVolume); | |
AudioMusicManager.SendMessage("PlayMusicClip", MusicOnTriggerEnter); | |
} | |
} | |
} |
This file contains 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; | |
public class SoundTriggerExit : MonoBehaviour | |
{ | |
public GameObject AudioMusicManager; | |
public int MusicOnTriggerEnter = -1; | |
public bool MusicClipLoop = true; | |
public float MusicClipVolume = 1.0f; | |
void OnTriggerExit2D(Collider2D col) | |
{ | |
if (col.gameObject.tag == "wall") | |
{ | |
AudioMusicManager.SendMessage("SetMusicClipLoop", MusicClipLoop); | |
AudioMusicManager.SendMessage("SetMusicClipVolume", MusicClipVolume); | |
AudioMusicManager.SendMessage("PlayMusicClip", MusicOnTriggerEnter); | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment