public string log;
Coroutine async;
float threshold=5f; //时间阈值,小于该时间,则只执行后一次
void Update()
{
if (Input.GetMouseButtonDown(0))
{
if (async != null)
{
Debug.Log("停下上一个: " + log);
StopCoroutine(async);// 如果在指定时间内,有新的操作,取消上一个协程
}
log = "www" + Random.Range(0, 100);
async = StartCoroutine(DelayedAction(log));
}
}
IEnumerator DelayedAction(string content)
{
yield return new WaitForSeconds(threshold);
Debug.Log("success >> " + content);
async = null;
}
unity协程,频繁调用时,只执行最后一次.
最新推荐文章于 2024-03-16 21:43:29 发布