温馨提示×

温馨提示×

您好,登录后才能下订单哦!

密码登录×
登录注册×
其他方式登录
点击 登录注册 即表示同意《亿速云用户服务条款》

unity如何实现延迟回调工具

发布时间:2021-09-26 13:51:58 来源:亿速云 阅读:144 作者:小新 栏目:开发技术

这篇文章主要介绍unity如何实现延迟回调工具,文中介绍的非常详细,具有一定的参考价值,感兴趣的小伙伴们一定要看完!

一个实用的计时器,可以计时延迟调用和延迟重复次数调用。

可以自己封装成单例模式挂在GameObject上使用,或者在另一个behavior的Update里执行这个类的OnUpdate()方法再使用。

为了更加安全的使用,建议在销毁MonoBehaviour时清理一下对应的所有计时器。

或者调用时可选择传入回调所在的MonoBehaviour,这样就可以自动清理了。

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

public static class DelayCall
{

    private static List<CallTimeObj> calltimes = new List<CallTimeObj>();
    private static Dictionary<int, CallObj> callsort = new Dictionary<int, CallObj>();
    private static int countid = 0;
    /// <summary>
    /// 生成id
    /// </summary>
    /// <returns>The new identifier.</returns>
    /// <param name="call">Call.</param>
    private static int getNewId(CallObj call)
    {
        countid++;
        if (countid >= int.MaxValue)
        {
            countid = 1;
        }
        while (callsort.ContainsKey(countid)) countid++;
        call.callid = countid;
        callsort.Add(countid, call);
        return countid;

    }
    public static void ClearAll()
    {

        calltimes.Clear();
        callsort.Clear();
    }
    /// <summary>
    /// 删除延迟执行.
    /// </summary>
    /// <param name='call'>
    /// Call.
    /// </param>
    public static void remove(int callid)
    {
        if (callid > 0 && callsort.ContainsKey(callid))
        {
            CallObj call = callsort[callid];
            callsort.Remove(callid);
            if (call != null)
            {
                calltimes.Remove((CallTimeObj)call);
            }
        }

    }

    public static int AddTime(float delayTime, object arg, int repeat = 1,Action<object> call)
    {
        var callobj = new CallTimeObj();
        callobj.argsCall = call;
        callobj.arg = arg;
        callobj.repeat = repeat;
        callobj.time = Time.realtimeSinceStartup + delayTime;
        callobj.delayTime = delayTime;
        if (repeat == 0)
        {
            callobj.isloop = true;
        }
        calltimes.Add(callobj);
        getNewId(callobj);
        return callobj.callid;

    }

    /// <summary>
    /// 添加延迟执行
    /// </summary>
    /// <param name="call">回调方法</param>
    /// <param name="delayTime">延迟时间</param>
    /// <param name="repeat">重复回调次数</param>
    /// <param name="mn">承载回掉函数的实例是否存在的判断</param>
    /// <param name="isUnique">是否是唯一的方法</param>
    /// <param name="isReplace">如果重复是否覆盖</param>
    /// <returns></returns>
    public static int AddTime(float delayTime, int repeat = 1, MonoBehaviour mn = null, bool isUnique = false, bool isReplace = false,Action call)
    {
        if (isUnique)
        {
            for (int i = 0; i < calltimes.Count; i++)
            {
                CallTimeObj call2 = calltimes[i];
                if (call2.mn == mn && call2.call == call)
                {
                    if (isReplace)
                    {
                        call2.time = Time.realtimeSinceStartup + delayTime;

                    }
                    return call2.callid;
                }
            }
        }

        var callobj = new CallTimeObj();
        callobj.call = call;
        callobj.isMN = (mn != null);
        callobj.mn = mn;
        callobj.repeat = repeat;
        callobj.time = Time.realtimeSinceStartup + delayTime;
        callobj.delayTime = delayTime;
        if (repeat == 0)
        {
            callobj.isloop = true;
        }
        calltimes.Add(callobj);
        getNewId(callobj);
        return callobj.callid;
    }


    public static void OnUpdate()
    {

        //time call
        if (calltimes.Count != 0) for (int i = 0; i < calltimes.Count; ++i)
            {
                CallTimeObj call = calltimes[i];
                if (call.time <= Time.realtimeSinceStartup)
                {
                    if (call.isloop == false)
                    {
                        call.repeat--;
                        if (call.repeat <= 0)
                        {
                            calltimes.RemoveAt(i);
                            callsort.Remove(call.callid);
                            --i;
                        }
                        else
                        {
                            //重新累加时间
                            call.time += call.delayTime;

                        }
                    }
                    else
                    {
                        call.time += call.delayTime;
                    }

                    if (!call.isMN || call.mn != null)
                    {
                        try
                        {
                            if (call.argsCall != null)
                            {
                                call.argsCall.Invoke(call.arg);
                                if (call.isloop == false)
                                {
                                    if (call.repeat <= 0)
                                    {
                                        call.arg = null;
                                        call.argsCall = null;
                                        call.mn = null;
                                    }
                                }
                            }
                            else
                            {
                                call.call();
                            }
                        }
                        catch (Exception e)
                        {
                            Debug.LogException(e);
                        }
                    }
                }
            }

    }
    private class CallObj
    {
        public Action call = null;
        public int frame;
        public bool isMN;
        public MonoBehaviour mn;
        public int callid = 0;
    }
    private class CallTimeObj : CallObj
    {
        public Action<object> argsCall = null;
        public float time;
        public int repeat = 1;
        public float delayTime = 0;
        public object arg;
        public bool isloop = false;
    }
}

以上是“unity如何实现延迟回调工具”这篇文章的所有内容,感谢各位的阅读!希望分享的内容对大家有帮助,更多相关知识,欢迎关注亿速云行业资讯频道!

向AI问一下细节

免责声明:本站发布的内容(图片、视频和文字)以原创、转载和分享为主,文章观点不代表本网站立场,如果涉及侵权请联系站长邮箱:is@yisu.com进行举报,并提供相关证据,一经查实,将立刻删除涉嫌侵权内容。

AI