Unity-TimerMgr

74 篇文章 5 订阅

以前在开发页游的时候,自己造过很多轮子:TimerMgr 也是其一

现在用 Unity 做开发,也少不了这个功能,所以再次造了个轮子

制作这个功能目的在于,部分场景可替代使用 Unity Coroutine,或是 async/await Task.Delay,因为这两种方式都有 GC 问题

但是 TimerMgr<T>的泛型回调也有 GC 这个是 C#.NET 的问题,但是具体使用消耗最低,可以查看 Git 中的 TimerMgr.cs 的注释说明


运行效果

在这里插入图片描述


C# 应用层代码示例

using UnityEngine;
using UnityEngine.UI;

// author   :   jave.lin
// 测试 TimerMgr
// TimerMgr     无参数 Timer
// TimerMgr<T>  有 T 类型参数的 Timer
public class Testing : MonoBehaviour
{
    public int no_arg_with_time_scale_timer_id = -1;
    public int no_arg_without_time_scale_timer_id = -1;

    public float with_time_scale_count;
    public float without_time_scale_count;

    public Image with_time_scale_img;
    public Image without_time_scale_img;

    public Slider timescale_slider;
    public Text timescale_txt;

    private void Start()
    {
        timescale_slider.value = Time.timeScale;
        timescale_txt.text = timescale_slider.value.ToString();
    }

    public void OnWithTimeScaleAddTimerBtnClick()
    {
        if (no_arg_with_time_scale_timer_id == -1)
        {
            no_arg_with_time_scale_timer_id = TimerMgr.Inst.AddTimer(no_arg_with_time_scale_timer_update, no_arg_with_time_scale_timer_complete, 0.1f, 0, true);
            Debug.Log($"OnWithTimeScaleAddTimerBtnClick, AddTimer, id : {no_arg_with_time_scale_timer_id}");
        }
    }

    public void OnWithTimeScaleRemoveTimerBtnClick()
    {
        if (no_arg_with_time_scale_timer_id != -1)
        {
            var remove_success = TimerMgr.Inst.RemoveTimerById(no_arg_with_time_scale_timer_id);
            Debug.Log($"OnWithTimeScaleRemoveTimerBtnClick, RemoveTimerById, id : {no_arg_with_time_scale_timer_id}, remove_success : {remove_success}");
            no_arg_with_time_scale_timer_id = -1;
        }
    }

    private void no_arg_with_time_scale_timer_update()
    {
        Debug.Log($"no_arg_with_time_scale_timer_update");
        with_time_scale_count += 0.1f;
        if (with_time_scale_count > 1.0f)
        {
            with_time_scale_count = 0.0f;
        }
        with_time_scale_img.fillAmount = with_time_scale_count;
    }

    private void no_arg_with_time_scale_timer_complete()
    {
        Debug.Log($"no_arg_with_time_scale_timer_complete");
    }

    //==========================
    public void OnWithoutTimeScaleAddTimerBtnClick()
    {
        if (no_arg_without_time_scale_timer_id == -1)
        {
            no_arg_without_time_scale_timer_id = TimerMgr.Inst.AddTimer(no_arg_without_time_scale_timer_update, no_arg_without_time_scale_timer_complete, 0.1f, 0, false);
            Debug.Log($"OnWithoutTimeScaleAddTimerBtnClick, AddTimer, id : {no_arg_without_time_scale_timer_id}");
        }
    }

    public void OnWithoutTimeScaleRemoveTimerBtnClick()
    {
        if (no_arg_without_time_scale_timer_id != -1)
        {
            var remove_success = TimerMgr.Inst.RemoveTimerById(no_arg_without_time_scale_timer_id);
            Debug.Log($"OnWithoutTimeScaleRemoveTimerBtnClick, RemoveTimerById, id : {no_arg_without_time_scale_timer_id}, remove_success : {remove_success}");
            no_arg_without_time_scale_timer_id = -1;
        }
    }

    private void no_arg_without_time_scale_timer_update()
    {
        Debug.Log($"no_arg_without_time_scale_timer_update");
        without_time_scale_count += 0.1f;
        if (without_time_scale_count > 1.0f)
        {
            without_time_scale_count = 0.0f;
        }
        without_time_scale_img.fillAmount = without_time_scale_count;
    }

    private void no_arg_without_time_scale_timer_complete()
    {
        Debug.Log($"no_arg_without_time_scale_timer_complete");
    }
    //==========================

    public void OnTimeScaleSlider()
    {
        Time.timeScale = timescale_slider.value;
        timescale_txt.text = Time.timeScale.ToString();
    }

    //==========================

    public void OnDelaySayHiBtnClick()
    {
        TimerMgr<string>.Inst.AddTimer(name => { Debug.Log($"Hi, {name}"); }, "Jave", null, null);
    }
}

Timer原来参考下面的 Project


Project

backup:

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 2
    评论
评论 2
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值