Created
November 2, 2017 18:58
-
-
Save tatmos/5c6ab1fd433e46cfb41f8101049e3740 to your computer and use it in GitHub Desktop.
2014年「 Rinne-」( https://www.youtube.com/watch?v=d5OGkFcZMNc&feature=youtu.be ) のSoundManager。 ADX2LEで音程やスケール、音量制御など。
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; | |
using System.Collections.Generic; | |
public class SoundManager : MonoBehaviour { | |
/// <summary> | |
/// デバッグフラグ | |
/// </summary> | |
public bool debugFlag = false; | |
public static SoundManager main = null; | |
// BGM ZukaSE Title Result Kuttuku | |
List<AudioSource> audioSourceBGMList = new List<AudioSource>(); | |
public List<AudioClip> bgmClipList = new List<AudioClip>(); | |
List<AudioSource> audioSourceZukaSeList = new List<AudioSource>(); | |
public List<AudioClip> zukaSE = new List<AudioClip>(); | |
int zukaSEIndex = 0; | |
List<AudioSource> audioSourceTitleList = new List<AudioSource>(); | |
public List<AudioClip> titleClipList = new List<AudioClip>(); | |
List<AudioSource> audioSourceResultList = new List<AudioSource>(); | |
public List<AudioClip> resultClipList = new List<AudioClip>(); | |
List<AudioSource> audioSourceKuttukuList = new List<AudioSource>(); | |
public List<AudioClip> kuttukuClipList = new List<AudioClip>(); | |
AudioSource timeUpSoundSource = null; | |
public AudioClip timeUpSoundClip; | |
PlayerController playerController = null; | |
void Awake() | |
{ | |
if(main != null) | |
{ | |
GameObject.Destroy(this.gameObject); | |
AutoPlayOnSceneLoad(); | |
} else | |
{ | |
main = this; | |
DontDestroyOnLoad(this.gameObject); | |
} | |
} | |
ParticleSystem ps = null; | |
[SerializeField] | |
private int _RenderQueue = 4000; | |
void RenderStart () | |
{ | |
renderer.material.renderQueue = _RenderQueue; | |
Transform trans = transform; | |
for (int i = 0; i < trans.childCount; i++) { | |
trans.GetChild (i).gameObject.renderer.material.renderQueue = _RenderQueue; | |
} | |
ps = this.gameObject.GetComponent<ParticleSystem>(); | |
} | |
/// <summary> | |
/// AudioSourcesの初期化 | |
/// </summary> | |
void InitSoundSources() | |
{ | |
for(int i = 0;i<4;i++){ | |
audioSourceBGMList.Add(this.gameObject.AddComponent<AudioSource>()); | |
audioSourceBGMList[i].loop = true; | |
} | |
for(int i = 0;i<4;i++){ | |
audioSourceZukaSeList.Add(this.gameObject.AddComponent<AudioSource>()); | |
audioSourceZukaSeList[i].loop = false; | |
audioSourceZukaSeList[i].volume = 0.41f; | |
} | |
for(int i = 0;i<3;i++){ | |
audioSourceTitleList.Add(this.gameObject.AddComponent<AudioSource>()); | |
audioSourceTitleList[i].loop = true; | |
audioSourceTitleList[i].clip = titleClipList[0]; | |
} | |
for(int i = 0;i<1;i++){ | |
audioSourceResultList.Add(this.gameObject.AddComponent<AudioSource>()); | |
audioSourceResultList[i].loop = true; | |
audioSourceResultList[i].clip = resultClipList[0]; | |
} | |
for(int i = 0;i<5;i++){ | |
audioSourceKuttukuList.Add(this.gameObject.AddComponent<AudioSource>()); | |
audioSourceKuttukuList[i].loop = false; | |
audioSourceKuttukuList[i].clip = kuttukuClipList[i]; | |
} | |
timeUpSoundSource = this.gameObject.AddComponent<AudioSource>(); | |
} | |
// Use this for initialization | |
void Start () { | |
RenderStart(); | |
InitSoundSources(); | |
AutoPlayOnSceneLoad(); | |
} | |
/// <summary> | |
/// シーン切り替え時に自動で再生コールする | |
/// </summary> | |
void AutoPlayOnSceneLoad() | |
{ | |
if(Application.loadedLevelName == "MainScene") | |
{ | |
SoundManager.main.PlayBGM(); | |
} | |
if(Application.loadedLevelName == "Title" || Application.loadedLevelName == "TitleLoop") | |
{ | |
SoundManager.main.PlayTitle(); | |
} | |
if(Application.loadedLevelName == "Result") | |
{ | |
SoundManager.main.PlayResult(); | |
} | |
} | |
float checkPlayerControlerTime = 0; | |
void OnLevelWasLoaded(int level) { | |
playerController = null; | |
} | |
// Update is called once per frame | |
void Update () { | |
Update_PlayerLifeSound(); | |
Update_Levels(); | |
} | |
/// <summary> | |
/// プレーヤを監視して、残りライフが少なくなったら タイムアップサウンド(ドラムロール)を少しずつ音量を上げていく | |
/// </summary> | |
void Update_PlayerLifeSound() | |
{ | |
if(playerController == null && checkPlayerControlerTime+1 < Time.timeSinceLevelLoad) | |
{ | |
checkPlayerControlerTime = Time.timeSinceLevelLoad; | |
GameObject go = GameObject.FindWithTag("Player"); | |
if(go != null){ | |
playerController = go.GetComponent<PlayerController>(); | |
} | |
} | |
if(playerController != null) | |
{ | |
if(playerController.Status.Data.Life < 10f) | |
{ | |
PlayTimeUpSound((10f - playerController.Status.Data.Life)*0.1f); | |
} else { | |
StopTimeUpSound(); | |
} | |
} else { | |
StopTimeUpSound(); | |
} | |
} | |
/// <summary> | |
/// 音量関連の動的処理 | |
/// </summary> | |
void Update_Levels() | |
{ | |
// ボリュームが小さい状態で再生開始した場合に音量を一定量まで徐々に上げる | |
if(audioSourceBGMList[0].volume < 0.21f) | |
{ | |
audioSourceBGMList[0].volume += 0.01f; | |
} | |
if(audioSourceBGMList[1].volume < 0.15f) | |
{ | |
audioSourceBGMList[1].volume += 0.001f; | |
} | |
if(audioSourceBGMList[2].volume < 0.1f) | |
{ | |
audioSourceBGMList[2].volume += 0.001f; | |
} | |
// タイトルBGMが再生中に 他のBGMが鳴っていたら徐々に音量を下げ、音量0で停止する | |
if(audioSourceTitleList[0].isPlaying) | |
{ | |
if(audioSourceBGMList[0].isPlaying){ | |
audioSourceTitleList[0].volume -= 0.01f; | |
if(audioSourceTitleList[0].volume < 0.1f) | |
{ | |
audioSourceTitleList[0].Stop(); | |
} | |
} else { | |
if(audioSourceTitleList[0].volume < 1.0f){ | |
audioSourceTitleList[0].volume += 0.1f; | |
} | |
} | |
} | |
} | |
int bgmIndex = -1; | |
/// <summary> | |
/// BGMを再生する BGMの再生コールのたびにカウントアップする内部値で音楽を徐々に切り替える | |
/// </summary> | |
public void PlayBGM() | |
{ | |
float seekPos = 0; | |
if(audioSourceTitleList[0].isPlaying) | |
{ | |
seekPos = audioSourceTitleList[0].time; | |
} | |
if(audioSourceBGMList[0].isPlaying) | |
{ | |
seekPos = audioSourceBGMList[0].time; | |
} | |
if(audioSourceResultList[0].isPlaying) | |
{ | |
seekPos = audioSourceResultList[0].time; | |
audioSourceResultList[0].Stop(); | |
} | |
// 0 | |
bgmIndex++; | |
bgmIndex = bgmIndex%(bgmClipList.Count-1); | |
audioSourceBGMList[0].clip = bgmClipList[bgmIndex]; | |
audioSourceBGMList[0].time = seekPos; | |
if(audioSourceBGMList[0].isPlaying == false) | |
{ | |
audioSourceBGMList[0].Play(); | |
audioSourceBGMList[1].volume = 0.12f; | |
}else { | |
audioSourceBGMList[0].volume = 0.1f; | |
audioSourceBGMList[1].volume = 0.1f; | |
} | |
// 1 | |
if(Random.Range(0,3) == 0){ | |
int bgmIndex2 = (bgmIndex+Random.Range(1,3))%(bgmClipList.Count); | |
audioSourceBGMList[1].clip = bgmClipList[bgmIndex2]; | |
if(audioSourceBGMList[1].isPlaying == false) | |
{ | |
audioSourceBGMList[1].volume = 0f; | |
audioSourceBGMList[1].Play(); | |
} else { | |
audioSourceBGMList[1].volume = 0.12f; | |
} | |
audioSourceBGMList[1].time = seekPos; | |
} | |
// 2 | |
if(Random.Range(0,3) == 0){ | |
int bgmIndex2 = (bgmIndex+Random.Range(1,3))%(bgmClipList.Count); | |
audioSourceBGMList[2].clip = bgmClipList[bgmIndex2]; | |
if(audioSourceBGMList[2].isPlaying == false) | |
{ | |
audioSourceBGMList[2].volume = 0f; | |
audioSourceBGMList[2].Play(); | |
}else { | |
audioSourceBGMList[1].volume = 0.12f; | |
} | |
audioSourceBGMList[2].time = seekPos; | |
} | |
if(audioSourceBGMList[0].clip == audioSourceBGMList[1].clip) | |
{ | |
audioSourceBGMList[1].Stop(); | |
} | |
if(audioSourceBGMList[0].clip == audioSourceBGMList[2].clip || | |
audioSourceBGMList[1].clip == audioSourceBGMList[2].clip) | |
{ | |
audioSourceBGMList[2].Stop(); | |
} | |
} | |
int zukaType = 0; | |
void OnGUI() | |
{ | |
if(debugFlag == false)return; | |
if(playerController != null){ | |
GUILayout.Label("Life = "+playerController.Status.Data.Life + " MaxLife = "+playerController.Status.Data.MaxLife ); | |
} | |
if(GUILayout.Button("Play Title")) | |
{ | |
this.PlayTitle(); | |
} | |
if(GUILayout.Button("Play BGM")) | |
{ | |
this.PlayBGM(); | |
} | |
if(GUILayout.Button("Play Result")) | |
{ | |
this.PlayResult(); | |
} | |
if(GUILayout.Button("Play Zuka Hana")){ this.PlayZukaSE(0);} | |
if(GUILayout.Button("Play Zuka Hosi")){ this.PlayZukaSE(1);} | |
if(GUILayout.Button("Play Zuka Sora")){ this.PlayZukaSE(2);} | |
if(GUILayout.Button("Play Zuka Tuki")){ this.PlayZukaSE(3);} | |
if(GUILayout.Button("Play Zuka Yuki")){ this.PlayZukaSE(4);} | |
if(GUILayout.Button("Play Up")){ this.PlayUp();} | |
if(GUILayout.Button("Play Down")){ this.PlayDown();} | |
if(GUILayout.Button("Play Kuttuku")){ this.PlayKuttuku();} | |
} | |
/// <summary> | |
/// エージェントが発生した時の音 | |
/// </summary> | |
/// <param name="no">エージェントのエレメントID</param> | |
public void PlayZukaSE(int no) | |
{ | |
zukaSEIndex++; | |
zukaSEIndex = zukaSEIndex%audioSourceZukaSeList.Count; | |
audioSourceZukaSeList[zukaSEIndex].clip = zukaSE[no%zukaSE.Count]; | |
audioSourceZukaSeList[zukaSEIndex].pitch = Mathf.Pow(2.0f,pitchToScale(Random.Range(0,12))/12.0f); | |
audioSourceZukaSeList[zukaSEIndex].Play(); | |
} | |
/// <summary> | |
/// BGMのキーに合わせた音の音程を返す | |
/// </summary> | |
/// <returns>The to scale.</returns> | |
/// <param name="io_note">Io_note.</param> | |
int pitchToScale(int io_note) | |
{ | |
io_note = Mathf.Abs(io_note); | |
switch(io_note) | |
{ | |
case 0:io_note = 0;break; | |
case 1:io_note = 0;break; | |
case 2:io_note = 0;break; | |
case 3:io_note = 3;break; | |
case 4:io_note = 3;break; | |
case 5:io_note = 5;break; | |
case 6:io_note = 5;break; | |
case 7:io_note = 7;break; | |
case 8:io_note = 7;break; | |
case 9:io_note = 10;break; | |
case 10:io_note = 10;break; | |
case 11:io_note = 10;break; | |
} | |
return io_note; | |
} | |
public void PlayTitle() | |
{ | |
audioSourceBGMList[0].Stop(); | |
audioSourceBGMList[1].Stop(); | |
audioSourceBGMList[2].Stop(); | |
float seekPos = 0; | |
if(audioSourceResultList[0].isPlaying){ | |
seekPos = audioSourceResultList[0].time; | |
audioSourceResultList[0].Stop(); | |
} | |
if(audioSourceTitleList[0].isPlaying == false){ | |
audioSourceTitleList[0].time = seekPos; | |
audioSourceTitleList[0].volume = 0.2f; | |
audioSourceTitleList[0].Play(); | |
} else { | |
audioSourceTitleList[0].volume = 0.2f; | |
} | |
} | |
public void PlayUp() | |
{ | |
audioSourceTitleList[1].clip = titleClipList[1]; | |
audioSourceTitleList[1].loop = false; | |
audioSourceTitleList[1].Play(); | |
} | |
float lastPlayedTime_PlayDown = 0;// 最後にスコアの入った時間 | |
int sametime_PlayDown_Count = 0;// 同時刻再生のカウント | |
/// <summary> | |
/// スコアの入る音が鳴る | |
/// </summary> | |
public void PlayDown() | |
{ | |
if(lastPlayedTime_PlayDown+1 > Time.timeSinceLevelLoad) | |
{ | |
sametime_PlayDown_Count ++; | |
Debug.Log("Same Time Score :" + sametime_PlayDown_Count.ToString()); | |
} else { | |
sametime_PlayDown_Count = 0; | |
} | |
switch(sametime_PlayDown_Count) | |
{ | |
case 0:audioSourceTitleList[2].clip = titleClipList[2];break; | |
case 1:audioSourceTitleList[2].clip = titleClipList[3];break; | |
case 2:audioSourceTitleList[2].clip = titleClipList[4];break; | |
default:audioSourceTitleList[2].clip = titleClipList[4];break; | |
} | |
audioSourceTitleList[2].loop = false; | |
audioSourceTitleList[2].Play(); | |
lastPlayedTime_PlayDown = Time.timeSinceLevelLoad; | |
} | |
public void PlayResult() | |
{ | |
audioSourceBGMList[0].Stop(); | |
audioSourceBGMList[1].Stop(); | |
audioSourceBGMList[2].Stop(); | |
audioSourceTitleList[0].Stop(); | |
audioSourceResultList[0].Play(); | |
} | |
int kuttukuCount = 0; | |
float lastkuttukuTime = 0; | |
public void PlayKuttuku() | |
{ | |
kuttukuCount++; | |
if(kuttukuCount%4 == 3){ | |
if(audioSourceResultList[0].isPlaying == false && | |
audioSourceTitleList[0].isPlaying == false && | |
lastkuttukuTime+2 < Time.timeSinceLevelLoad){ | |
lastkuttukuTime = Time.timeSinceLevelLoad; | |
PlayBGM(); | |
} | |
} | |
audioSourceKuttukuList[Random.Range(0,audioSourceKuttukuList.Count)].Play(); | |
} | |
public void PlayTimeUpSound(float vol) | |
{ | |
if(timeUpSoundSource != null){ | |
if(timeUpSoundSource.isPlaying == false) | |
{ | |
timeUpSoundSource.clip = timeUpSoundClip; | |
timeUpSoundSource.loop = true; | |
timeUpSoundSource.time = audioSourceBGMList[0].time; | |
timeUpSoundSource.Play(); | |
} | |
timeUpSoundSource.volume = vol; | |
} | |
} | |
public void StopTimeUpSound() | |
{ | |
if(timeUpSoundSource != null){ | |
timeUpSoundSource.Stop(); | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment