时间轴上的定时行为

21 篇文章 1 订阅
17 篇文章 0 订阅
public class TimeActioner
{
    private float loadTime;
    private float delayTime;
    private float repeatTime;
    private int totalRepeatCount = 1;//如果为-1 就永不停止
    private int curRepeatCount = 0;
    public int TimeAcId;
    private System.Action<float> action;
    public TimeActioner(int pId,float pLoadTime,float pDelayTime,System.Action<float> pAction,float pRepeatTime,int pRepeatCount = 1)
    {
        TimeAcId = pId;
        loadTime = pLoadTime;
        delayTime = pDelayTime;
        repeatTime = pRepeatTime;
        totalRepeatCount = pRepeatCount;
        action = pAction;
    }

    public bool OnRuning(float pTime)
    {
        curRepeatCount++;
        if(action != null)
            action.Invoke(pTime);
        return curRepeatCount >= totalRepeatCount && totalRepeatCount > 0;
    }
    public bool IsPass(float pTime)
    {
        if (curRepeatCount < totalRepeatCount)
        {
            if (pTime >= (delayTime + loadTime + curRepeatCount * repeatTime))
            {
                return true;
            }
        }
        return false;
    }

}

using System.Collections;
using System.Collections.Generic;
using UnityEngine;

public enum TimeState
{ 
    Runing = 1,//运行
    Pause = 2,//暂停
}
public class Timer:MonoBehaviour
{
    public TimeState state = TimeState.Runing;
    private Dictionary<int, TimeActioner> Timers = new Dictionary<int, TimeActioner>();
    public float time;
    public float Speed = 1;
    private List<TimeActioner> passTimers = new List<TimeActioner>();
    private List<int> endTimer = new List<int>();
    private int acId = 0;
    private void Update()
    {
        if (state == TimeState.Pause)
            return;
        OnRun(Time.deltaTime);
    }
    public int AddTimeActioner(float pDelayTime, System.Action<float> pAction, float pRepeatTime = 1, int pRepeatCount = 1)
    {
        acId++;
        var timeActioner = new TimeActioner(acId,time,pDelayTime,pAction,pRepeatTime,pRepeatCount);
        Timers.Add(acId, timeActioner);
        return acId;
    }
    public void RemoveTimer(int pAcId)
    {
        if (Timers.ContainsKey(pAcId))
        {
            Timers.Remove(pAcId);
        }
    }
    public void OnRun(float pDeltaTime)
    {
        time += pDeltaTime*Speed;
        CheckTimer(time);
        endTimer.Clear();
        foreach (var temp in passTimers)
        {
            var isEnd = temp.OnRuning(time);
            if (isEnd)
            {
                endTimer.Add(temp.TimeAcId);
            }
        }
        foreach (var temp in endTimer)
        {
            RemoveTimer(temp);
        }
    }
    public void CheckTimer(float pTime){
        passTimers.Clear();
        foreach (var temp in Timers)
        {
            if (temp.Value.IsPass(pTime))
            {
                passTimers.Add(temp.Value);
            }
        }
    }
}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值