Skip to content

Instantly share code, notes, and snippets.

@bernatfortet
Last active December 12, 2015 12:19
Show Gist options
  • Save bernatfortet/4771372 to your computer and use it in GitHub Desktop.
Save bernatfortet/4771372 to your computer and use it in GitHub Desktop.
/*
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