Skip to content

Instantly share code, notes, and snippets.

@tatmos
Last active November 2, 2017 05:21
Show Gist options
  • Save tatmos/7baa950c706f50526908cb3bcbe1a6e9 to your computer and use it in GitHub Desktop.
Save tatmos/7baa950c706f50526908cb3bcbe1a6e9 to your computer and use it in GitHub Desktop.
2014年くらいにUnityオーディオでビートに合わせてイベントを飛ばすようなもののSoundManager http://www.nicovideo.jp/watch/sm24159030 (GAMEOVERというタイトル)
using UnityEngine;
using System.Collections;
using System.Collections.Generic;
public class SoundManager : MonoBehaviour {
public static SoundManager main = null;
AudioSource audioSourceBGM;
public List<AudioClip> bgmClipList = new List<AudioClip>();
public enum TrgType {
CLOSE,
SWITCH_ENEMY,
OPEN,
}
public class BPM_Time_Struct
{
public BPM_Time_Struct(TrgType type, float t,float b,string n)
{
trgtype = type;
changeTime = t;
bpm = b;
name=n;
}
public TrgType trgtype = TrgType.CLOSE;
public float changeTime = 0;
public float bpm = 105;
public string name = "";
}
void Awake()
{
if(main != null)
{
GameObject.Destroy(this);
} else
{
main = this;
}
}
public List<BPM_Time_Struct> bpmList = new List<BPM_Time_Struct>();
ParticleSystem ps;
[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>();
}
// Use this for initialization
void Start () {
RenderStart();
bpmList.Add(new BPM_Time_Struct( TrgType.SWITCH_ENEMY, 0,105,"A"));
bpmList.Add(new BPM_Time_Struct( TrgType.OPEN, (9.03f/8)*1,105,"A"));
bpmList.Add(new BPM_Time_Struct( TrgType.CLOSE, (9.03f/8)*2,105,"A"));
bpmList.Add(new BPM_Time_Struct( TrgType.OPEN, (9.03f/8)*3,105,"A"));
bpmList.Add(new BPM_Time_Struct( TrgType.OPEN, (9.03f/8)*4,105,"A"));
bpmList.Add(new BPM_Time_Struct( TrgType.CLOSE, (9.03f/8)*5,105,"A"));
bpmList.Add(new BPM_Time_Struct( TrgType.SWITCH_ENEMY, (9.03f/8)*6,105,"A"));
bpmList.Add(new BPM_Time_Struct( TrgType.OPEN, 9.03f + ((17.66f-9.03f)/8)*0,118,"B"));
bpmList.Add(new BPM_Time_Struct( TrgType.CLOSE, 9.03f + ((17.66f-9.03f)/8)*1,118,"B"));
bpmList.Add(new BPM_Time_Struct( TrgType.OPEN, 9.03f + ((17.66f-9.03f)/8)*2,118,"B"));
bpmList.Add(new BPM_Time_Struct( TrgType.OPEN, 9.03f + ((17.66f-9.03f)/8)*3,118,"B"));
bpmList.Add(new BPM_Time_Struct( TrgType.CLOSE, 9.03f + ((17.66f-9.03f)/8)*4,118,"B"));
bpmList.Add(new BPM_Time_Struct( TrgType.OPEN, 9.03f + ((17.66f-9.03f)/8)*5,118,"B"));
bpmList.Add(new BPM_Time_Struct( TrgType.OPEN, 9.03f + ((17.66f-9.03f)/8)*6,118,"B"));
bpmList.Add(new BPM_Time_Struct( TrgType.CLOSE, 9.03f + ((17.66f-9.03f)/8)*7,118,"B"));
bpmList.Add(new BPM_Time_Struct( TrgType.SWITCH_ENEMY, 17.66f + ((29.22f-17.66f)/8)*0,152,"C"));
bpmList.Add(new BPM_Time_Struct( TrgType.OPEN, 17.66f + ((29.22f-17.66f)/8)*1,152,"C"));
bpmList.Add(new BPM_Time_Struct( TrgType.CLOSE, 17.66f + ((29.22f-17.66f)/8)*2,152,"C"));
bpmList.Add(new BPM_Time_Struct( TrgType.SWITCH_ENEMY, 17.66f + ((29.22f-17.66f)/8)*3,152,"C"));
bpmList.Add(new BPM_Time_Struct( TrgType.OPEN, 17.66f + ((29.22f-17.66f)/8)*4,152,"C"));
bpmList.Add(new BPM_Time_Struct( TrgType.CLOSE, 17.66f + ((29.22f-17.66f)/8)*5,152,"C"));
bpmList.Add(new BPM_Time_Struct( TrgType.SWITCH_ENEMY, 17.66f + ((29.22f-17.66f)/8)*6,152,"C"));
bpmList.Add(new BPM_Time_Struct( TrgType.OPEN, 17.66f + ((29.22f-17.66f)/8)*7,152,"C"));
bpmList.Add(new BPM_Time_Struct( TrgType.OPEN, 29.22f+2f,152,"EndPre"));
bpmList.Add(new BPM_Time_Struct( TrgType.CLOSE, 29.22f+4f,152,"End"));
EventPartChange += PartChangeTest;
audioSourceBGM = this.gameObject.AddComponent<AudioSource>();
if(Application.loadedLevelName == "game"){
audioSourceBGM.clip = bgmClipList[Random.Range(0,7)%(bgmClipList.Count-1)];
audioSourceBGM.Play();
}
}
void PartChangeTest(BPM_Time_Struct arg)
{
Debug.Log(arg.trgtype.ToString());
}
// int lastBeatNo = 0;
// int beatNo = 0;
BPM_Time_Struct GetBPM_Time_Struct(float time)
{
if(time == 0)
{
return bpmList[0];
}
for(int btsIndex = 0;btsIndex < bpmList.Count - 1;btsIndex++)
{
if(bpmList[btsIndex].changeTime < time && time < bpmList[btsIndex+1].changeTime)
{
return bpmList[btsIndex];
}
}
return bpmList[bpmList.Count-1];
}
BPM_Time_Struct lastBPM_Time_Struct;
public BPM_Time_Struct currentBPM_Time_Struct;
// Update is called once per frame
void Update () {
if(audioSourceBGM.isPlaying)
{
currentBPM_Time_Struct = GetBPM_Time_Struct(audioSourceBGM.time);
if(currentBPM_Time_Struct != lastBPM_Time_Struct)
{
EventPartChange(currentBPM_Time_Struct);
lastBPM_Time_Struct = currentBPM_Time_Struct;
}
}
}
//---------------イベント-------------------------------------------------------------------------
public delegate void PartChange (BPM_Time_Struct arg); // ビートイベントコールバック登録用デリゲート
static public PartChange EventPartChange; // ビートイベントコールバック登録用
//----------------------------------------------------------------------------------
public List<AudioClip> continueClipList = new List<AudioClip>();
public void PlayneCount(int neCount)
{
if(neCount%20 == 0){
//Debug.Log("neCount " + neCount);
audio.PlayOneShot(continueClipList[(neCount/20)%(continueClipList.Count-1)],0.41f);
}
}
public List<AudioClip> hajikuClipList = new List<AudioClip>();
int HajikuCount = 0;
public void PlayHajiku()
{
HajikuCount++;
if(HajikuCount%4 == 0){
audio.PlayOneShot(hajikuClipList[0],0.7f);
ps.enableEmission = true;
ps.maxParticles = 100;
}
}
public void PlayShut()
{
ps.maxParticles = 0;
ps.enableEmission = false;
{
audio.PlayOneShot(hajikuClipList[1],0.7f);
}
}
public void PlayClose()
{
ps.maxParticles = 0;
ps.enableEmission = false;
{
audio.PlayOneShot(hajikuClipList[2],0.7f);
}
}
int GrowlCount = 0;
public void PlayGrowl()
{
GrowlCount += Random.Range(1,3);
ps.maxParticles = 0;
ps.enableEmission = false;
if(GrowlCount%8 == 0)
{
audio.PlayOneShot(hajikuClipList[3],0.7f);
}
}
public List<AudioClip> titleClipList = new List<AudioClip>();
public void PlayTitle()
{
//audio.PlayOneShot(titleClipList[0],0.7f);
audioSourceBGM.clip = titleClipList[0];
audioSourceBGM.loop = true;
audioSourceBGM.Play();
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment