Skip to content

Instantly share code, notes, and snippets.

@bernatfortet bernatfortet/gist:4771372
Last active Dec 12, 2015

Embed
What would you like to do?
/*
USAGE
timer = gameObject.AddComponent<Timer>().setup( 1f, callBackHandler );
*/
using UnityEngine;
using System.Collections;
public delegate void TimerCallback();
public class Timer : MonoBehaviour {
public event TimerCallback timerCallbackEvent;
public float duration;
private float time = 0f;
public void setup( float duration, TimerCallback callbackEvent )
{
this.duration = duration;
timerCallbackEvent += new TimerCallback( callbackEvent );
}
void Start ()
{
time = Time.time;
Debug.Log( "starting" );
}
void Update ()
{
Debug.Log( "Updating" );
if( time <= Time.time - duration ){
onEnd();
}
}
void onEnd()
{
Debug.Log( "End" );
if( timerCallbackEvent != null ) timerCallbackEvent();
Destroy( this );
}
}
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.