Created
July 25, 2015 17:01
-
-
Save yasuakiohama/1fc2b41e436ec35af868 to your computer and use it in GitHub Desktop.
BGMAudioManager.cs
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 BGMAudioManager : MonoBehaviour | |
{ | |
private static BGMAudioManager instance; | |
public static BGMAudioManager Instance { | |
get { | |
if (instance == null) { | |
instance = (BGMAudioManager)FindObjectOfType(typeof(BGMAudioManager)); | |
if (instance == null) { | |
Debug.LogError (typeof(BGMAudioManager) + "is nothing"); | |
} | |
} | |
return instance; | |
} | |
} | |
private CustomAudioSource[] customAudioSource; | |
private bool isClossFade; | |
private int main = 0; | |
private int sub = 1; | |
void Awake() | |
{ | |
if(this != Instance) | |
{ | |
Destroy(this); | |
return; | |
} | |
DontDestroyOnLoad(this.gameObject); | |
this.customAudioSource = transform.GetComponentsInChildren<CustomAudioSource> (); | |
} | |
/// <summary> | |
/// Load the specified clip, volume, loop, fadeSpeed and fadeOutThreshold. | |
/// </summary> | |
/// <param name="clip">Clip.</param> | |
/// <param name="volume">Volume.</param> | |
/// <param name="loop">If set to <c>true</c> loop.</param> | |
/// <param name="fadeSpeed">Fade speed.</param> | |
/// <param name="fadeOutThreshold">Fade out threshold.</param> | |
public void Load( | |
AudioClip clip, | |
float volume = 1.0f, | |
bool loop = true, | |
float fadeSpeed = 1f, | |
float fadeOutThreshold = 0.1f, | |
float clossFadeSpeed = 1f, | |
float clossFadeOutThreshold = 0.1f, | |
Transform target = null) | |
{ | |
if (target != null) { | |
this.transform.position = target.position; | |
} | |
this.customAudioSource [main].Load (clip, volume, loop, fadeSpeed, fadeOutThreshold); | |
this.customAudioSource [sub].Load (clip, volume, loop, clossFadeSpeed, clossFadeOutThreshold); | |
} | |
/// <summary> | |
/// Continues fading in the current audio clip. | |
/// </summary> | |
public void Play(CustomAudioSource.ExecuteType executeType = CustomAudioSource.ExecuteType.Nomal) | |
{ | |
if (isClossFade) { | |
ClossFade (); | |
return; | |
} | |
this.customAudioSource [main].Play (executeType); | |
} | |
/// <summary> | |
/// Stop playing the current audio clip immediately. | |
/// </summary> | |
public void Pause() | |
{ | |
this.customAudioSource [main].Pause (); | |
this.customAudioSource [sub].Pause (); | |
} | |
/// <summary> | |
/// Stop playing the current audio clip immediately. | |
/// </summary> | |
public void Stop(CustomAudioSource.ExecuteType executeType = CustomAudioSource.ExecuteType.Nomal) | |
{ | |
isClossFade = false; | |
this.customAudioSource [main].Stop (executeType); | |
this.customAudioSource [sub].Stop (executeType); | |
} | |
/// <summary> | |
/// Closses the fade. | |
/// </summary> | |
public void ClossFade() | |
{ | |
//sub:Nextに音が入っている | |
//main:音出てる | |
//の両方に音が入っていない場合処理しないようにする...? | |
isClossFade = true; | |
this.customAudioSource [main].Stop (CustomAudioSource.ExecuteType.Fade); | |
this.customAudioSource [sub].Play (CustomAudioSource.ExecuteType.Fade); | |
} | |
/// <summary> | |
/// Lowlight this instance. | |
/// </summary> | |
public void LowlightIn(float lowlightThreshold = 0.2f, float lowlightFadeSpeed = 1.0f) | |
{ | |
this.customAudioSource [main].LowlightIn (lowlightThreshold); | |
this.customAudioSource [sub].LowlightIn (lowlightThreshold); | |
} | |
/// <summary> | |
/// Lowlight this instance. | |
/// </summary> | |
public void LowlightOut() | |
{ | |
this.customAudioSource [main].LowlightOut (); | |
this.customAudioSource [sub].LowlightOut (); | |
} | |
public void EndFadingOut () | |
{ | |
if (!isClossFade) { | |
return; | |
} | |
isClossFade = false; | |
if (main == 1) { | |
main = 0; | |
sub = 1; | |
} else { | |
main = 1; | |
sub = 0; | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment