游戏中时间管理器

来源:互联网 发布:淘宝樊小林捕鼠 编辑:程序博客网 时间:2024/05/20 13:07

摘自我司主程周峰之  gametimer.cs


using System;
using System.IO;
using System.Threading;
using UnityEngine;
using System.Collections;


public class GameTimer : MonoBehaviour, PauseAbleBase
{


    protected static GameTimer _inst = null;


    public static GameTimer Instance
    {
        get
        {
            if (_inst == null)
            {
                _inst = FindObjectOfType<GameTimer>();
                if (_inst == null)
                {
                    var obj = new GameObject("__GameTimer");
                    _inst = obj.AddComponent<GameTimer>();
                }
            }
            return _inst;
        }
    }


    void OnEnable() { _inst = this; }
    void OnDisable() { _inst = null; }

// Update is called once per frame
void Update ()
{
        if (!IsPaused)
       TimeFromSceneLoad += Time.deltaTime;
}


    public float DeltaTime
    {
        get
        {
            if (! IsPaused)
            {
                return Time.deltaTime;
            }
            else
            {
                return 0;
            }
        }
    }


    private float TimeFromSceneLoad = 0f;


    private bool IsPaused = false;




    public bool Pause
    {
        get { return IsPaused; }
    }


    public void OnPause()
    {
        // pause audio
        AudioManager.Instance.OnPause();
        EnemyController.Instance.PauseGame();
        if (BossBase.Instance)
        {
            BossBase.Instance.WhenPause();
        }


        if (UIBattleManager.Instance)
        {
            UIBattleManager.Instance.PauseBuff();
        }
        IsPaused = true;
    }


    public void OnResume()
    {
        // resume audio
        AudioManager.Instance.OnResume();
        IsPaused = false;
        EnemyController.Instance.ResumeGame();
        if (BossBase.Instance)
        {
            BossBase.Instance.WhenResume();
        }
        if (UIBattleManager.Instance)
        {
            UIBattleManager.Instance.ResumeBuff();
        }
    }


    public float TimeFromStart
    {
        get { return TimeFromSceneLoad; }
    }


    /// <summary>
    /// 获得格林尼治时间ticks
    /// </summary>
    public static long SystemNowTimeStamp
    {
        get { return DateTime.Now.ToFileTime(); }
    }


    public static void GetNowTimeFormat(out int year,out int month, out int day)
    {
        GetTimeFormat(out year, out month, out day, SystemNowTimeStamp);
    }


    public static void GetTimeFormat(out int year, out int month,out int day, long timeStamp)
    {
        var date = DateTime.FromFileTime(timeStamp);
        year = date.Year;
        month = date.Month;
        day = date.Day;
    }


    public static bool IsSameDay(long timestamp1, long timestamp2)
    {
        int year, month, day, lastYear, lastMonth, lastDay;
        GetTimeFormat(out lastYear, out lastMonth, out lastDay, timestamp1);
        GetTimeFormat(out year, out month, out day, timestamp2);
        if (year != lastYear || month != lastMonth || day != lastDay)
        {
            return false;
        }
        else
        {
            return true;
        }
    }




    public static int TimeSpanMinites(long after, long before)
    {
        TimeSpan span = new TimeSpan(after - before);
        //span.TotalMinutes
        return Mathf.FloorToInt((float)span.TotalMinutes);
    }


    public static double TimeSpanSeconds(long after, long before)
    {
        TimeSpan span = new TimeSpan(after-before);
        return ( span.TotalSeconds);
    }
}

0 0
原创粉丝点击