最新国产好看的视频,伊人天堂AV在线,国产Aaaaaa视频,蜜臀视频在线观看一区,人妻av色图,密臀久久久精品影片,青青视频免费观看毛片,久草在线观看视,国产三级精品色情在线

unity實(shí)現(xiàn)延遲回調(diào)工具

 更新時(shí)間:2021年09月26日 10:10:39   作者:騷年狠沖洞  
這篇文章主要為大家詳細(xì)介紹了unity實(shí)現(xiàn)延遲回調(diào)工具,文中示例代碼介紹的非常詳細(xì),具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下

一個(gè)實(shí)用的計(jì)時(shí)器,可以計(jì)時(shí)延遲調(diào)用和延遲重復(fù)次數(shù)調(diào)用。

可以自己封裝成單例模式掛在GameObject上使用,或者在另一個(gè)behavior的Update里執(zhí)行這個(gè)類的OnUpdate()方法再使用。

為了更加安全的使用,建議在銷毀MonoBehaviour時(shí)清理一下對(duì)應(yīng)的所有計(jì)時(shí)器。

或者調(diào)用時(shí)可選擇傳入回調(diào)所在的MonoBehaviour,這樣就可以自動(dòng)清理了。

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>
    /// 刪除延遲執(zhí)行.
    /// </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>
    /// 添加延遲執(zhí)行
    /// </summary>
    /// <param name="call">回調(diào)方法</param>
    /// <param name="delayTime">延遲時(shí)間</param>
    /// <param name="repeat">重復(fù)回調(diào)次數(shù)</param>
    /// <param name="mn">承載回掉函數(shù)的實(shí)例是否存在的判斷</param>
    /// <param name="isUnique">是否是唯一的方法</param>
    /// <param name="isReplace">如果重復(fù)是否覆蓋</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
                        {
                            //重新累加時(shí)間
                            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;
    }
}

以上就是本文的全部?jī)?nèi)容,希望對(duì)大家的學(xué)習(xí)有所幫助,也希望大家多多支持腳本之家。

相關(guān)文章

  • C# 打印網(wǎng)頁(yè)不顯示頁(yè)眉頁(yè)腳的實(shí)現(xiàn)方法

    C# 打印網(wǎng)頁(yè)不顯示頁(yè)眉頁(yè)腳的實(shí)現(xiàn)方法

    這篇文章主要介紹了C# 打印網(wǎng)頁(yè)不顯示頁(yè)眉頁(yè)腳的實(shí)現(xiàn)方法,文中通過(guò)示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來(lái)一起學(xué)習(xí)學(xué)習(xí)吧
    2020-01-01
  • 詳解c# 多態(tài)

    詳解c# 多態(tài)

    這篇文章主要介紹了c# 多態(tài)的相關(guān)資料,文中講解非常細(xì)致,代碼幫助大家更好的理解和學(xué)習(xí),感興趣的朋友可以了解下
    2020-07-07
  • C#使用GZipStream解壓縮數(shù)據(jù)文件的方法

    C#使用GZipStream解壓縮數(shù)據(jù)文件的方法

    這篇文章主要介紹了C#使用GZipStream解壓縮數(shù)據(jù)文件的方法,實(shí)例分析了C#中GZipStream方法的原理與使用技巧,需要的朋友可以參考下
    2015-04-04
  • C#/VB.NET實(shí)現(xiàn)從PPT中提取圖片的示例代碼

    C#/VB.NET實(shí)現(xiàn)從PPT中提取圖片的示例代碼

    PPT是用于制作幻燈片(演示文稿)的應(yīng)用軟件,每張幻燈片中都可以包含文字、圖形、圖形、表格、聲音和影像等多種信息。本文主要介紹了如何實(shí)現(xiàn)從PPT中提取圖片的功能,需要的可以參考一下
    2023-03-03
  • c# AES字節(jié)數(shù)組加密解密流程及代碼實(shí)現(xiàn)

    c# AES字節(jié)數(shù)組加密解密流程及代碼實(shí)現(xiàn)

    這篇文章主要介紹了c# AES字節(jié)數(shù)組加密解密流程及代碼實(shí)現(xiàn),幫助大家更好的理解和使用c#,感興趣的朋友可以了解下
    2020-11-11
  • C#實(shí)現(xiàn)全局快捷鍵功能

    C#實(shí)現(xiàn)全局快捷鍵功能

    這篇文章介紹了C#實(shí)現(xiàn)全局快捷鍵功能的方法,文中通過(guò)示例代碼介紹的非常詳細(xì)。對(duì)大家的學(xué)習(xí)或工作具有一定的參考借鑒價(jià)值,需要的朋友可以參考下
    2022-06-06
  • C# 繪制實(shí)時(shí)折線圖,波形圖

    C# 繪制實(shí)時(shí)折線圖,波形圖

    這篇文章主要介紹了C# 繪制實(shí)時(shí)折線圖,波形圖的方法,文中示例代碼非常詳細(xì),幫助大家更好的理解和學(xué)習(xí),感興趣的朋友可以了解下
    2020-07-07
  • C# 開(kāi)發(fā)圓角控件(窗體)的具體實(shí)現(xiàn)

    C# 開(kāi)發(fā)圓角控件(窗體)的具體實(shí)現(xiàn)

    這篇文章主要介紹了C# 開(kāi)發(fā)圓角控件的具體實(shí)現(xiàn),需要的朋友可以參考下
    2014-02-02
  • C#之lock的使用及說(shuō)明

    C#之lock的使用及說(shuō)明

    這篇文章主要介紹了C#之lock的使用及說(shuō)明,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。如有錯(cuò)誤或未考慮完全的地方,望不吝賜教
    2023-06-06
  • C#實(shí)現(xiàn)3D效果完整實(shí)例

    C#實(shí)現(xiàn)3D效果完整實(shí)例

    這篇文章主要介紹了C#實(shí)現(xiàn)3D效果的方法,結(jié)合完整實(shí)例形式分析了C#實(shí)現(xiàn)文字3D顯示效果的具體步驟與相關(guān)操作技巧,需要的朋友可以參考下
    2016-08-08

最新評(píng)論

鹤峰县| 图木舒克市| 子洲县| 达拉特旗| 辽阳市| 酉阳| 江油市| 综艺| 潼南县| 泾川县| 县级市| 青田县| 鲁甸县| 新巴尔虎右旗| 通州市| 北票市| 商丘市| 伊川县| 达尔| 五寨县| 乐平市| 邵阳县| 高安市| 龙山县| 东至县| 广灵县| 精河县| 祁东县| 洞头县| 科技| 沙雅县| 汤阴县| 泉州市| 天津市| 金平| 佛冈县| 额尔古纳市| 垣曲县| 玉环县| 辉县市| 乐至县|