Unity 时间定时调度系统

这篇具有很好参考价值的文章主要介绍了Unity 时间定时调度系统。希望对大家有所帮助。如果存在错误或未考虑完全的地方,请大家不吝赐教,您也可以点击"举报违法"按钮提交疑问。

C# Unity 时间定时调度系统

之前的文章也有写过时间调度系统,但是没有支持异步调度只有回调调度,而且效率和代码可读性不是很好,下面介绍一种更优质的时间调度系统

1.TimerAction

首先需要定义一个时间行为,每次延时后需要干什么,延迟的时间类型是什么都需要使用TimerAction

public sealed class TimerAction : IDisposable
{
    private static long _id;//每个时间任务的ID生成
    public long Id;//每个时间任务的ID
    public long Time;//需要等待的时间(只有设置为Repeated的类型才有用)
    public object CallBack;//定时回调(不同类型的时间调度不同的类型可能是异步可能是委托所以用object类型)
    public TimerType TimerType;//时间调度类型
    public static TimerAction Create()
    {
        TimerAction timerAction = Pool<TimerAction>.Rent(); // 从引用池中创建时间调度任务减轻GC
        timerAction.Id = _id++;
        return timerAction;
    }
    public void Dispose()
    {
        Id = 0;
        Time = 0;
        CallBack = null;
        this.TimerType = TimerType.None;
        Pool<TimerAction>.Return(this);
    }
}

2.时间调度类型TimerType

public enum TimerType 
{
    None,
    OnceTimer,//单次回调委托类型的时间调度
    OnceWaitTimer,//单次异步时间调度(await/async)
    RepeatedTimer,//重复的时间调度
}

3.TimerScheduler时间调度器

这个调度器是个单例,单例在此不讲解实现可以看之前的文章有提

(1)获取当前时间

由于是在Unity环境下则使用Unity获取时间的方式,若在服务器或者纯C#的环境该时间的获取需要根据对应时间填入即可文章来源地址https://www.toymoban.com/news/detail-777410.html

    private long GetNowTime()
    {
        return (long)Time.time * 1000;
    }

(2)执行一个时间调度

    public long OnceTimer(long time, Action action)
    {
        return OnceTillTimer(GetNowTime() + time, action);
    }
    public long OnceTillTimer(long tillTime, Action action)
    {
        if (tillTime < GetNowTime())
            Debug.LogError($"new once time too small tillTime:{tillTime} Now:{GetNowTime()}");
        TimerAction timerAction = TimerAction.Create();
        timerAction.CallBack = action;
        timerAction.TimerType = TimerType.OnceTimer;
        AddTimer(tillTime, timerAction);
        return timerAction.Id;
    }
  • 这里说明一下_minTime 这个字段用于辨别最小时间是否达到,如果连最小时间都没有达到就不执行所有时间调度的检测
    private void AddTimer(long tillTime, TimerAction timerAction)
    {
        _timerActionDic.Add(timerAction.Id, timerAction);
        _timeIdDic.Add(tillTime, timerAction.Id);
        if (tillTime < _minTime)
            _minTime = tillTime;
    }

(3)执行时间调度检测

    private long _minTime;
    private readonly Queue<long> _timeOutTimeQueue = new();//tillTime
    private readonly Queue<long> _timeOutIdsQueue = new();//id
    private readonly Dictionary<long, TimerAction> _timerActionDic = new();//Key: id
    private readonly SortedOneToManyList<long, long> _timeIdDic = new();//Key: tillTime  Value: id
    public void Update()
    {
        try
        {
            long currTime = GetNowTime();
            if (_timeIdDic.Count == 0)
                return;
            if (currTime < _minTime)
                return;
            _timeOutTimeQueue.Clear();
            _timeOutIdsQueue.Clear();

            foreach (var (key, value) in _timeIdDic)
            {
                if (key > currTime)
                {
                    _minTime = key;
                    break;
                }
                _timeOutTimeQueue.Enqueue(key);
                
            }

            while (_timeOutTimeQueue.TryDequeue(out long tillTime))
            {
                foreach (long timerId in _timeIdDic[tillTime])
                {
                    _timeOutIdsQueue.Enqueue(timerId);
                }
                _timeIdDic.RemoveKey(tillTime);
            }

            while (_timeOutIdsQueue.TryDequeue(out long timerId))
            {
                if (!_timerActionDic.TryGetValue(timerId, out TimerAction timerAction))
                    continue;
                _timerActionDic.Remove(timerId);
                switch (timerAction.TimerType)
                {
                    case TimerType.OnceTimer:
                        {
                            Action action = (Action)timerAction.CallBack;
                            timerAction.Dispose();
                            if (action == null)
                            {
                                Debug.LogError("Call Back Is Null");
                                break;
                            }
                            action();
                        }
                        break;
                    case TimerType.RepeatedTimer:
                        {
                            Action action = (Action)timerAction.CallBack;
                            AddTimer(GetNowTime() + timerAction.Time, timerAction);
                            if (action == null)
                            {
                                Debug.LogError("Call Back Is Null");
                                break;
                            }
                            action();
                        }
                        break;
                    case TimerType.OnceWaitTimer:
                        {
                            TaskCompletionSource<bool> taskCompletionSource = (TaskCompletionSource<bool>)timerAction.CallBack;
                            timerAction.Dispose();
                            taskCompletionSource.SetResult(true);
                        }
                        break;
                    default:
                        break;
                }
            }
        }
        catch (Exception ex)
        {
            Debug.LogError(ex.Message);
        }
    }

(4)TimerScheduler完整代码

public class TimerScheduler : Singleton<TimerScheduler>, IUpdateSingleton
{
    private long _minTime;
    private readonly Queue<long> _timeOutTimeQueue = new();//tillTime
    private readonly Queue<long> _timeOutIdsQueue = new();//id
    private readonly Dictionary<long, TimerAction> _timerActionDic = new();//Key: id
    private readonly SortedOneToManyList<long, long> _timeIdDic = new();//Key: tillTime  Value: id
    private long GetNowTime()
    {
        return (long)Time.time * 1000;
    }
    private void AddTimer(long tillTime, TimerAction timerAction)
    {
        _timerActionDic.Add(timerAction.Id, timerAction);
        _timeIdDic.Add(tillTime, timerAction.Id);
        if (tillTime < _minTime)
            _minTime = tillTime;
    }
    public long OnceTimer(long time, Action action)
    {
        return OnceTillTimer(GetNowTime() + time, action);
    }
    public long OnceTillTimer(long tillTime, Action action)
    {
        if (tillTime < GetNowTime())
            Debug.LogError($"new once time too small tillTime:{tillTime} Now:{GetNowTime()}");
        TimerAction timerAction = TimerAction.Create();
        timerAction.CallBack = action;
        timerAction.TimerType = TimerType.OnceTimer;
        AddTimer(tillTime, timerAction);
        return timerAction.Id;
    }
    public long RepeatedTimer(long time,Action action)
    {
        if (time <= 0)
        {
            throw new Exception("repeated time <= 0");
        }
        long tillTime = GetNowTime() + time;
        TimerAction timerAction = TimerAction.Create();
        timerAction.CallBack = action;
        timerAction.TimerType = TimerType.RepeatedTimer;
        timerAction.Time = time;
        AddTimer(tillTime, timerAction);
        return timerAction.Id;
    }
    public void Remove(long timerId)
    {
        if (!_timerActionDic.Remove(timerId, out TimerAction timerAction))
            return;
        timerAction?.Dispose();
    }
    public void Update()
    {
        try
        {
            long currTime = GetNowTime();
            if (_timeIdDic.Count == 0)
                return;
            if (currTime < _minTime)
                return;
            _timeOutTimeQueue.Clear();
            _timeOutIdsQueue.Clear();

            foreach (var (key, value) in _timeIdDic)
            {
                if (key > currTime)
                {
                    _minTime = key;
                    break;
                }
                _timeOutTimeQueue.Enqueue(key);
                
            }

            while (_timeOutTimeQueue.TryDequeue(out long tillTime))
            {
                foreach (long timerId in _timeIdDic[tillTime])
                {
                    _timeOutIdsQueue.Enqueue(timerId);
                }
                _timeIdDic.RemoveKey(tillTime);
            }

            while (_timeOutIdsQueue.TryDequeue(out long timerId))
            {
                if (!_timerActionDic.TryGetValue(timerId, out TimerAction timerAction))
                    continue;
                _timerActionDic.Remove(timerId);
                switch (timerAction.TimerType)
                {
                    case TimerType.OnceTimer:
                        {
                            Action action = (Action)timerAction.CallBack;
                            timerAction.Dispose();
                            if (action == null)
                            {
                                Debug.LogError("Call Back Is Null");
                                break;
                            }
                            action();
                        }
                        break;
                    case TimerType.RepeatedTimer:
                        {
                            Action action = (Action)timerAction.CallBack;
                            AddTimer(GetNowTime() + timerAction.Time, timerAction);
                            if (action == null)
                            {
                                Debug.LogError("Call Back Is Null");
                                break;
                            }
                            action();
                        }
                        break;
                    case TimerType.OnceWaitTimer:
                        {
                            TaskCompletionSource<bool> taskCompletionSource = (TaskCompletionSource<bool>)timerAction.CallBack;
                            timerAction.Dispose();
                            taskCompletionSource.SetResult(true);
                        }
                        break;
                    default:
                        break;
                }
            }
        }
        catch (Exception ex)
        {
            Debug.LogError(ex.Message);
        }
    }
    public async Task<bool> WaitAsync(long time, CancellationAction cancellationAction = null)
    {
        return await WaitTillAsync(GetNowTime() + time, cancellationAction);
    }
    public async Task<bool> WaitTillAsync(long tillTime, CancellationAction cancellationAction = null)
    {
        if (GetNowTime() > tillTime)
            return true;
        TaskCompletionSource<bool> taskCompletionSource = new TaskCompletionSource<bool>();
        TimerAction timerAction = TimerAction.Create();
        long timerId = timerAction.Id;
        timerAction.CallBack = taskCompletionSource;
        timerAction.TimerType = TimerType.OnceWaitTimer;

        void CancelAction()
        {
            if (!_timerActionDic.ContainsKey(timerId))
                return;
            Remove(timerId);
            taskCompletionSource.SetResult(false);
        }

        bool b;
        try
        {
            cancellationAction?.Add(CancelAction);
            AddTimer(tillTime, timerAction);
            b = await taskCompletionSource.Task;
        }
        catch(Exception ex)
        {
            Debug.LogError(ex.Message);
            return true;
        }

        return b;
    }

    protected override void Load(int assemblyName)
    {
    }

    protected override void UnLoad(int assemblyName)
    {
    }
}

4.测试

  • 提供一下CancellationAction代码实现,用于一切的取消事件(非时间调度器专属,任何取消操作都可参考)
public sealed class CancellationAction
{
    private HashSet<Action> _actions = new HashSet<Action>();
    public bool IsCanel => _actions == null;

    public void Add(Action action)
    {
        _actions.Add(action);
    }

    public void Remove(Action action)
    {
        _actions.Remove(action);
    }

    public void Cancel()
    {
        if (_actions == null)
            return;

        foreach (Action action in _actions)
        {
            try
            {
                action?.Invoke();
            }
            catch (Exception e)
            {
                Debug.LogError(e.Message);
            }
        }

        _actions.Clear();
        _actions = null;
    }
}
public class TestTimer : MonoBehaviour
{
    private long repeatedId;
    private CancellationAction timerCancelAction = new CancellationAction();
    async void Start()
    {
        SingletonSystem.Initialize();
        AssemblyManager.Initialize();

        TimerScheduler.Instance.OnceTimer(5000, () =>
        {
            Debug.Log("第一个5s后了!!!");
        });
        TimerScheduler.Instance.OnceTimer(5000, () =>
        {
            Debug.Log("第二个5s后了!!!");
        });
        TimerScheduler.Instance.OnceTimer(6000, () =>
        {
            Debug.Log("6s后了!!!");
        });

        repeatedId = TimerScheduler.Instance.RepeatedTimer(2000, () =>
        {
            Debug.Log("每两秒重复运行!!!");
        });

        await TimerScheduler.Instance.WaitAsync(3000);
        Debug.Log("这是第一个3s以后");
        await TimerScheduler.Instance.WaitAsync(3000);
        Debug.Log("这是第二个3s以后");

        bool isCanel = await TimerScheduler.Instance.WaitAsync(5000, timerCancelAction);
        if (!isCanel)
        {
            Debug.Log("取消定时");
        }
        else
        {
            Debug.Log("五秒后执行!!!");
        }
    }

    void Update()
    {
        SingletonSystem.Update();
        if (Input.GetKeyDown(KeyCode.P))
        {
            if (!timerCancelAction.IsCanel)
            {
                timerCancelAction.Cancel();
            }
        }
    }
}

到了这里,关于Unity 时间定时调度系统的文章就介绍完了。如果您还想了解更多内容,请在右上角搜索TOY模板网以前的文章或继续浏览下面的相关文章,希望大家以后多多支持TOY模板网!

本文来自互联网用户投稿,该文观点仅代表作者本人,不代表本站立场。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如若转载,请注明出处: 如若内容造成侵权/违法违规/事实不符,请点击违法举报进行投诉反馈,一经查实,立即删除!

领支付宝红包 赞助服务器费用

相关文章

  • Unity、UE、Cocos游戏开发引擎的区别

    Unity、Unreal Engine(UE)和Cocos引擎是三个常用的游戏开发引擎,它们在功能和特性上有一些区别。以下是它们之间的主要区别: 编程语言:Unity使用C#作为主要的编程语言,开发者可以使用C#脚本进行游戏逻辑编写。Unreal Engine主要使用C++作为编程语言,但也支持蓝图系统,允许

    2024年02月22日
    浏览(47)
  • Unity vs Godot :哪个游戏引擎更适合你?

    游戏引擎的选择对开发过程和最终产品质量有着重大影响。近年来,Godot和Unity这两款引擎受到广泛关注。本文将从多个维度对两者进行比较,以期为开发者提供正确的选择建议。 Godot和Unity都有各自的优势,没有绝对的好坏之分。Godot开源免费,上手简单,更适合2D和小型游戏

    2024年01月23日
    浏览(74)
  • 30分钟了解所有引擎组件,132个Unity 游戏引擎组件速通!【收藏 == 学会】

    🎬 博客主页:https://xiaoy.blog.csdn.net 🎥 本文由 呆呆敲代码的小Y 原创,首发于 CSDN 🙉 🎄 学习专栏推荐:Unity系统学习专栏 🌲 游戏制作专栏推荐:游戏制作 🌲Unity实战100例专栏推荐:Unity 实战100例 教程 🏅 欢迎点赞 👍 收藏 ⭐留言 📝 如有错误敬请指正! 📆 未来很长

    2024年02月11日
    浏览(52)
  • Unity Physics2D 2d物理引擎游戏 笔记

    2d 材质 里面可以设置 摩擦力 和 弹力 Simulated:是否在当前的物理环境中模拟,取消勾选该框类似于Disable Rigidbody,但使用这个参数更加高效,因为Disable会销毁内部产生的GameObject,而取消勾选Simulated只是禁用。 Kinematic 动力学刚体 动力学刚体不受重力和力的影响,而受用户的

    2023年04月24日
    浏览(102)
  • Unity和UE4两大游戏引擎,你该如何选择?

    目录 游戏引擎 2 —— 难易区别 编程语言 3 —— 游戏产品 UE4制作的游戏产品  Unity制作的游戏产品  产品类型 5 —— 资源商店 6 —— 人才需求 平均薪资 总结      Unity和UE4都是游戏引擎,所谓游戏引擎就是集成了复杂功能的游戏开发软件,他们帮我们实现了复杂的底层逻

    2023年04月08日
    浏览(53)
  • GODOT游戏引擎简介,包含与unity性能对比测试,以及选型建议

    GODOT,是一个免费开源的3D引擎。本文以unity作对比,简述两者区别和选型建议。由于是很久以前写的ppt,技术原因视频和部分章节丢失了。建议当做业务参考。 GODOT目前为止遇到3个比较重大的机遇,第一个是oprea的合作奖,第二个是用支持c#换来的微软的投资,第三个是虚幻

    2024年02月14日
    浏览(48)
  • Unity 开发人员转CGE(castle Game engine)城堡游戏引擎指导手册

    一、简介 2. Unity相当于什么GameObject? 3. 如何设计一个由多种资产、生物等组成的关卡? 4. 在哪里放置特定角色的代码(例如生物、物品)?Unity 中“向 GameObject 添加 MonoBehaviour”相当于什么? 5.Unity子目录相当于什么Assets? 6. 支持哪些模型格式? 7. 支持FBX模型格式吗? 8.

    2024年02月07日
    浏览(41)
  • 【毕业论文】| 基于Unity3D引擎的冒险游戏的设计与实现

    📢博客主页:肩匣与橘 📢欢迎点赞 👍 收藏 ⭐留言 📝 如有错误敬请指正! 📢本文由 肩匣与橘 编写,首发于 CSDN 🙉 📢生活依旧是美好而又温柔的,你也是✨  基于Unity3D引擎的冒险游戏的设计与实现 📢前言 摘要 Abstract 1 绪论 1.1 选题背景 1.2 研究目的及意义 2 开发工具

    2024年02月05日
    浏览(46)
  • SuperMap Hi-Fi 3D SDK for Unity制作游戏引擎材质

    kele     在交通,电力,规划等行业中,有的对象常常具有很强的质感,比如金属质感的 钢轨,电力塔;陶瓷材质的绝缘子;玻璃材质的建筑幕墙等,但常规方式的表现效果 往往差强人意。     游戏引擎(Unity3D)中已有丰富的材质资源库,比如玻璃,金属等材质,这

    2024年02月09日
    浏览(75)
  • 吐槽laya:H5小游戏开发应该用什么引擎好?laya、cocos还是unity?

    我看有人推荐laya,放在H5小游戏的前三排名,这压根不靠谱。 laya只能算个半成品,整体非常垃圾,如果是首次选择游戏引擎,至少转去cocos,实在选laya,那也没办法了。 下面说说laya有什么问题,如果只是一些简单的bug什么的,我是不会花这个时间吐槽的,但是如下的问题实

    2024年02月13日
    浏览(48)

觉得文章有用就打赏一下文章作者

支付宝扫一扫打赏

博客赞助

微信扫一扫打赏

请作者喝杯咖啡吧~博客赞助

支付宝扫一扫领取红包,优惠每天领

二维码1

领取红包

二维码2

领红包