Created
January 28, 2014 01:50
-
-
Save adarapata/8661071 to your computer and use it in GitHub Desktop.
Unityの自作タイマー
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
using UnityEngine; | |
using System; | |
using System.Collections; | |
/// <summary> | |
/// 全てのTimerクラスをupdateさせる | |
/// </summary> | |
public class TimerExecutor : MonoBehaviour { | |
public event Action timeUpdate; | |
void Update() | |
{ | |
if(timeUpdate != null)timeUpdate(); | |
} | |
} | |
public class Timer | |
{ | |
public float nowTime { get; private set; } | |
public float waitingTime { get; private set; } | |
public bool isLoop {get; private set; } | |
public bool isFinished { get { return nowTime > waitingTime; } } | |
private Action notify; | |
public Timer (float time, Action callBack) | |
{ | |
nowTime = 0; | |
waitingTime = time; | |
notify = callBack; | |
} | |
/// <summary> | |
/// メソッドを呼ぶ必要が無い場合こちらをつかう | |
/// </summary> | |
/// <param name="time">Time.</param> | |
public Timer(float time) | |
{ | |
nowTime = 0; | |
waitingTime = time; | |
notify = null; | |
} | |
public void Start(bool loop) | |
{ | |
isLoop = loop; | |
Singleton<TimerExecutor>.Instance.timeUpdate += Update; | |
} | |
private void Update () | |
{ | |
nowTime += Time.deltaTime; | |
if (nowTime > waitingTime) { | |
if(notify != null)notify (); | |
if(isLoop) { nowTime = 0;} | |
else { Singleton<TimerExecutor>.Instance.timeUpdate -= Update; } | |
} | |
} | |
/// <summary> | |
/// 時間経過をリセットする | |
/// </summary> | |
public void Reset() | |
{ | |
nowTime = Time.deltaTime; | |
nowTime = 0; | |
} | |
/// <summary> | |
/// 新たにタイマーをセットした上でリセット | |
/// </summary> | |
/// <param name="time">Time.</param> | |
public void Reset(float time) | |
{ | |
waitingTime = time; | |
Reset(); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment