东莞专业设计网站,seo基础入门,typecho to wordpress,如何做盆栽蔬菜网站文章目录 前言封装协程工具类#xff0c;避免 GC#xff08;垃圾回收#xff09;使用1.使用默认方式使用协程2.使用自定义的 CoroutineTool 工具类来等待不同的时间 完结 前言
在 Unity 中#xff0c;使用 yield return null 、yield return new WaitForEndOfFrame()等会导… 文章目录 前言封装协程工具类避免 GC垃圾回收使用1.使用默认方式使用协程2.使用自定义的 CoroutineTool 工具类来等待不同的时间 完结 前言
在 Unity 中使用 yield return null 、yield return new WaitForEndOfFrame()等会导致 GC垃圾回收开销。 yield return null: 每次调用这个语句Unity 会创建一个新的迭代器状态机。当你执行协程时如果你在协程中使用 yield return null它会生成一个新的迭代器这样会产生额外的内存分配。 new WaitForEndOfFrame(): 每次使用 new WaitForEndOfFrame() 都会创建一个新的 WaitForEndOfFrame 对象。这种频繁的对象创建会增加内存开销并可能导致 GC 的触发。
但是注意启动协程时会创建一个 Coroutine 对象这本身会导致一次内存分配进而可能引发垃圾回收GC。这是协程在 Unity 中的一个固有特性无法完全避免。但是它还是解决了协程的主要痛点毕竟一个项目启动的协程一般不会很多。如果你在意可以选择使用 UniTask 【推荐100个unity插件之33】比 Unity 自带协程更高效的异步处理方式提供一个高性能和0GC的async/await异步方案——UniTask插件
封装协程工具类避免 GC垃圾回收
提前new好协程所需要的WaitForEndOfFrame、WaitForFixedUpdate、WaitForFrameStruct类的对象,避免GC。
/// summary
/// 协程工具类避免 GC垃圾回收
/// /summary
public static class CoroutineTool
{// 定义一个结构体用于表示等待一帧的状态private struct WaitForFrameStruct : IEnumerator{public object Current null;public bool MoveNext() { return false; } // 一旦调用立即返回 false停止迭代public void Reset() { } // 重置方法不做任何操作}// 预定义的等待结束帧对象避免多次创建private static WaitForEndOfFrame waitForEndOfFrame new WaitForEndOfFrame();// 预定义的等待固定更新对象避免多次创建private static WaitForFixedUpdate waitForFixedUpdate new WaitForFixedUpdate();/// summary/// 获取等待结束帧的对象/// /summarypublic static WaitForEndOfFrame WaitForEndOfFrame(){return waitForEndOfFrame;}/// summary/// 获取等待固定更新的对象/// /summarypublic static WaitForFixedUpdate WaitForFixedUpdate(){return waitForFixedUpdate;}/// summary/// 等待指定时间以秒为单位/// /summary/// param nametime等待的时间/parampublic static IEnumerator WaitForSeconds(float time){float currTime 0;while (currTime time){currTime Time.deltaTime;yield return new WaitForFrameStruct(); // 等待一帧}}/// summary/// 等待指定的实时时间不受时间缩放影响/// /summary/// param nametime等待的时间/parampublic static IEnumerator WaitForSecondsRealtime(float time){float currTime 0; // 当前经过的时间while (currTime time) // 当经过的时间小于指定时间时{currTime Time.unscaledDeltaTime; // 增加经过的时间不受时间缩放影响yield return new WaitForFrameStruct(); // 等待一帧}}/// summary/// 等待指定帧数/// /summary/// param namecount等待的帧数默认为1/parampublic static IEnumerator WaitForFrame(int count 1){for (int i 0; i count; i){yield return new WaitForFrameStruct(); // 等待一帧}}
}使用
1.使用默认方式使用协程
IEnumerator DelayedAction()
{yield return new WaitForEndOfFrame();// 等待到当前帧的结束即所有渲染操作完成后yield return new WaitForFixedUpdate();// 等待到下一个固定更新适用于物理计算yield return new WaitForSeconds(2.0f); // 等待2秒游戏时间yield return new WaitForSecondsRealtime(2.0f);// 等待2秒现实时间不受游戏时间缩放影响yield return null;// 等待一帧
}2.使用自定义的 CoroutineTool 工具类来等待不同的时间
IEnumerator DelayedAction()
{yield return CoroutineTool.WaitForEndOfFrame(); // 等待到当前帧的结束yield return CoroutineTool.WaitForFixedUpdate(); // 等待到下一个固定更新yield return CoroutineTool.WaitForSeconds(2.0f); // 等待2秒游戏时间yield return CoroutineTool.WaitForSecondsRealtime(2.0f); // 等待2秒现实时间不受游戏时间缩放影响yield return CoroutineTool.WaitForFrame(); // 等待一帧yield return CoroutineTool.WaitForFrame(3); // 等待3帧
}完结
赠人玫瑰手有余香如果文章内容对你有所帮助请不要吝啬你的点赞评论和关注你的每一次支持都是我不断创作的最大动力。当然如果你发现了文章中存在错误或者有更好的解决方法也欢迎评论私信告诉我哦
好了我是向宇https://xiangyu.blog.csdn.net
一位在小公司默默奋斗的开发者闲暇之余边学习边记录分享站在巨人的肩膀上通过学习前辈们的经验总是会给我很多帮助和启发如果你遇到任何问题也欢迎你评论私信或者加群找我 虽然有些问题我也不一定会但是我会查阅各方资料争取给出最好的建议希望可以帮助更多想学编程的人共勉~