Unity自定义定时器
2015-06-03 18:00
579 查看
自己写的一个Timer,绑定到一个prefab上,使用时直接Instantaite,要用几个就Instantiate几个,非常方便
using UnityEngine; using System.Collections; public class Timer : MonoBehaviour { private float _time; private float _lastTime; private bool _isStart; public float time { get { return _time; } } public bool isStart { get { return _isStart; } } // Use this for initialization void Start() { _time = 0; _lastTime = 0; _isStart = false; } // Update is called once per frame void Update() { if (_isStart) { _time = Time.time - _lastTime; //Debug.Log("Time:" + _time); } } public void RestartTimer() { _lastTime = Time.time; _isStart = true; Debug.Log("RestartTimer"); } public void StopTimer() { if (_isStart) { _lastTime = 0; _time = 0; _isStart = false; Debug.Log("StopTimer"); } } public void PauseTimer() { if (_isStart) { _lastTime = _time; _isStart = false; Debug.Log("PauseTimer"); } } public void StartTimer() { if (!_isStart) { _lastTime = Time.time - time; _isStart = true; Debug.Log("StartTimer"); } } #region //for test //void OnGUI() //{ // if (GUI.Button(new Rect(0, 0, 200, 200), "Start")) // { // StartTimer(); // } // if (GUI.Button(new Rect(200, 0, 200, 200), "Pause")) // { // PauseTimer(); // } // if (GUI.Button(new Rect(400, 0, 200, 200), "Stop")) // { // StopTimer(); // } // if (GUI.Button(new Rect(600, 0, 200, 200), "Restart")) // { // RestartTimer(); // } //} #endregion }
相关文章推荐
- Mootools 1.2教程 定时器和哈希简介
- C#中timer定时器用法实例
- JS中自定义定时器让它在某一时刻执行
- JavaScript定时器详解及实例
- asp.net中Timer无刷新定时器的实现方法
- libevent库的使用--定时器的使用实例
- MySQL定时器EVENT学习笔记
- js函数定时器实现定时读取系统实时连接数
- linux使用select实现精确定时器详解
- Cocos2d-x Schedule定时器的使用实例
- VC定时器的用法实例详解
- c#定时器和global实现自动job示例
- JavaScript学习笔记之定时器
- js定时器的使用(实例讲解)
- c#定时器使用示例详解
- 深入探寻javascript定时器
- 浅谈javascript语法和定时函数
- JS定时器实例详细分析
- Nodejs极简入门教程(二):定时器
- javascript + jquery实现定时修改文章标题