首页 > 编程语言 > 详细

Unity自定义定时器

时间:2015-06-03 21:45:49      阅读:410      评论:0      收藏:0      [点我收藏+]

自己写的一个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
}

Unity自定义定时器

原文:http://blog.csdn.net/congcongjoy/article/details/46349293

(0)
(0)
   
举报
评论 一句话评论(0
关于我们 - 联系我们 - 留言反馈 - 联系我们:wmxa8@hotmail.com
© 2014 bubuko.com 版权所有
打开技术之扣,分享程序人生!