Skip to content

Instantly share code, notes, and snippets.

@poemdexter
Created June 19, 2013 01:42
Show Gist options
  • Save poemdexter/5811078 to your computer and use it in GitHub Desktop.
Save poemdexter/5811078 to your computer and use it in GitHub Desktop.
music in unity proof of concept
using UnityEngine;
using System.Collections;
[RequireComponent(typeof(AudioSource))]
public class SoundPOC : MonoBehaviour
{
FContainer container;
CBox greenbox;
bool started = false;
double start;
double nextBeat;
double bpm = 95;
void Awake()
{
Application.targetFrameRate = 60;
}
// Use this for initialization
void Start ()
{
FutileParams fparams = new FutileParams(true,true,false,false);
fparams.AddResolutionLevel(640.0f, 1.0f, 1.0f, "");
fparams.origin = new Vector2(0.5f, 0.5f);
fparams.backgroundColor = Color.grey;
Futile.instance.Init(fparams);
Futile.atlasManager.LoadAtlas("Atlases/squares");
container = new FContainer();
Futile.stage.AddChild(container);
greenbox = new CBox("green_square");
container.AddChild(greenbox);
// music stuff
start = AudioSettings.dspTime + 5; // 5s wait
audio.PlayScheduled(start);
}
// Update is called once per frame
void Update ()
{
double time = AudioSettings.dspTime;
if (!started)
{
if (time >= start)
{
greenbox.Move();
nextBeat = time + (60/bpm);
started = true;
}
}
else
{
if (time >= nextBeat)
{
greenbox.Move();
nextBeat += (60/bpm);
}
}
}
public class CBox : FSprite
{
bool Direction = true;
public CBox(string name) : base(name) {}
public void Move()
{
y = (Direction) ? y + 50 : y - 50;
Direction = !Direction;
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment