Created
December 19, 2013 11:23
-
-
Save voidtuxic/8037774 to your computer and use it in GitHub Desktop.
C# Metronome for Unity3D
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 delegate void MetronomeEvent(Metronome metronome); | |
public class Metronome : MonoBehaviour { | |
public int Base; | |
public int Step; | |
public float BPM; | |
public int CurrentStep = 1; | |
public int CurrentMeasure; | |
private float interval; | |
private float nextTime; | |
public event MetronomeEvent OnTick; | |
public event MetronomeEvent OnNewMeasure; | |
// Use this for initialization | |
void Start () { | |
StartMetronome(); | |
} | |
// Update is called once per frame | |
void Update () { | |
} | |
public void StartMetronome() | |
{ | |
StopCoroutine("DoTick"); | |
CurrentStep = 1; | |
var multiplier = Base / 4f; | |
var tmpInterval = 60f / BPM; | |
interval = tmpInterval / multiplier; | |
nextTime = Time.time; | |
StartCoroutine("DoTick"); | |
} | |
IEnumerator DoTick() | |
{ | |
for (; ; ) | |
{ | |
if (CurrentStep == 1 && OnNewMeasure != null) | |
OnNewMeasure(this); | |
if (OnTick != null) | |
OnTick(this); | |
nextTime += interval; | |
yield return new WaitForSeconds(nextTime - Time.time); | |
CurrentStep++; | |
if (CurrentStep > Step) | |
{ | |
CurrentStep = 1; | |
CurrentMeasure++; | |
} | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment