Unity中的定时器与延时器

时间:2022-04-04 23:29:23

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() + "\t" + Time.time);
}
else
{
// 延时超过 2000 毫秒

}
}
}
}

 

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