构成
CoroutineTool.cs
有几个公开的静态方法,可供调用:
WaitForFixedUpdate()
WaitForSeconds(float time)等待几秒
WaitForSecondsRealtime(float time)等待几秒,但是不受 timeScale的影响
WaitForFrames(int count=1) 等待几帧
作用
主要是对协程的等待做了一层封装,较少GC,方便调用。
源码
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
namespace JKFrame
{
/// <summary>
///
/// </summary>
public static class CoroutineTool
{
private static WaitForEndOfFrame waitForEndOfFrame = new WaitForEndOfFrame();
private static WaitForFixedUpdate waitForFixedUpdate = new WaitForFixedUpdate();
private static YieldInstruction yieldInstruction = new YieldInstruction();
public static WaitForEndOfFrame WaitForEndOfFrame()
{
return waitForEndOfFrame;
}
public static WaitForFixedUpdate WaitForFixedUpdate()
{
return waitForFixedUpdate;
}
public static IEnumerator WaitForSeconds(float time)
{
float currTime = 0;
while (currTime < time)
{
currTime += Time.deltaTime;
yield return yieldInstruction;
}
}
public static IEnumerator WaitForSecondsRealtime(float time)
{
float currTime = 0;
while (currTime < time)
{
currTime += Time.unscaledDeltaTime;
yield return yieldInstruction;
}
}
public static IEnumerator WaitForFrames(int count=1)
{
for (int i = 0; i < count; i++)
{
yield return yieldInstruction;
}
}
}
}