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