zoukankan      html  css  js  c++  java
  • Unity中的定时器与延时器

    JavaScript中的定时器与延时器,分别是 setInterval、setTimeout,对应的清理函数是:clearInterval、clearTimeout。

    而在Unity中,则分别是:Invoke、InvokeRepeating和取消延迟调用  CancelInvoke

    延迟调用

    void Invoke(string methodName, float time);

    重复延迟调用

    void InvokeRepeating(string methodName, float time, float repeatRate);

    在 time 后调用 methodName 方法然后每 repeatRate 秒 重复调用。

    上面二个方法有什么用处呢?我想到的应用场景就是,在游戏主界面显示当前游戏的延迟 —— 几乎成为游戏的标配了。

    为了设计不至于太过复杂,我直接采用了定时器不断的Ping,每隔几秒就Ping一次,并给定每一次 Ping 一个超时上限。

    using System.Collections;
    using System.Collections.Generic;
    using UnityEngine;
    
    
    public class PingTool : MonoBehaviour
    {
        // 最大尝试ping的次数
        private static int nMaxTryNum = 10;
    
        // 检测频率
        private static float nCheckInterval = 3f;
    
        // 需要 ping 的 IP
        private static string strRemoteIP = "";
        
        private static PingTool pingTool;
                
        public static void CreatePing(string strIP)
        {
            if (string.IsNullOrEmpty(strIP)) return;
    
            if (pingTool != null)
            {
                Debug.Log("Please Stop Ping Before.");
                return;
            }
    
            strRemoteIP = strIP;
    
            // 复用组件,避免频繁的创建和销毁组件
            GameObject go = GameObject.Find("PingTool");
            if (go == null)
            {
                go = new GameObject("PingTool");
                DontDestroyOnLoad(go);
            }
            
            pingTool = go.AddComponent<PingTool>();
        }
    
        public static void StopPing()
        {
            if (pingTool != null)
            {
                pingTool.CancelInvoke();
                Destroy(pingTool);
            }            
        }
    
        public static void SetCheckInterval(float value)
        {
            nCheckInterval = value;
        }
        
    
        private void Start()
        {  
            InvokeRepeating("Execute", 0, nCheckInterval);
        }
    
        private void Execute()
        {
            if (pingTool == null) return;
    
            StartCoroutine(PingConnect());
        }
    
        private void Destroy()
        {
            strRemoteIP = "";
            nCheckInterval = 1.0f;
    
            pingTool = null;
        }
        
        private IEnumerator PingConnect()
        {
            if (pingTool != null)
            {
                Ping ping = new Ping(strRemoteIP);
    
                int nTryNum = 0;
                while (!ping.isDone)
                {
                    yield return new WaitForSeconds(0.2f);
    
                    // Ping Fail
                    if (nTryNum++ > nMaxTryNum)
                    {
                        yield break;
                    }
                }
    
                if (ping.isDone)
                {
                    int nDelayTime = ping.time;
    
                    Debug.Log("nDelayTime : " + nDelayTime.ToString() + "	" + Time.time);
                }
                else
                {
                    // 延时超过 2000 毫秒
    
                }
            }            
        }
    }

    上面的例子每0.2秒检测一次 Ping 是否结束,最多十次检测,也就是2秒超时的时长。每3秒再检测一次,上面示例代码可以再添加一个 callback

  • 相关阅读:
    LightOJ 1422 Halloween Costumes(区间dp)
    zoj 3537 Cake(区间dp)
    POJ 2955 Brackets(区间dp)
    HDU 1058 Humble Numbers(dp)
    uva 10934 Dropping water balloons(转载)
    树形dp
    Manacher算法求回文半径
    poj-1236.network of schools(强连通分量 + 图的入度出度)
    hdu-2255.奔小康赚大钱(最大权二分匹配)
    poj-2289.jamies contact groups(二分答案 + 二分多重匹配)
  • 原文地址:https://www.cnblogs.com/meteoric_cry/p/7771969.html
Copyright © 2011-2022 走看看