【unity框架开发4】封装unity协程工具,避免 GC(垃圾回收)

发布于:2024-10-12 ⋅ 阅读:(7) ⋅ 点赞:(0)

前言

在 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>
    /// 获取等待结束帧的对象
    /// </summary>
    public static WaitForEndOfFrame WaitForEndOfFrame()
    {
        return waitForEndOfFrame;
    }

    /// <summary>
    /// 获取等待固定更新的对象
    /// </summary>
    public static WaitForFixedUpdate WaitForFixedUpdate()
    {
        return waitForFixedUpdate;
    }

    /// <summary>
    /// 等待指定时间(以秒为单位)
    /// </summary>
    /// <param name="time">等待的时间</param>
    public static IEnumerator WaitForSeconds(float time)
    {
        float currTime = 0;
        while (currTime < time)
        {
            currTime += Time.deltaTime;
            yield return new WaitForFrameStruct(); // 等待一帧
        }
    }

    /// <summary>
    /// 等待指定的实时时间(不受时间缩放影响)
    /// </summary>
    /// <param name="time">等待的时间</param>
    public static IEnumerator WaitForSecondsRealtime(float time)
    {
        float currTime = 0; // 当前经过的时间
        while (currTime < time) // 当经过的时间小于指定时间时
        {
            currTime += Time.unscaledDeltaTime; // 增加经过的时间(不受时间缩放影响)
            yield return new WaitForFrameStruct(); // 等待一帧
        }
    }

    /// <summary>
    /// 等待指定帧数
    /// </summary>
    /// <param name="count">等待的帧数,默认为1</param>
    public 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

一位在小公司默默奋斗的开发者,闲暇之余,边学习边记录分享,站在巨人的肩膀上,通过学习前辈们的经验总是会给我很多帮助和启发!如果你遇到任何问题,也欢迎你评论私信或者加群找我, 虽然有些问题我也不一定会,但是我会查阅各方资料,争取给出最好的建议,希望可以帮助更多想学编程的人,共勉~
在这里插入图片描述


网站公告

今日签到

点亮在社区的每一天
去签到