关于u3d:U3D游戏开发框架五计时器

37次阅读

共计 2739 个字符,预计需要花费 7 分钟才能阅读完成。

一:目标

游戏开发中,计时是一个常见的性能。比方技能的冷却工夫,角色的生命回复等等

个别的实现办法是通过协程或者 Invoke,然而这样并没有对立治理所有的计时器,所以咱们须要一个管理器对立治理计时器的相干操作

二:解决的问题及长处

——注册和登记计时器
——失去某一个计时器的倒计时剩余时间
——设置所有倒计时同步或异步

三:应用办法

——应用 TimerMgr.Ins.XXX

TimerMgr.Ins.RegisterTimer(3, () =>
 {Debug.Log("finish 1");
 }, "", true);
TimerMgr.Ins.RegisterTimer(4, () =>
{Debug.Log("finish 2");
}, "", true);
TimerMgr.Ins.UnRegisterAllTimer();
TimerMgr.Ins.UnRegisterTimer("Timer1");
TimerMgr.Ins.GetRemainTime("Timer1");

四:代码实现

using System.Collections.Generic;
using UnityEngine;
using System;
 
/// <summary>
/// 计时管理器
/// </summary>
public class TimerMgr : MonoSingleton<TimerMgr>
{
    // 是否同步计时
    private bool isSync = true;
    public bool IsSync
    {
        get
        {return isSync;}
        set
        {isSync = value;}
    }
 
    private List<TimerData> timerList = new List<TimerData>();// 倒计时列表
 
    private void Awake()
    {lastUpdateTime = Time.realtimeSinceStartup;}
 
    /// <summary>
    /// 注册倒计时
    /// </summary>
    public void RegisterTimer(float countdown, Action onFinish = null, string timerName = "", bool ignoreTimeScale = false)
    {TimerData timerData = new TimerData(countdown, onFinish, timerName, ignoreTimeScale);
        timerList.Add(timerData);
    }
 
    /// <summary>
    /// 登记所有倒计时
    /// </summary>
    public void UnRegisterAllTimer()
    {timerList.Clear();
    }
    /// <summary>
    /// 登记某一个倒计时
    /// </summary>
    public void UnRegisterTimer(string name)
    {List<TimerData> unRegisterCache = new List<TimerData>();
        foreach (var timer in timerList)
        {if (timer.name == name)
            {unRegisterCache.Add(timer);
            }
        }
 
        for (int i = unRegisterCache.Count - 1; i >= 0; i--)
        {timerList.RemoveAt(i);
        }
    }
 
    /// <summary>
    /// 失去倒计时剩余时间
    /// </summary>
    public float GetRemainTime(string name)
    {foreach (var timer in timerList)
        {if (timer.name == name)
            {return timer.Timer;}
        }
        Debug.LogWarning("没有注册倒计时:" + name);
        return -1;
    }
 
    float lastUpdateTime;// 上一次的工夫
    private void Update()
    {
        float realDeltaTime = Time.realtimeSinceStartup - lastUpdateTime;
        lastUpdateTime = Time.realtimeSinceStartup;
 
        if (isSync)
        {for (int i = 0; i < timerList.Count; i++)
            {timerList[i].UpdateTimer(timerList[i].ignoreTimeScale ? realDeltaTime : Time.deltaTime);
                if (timerList[i].isFinish)
                {timerList.Remove(timerList[i]);
                    i--;
                }
            }
        }
        else
        {if (timerList.Count == 0) return;
 
            timerList[0].UpdateTimer(timerList[0].ignoreTimeScale ? realDeltaTime : Time.deltaTime);
            if (timerList[0].isFinish)
            {timerList.Remove(timerList[0]);
            }
        }
    }
}
 
/// <summary>
/// 计时器的数据类
/// </summary>
public class TimerData
{
    // 计时器
    private float timer;
    public float Timer
    {
        get
        {return timer;}
    }
 
    public float countdown;// 倒计时工夫
    public string name;// 倒计时的名称
    public bool ignoreTimeScale;// 是否疏忽工夫比例
    public Action onFinish;// 倒计时完结的回调
    public bool isFinish;// 是否实现倒计时
 
    /// <summary>
    /// 初始化计时器
    /// </summary>
    public TimerData(float countdown, Action onFinish, string name, bool ignoreTimeScale)
    {
        timer = countdown;
        this.countdown = countdown;
        this.onFinish = onFinish;
        this.name = name;
        this.ignoreTimeScale = ignoreTimeScale;
    }
 
    /// <summary>
    /// 更新倒计时
    /// </summary>
    public void UpdateTimer(float deltaTime)
    {
        timer -= deltaTime;
        if (timer <= 0)
        {
            isFinish = true;
            onFinish?.Invoke();}
    }
}

图片起源:http://www.coubai.com/ 网页游戏

正文完
 0