Skip to content

Instantly share code, notes, and snippets.

What would you like to do?
C# Metronome for Unity3D
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 () {
// Update is called once per frame
void Update () {
public void StartMetronome()
CurrentStep = 1;
var multiplier = Base / 4f;
var tmpInterval = 60f / BPM;
interval = tmpInterval / multiplier;
nextTime = Time.time;
IEnumerator DoTick()
for (; ; )
if (CurrentStep == 1 && OnNewMeasure != null)
if (OnTick != null)
nextTime += interval;
yield return new WaitForSeconds(nextTime - Time.time);
if (CurrentStep > Step)
CurrentStep = 1;
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
You can’t perform that action at this time.
You signed in with another tab or window. Reload to refresh your session. You signed out in another tab or window. Reload to refresh your session.