Unity 协程GC优化记录

第一种 缓存

csharp 复制代码
public static class WaitCache
{
    private static readonly Dictionary<float, WaitForSeconds> _cache = new();
    
    public static WaitForSeconds Get(float seconds)
    {
        if (!_cache.TryGetValue(seconds, out var wait))
        {
            wait = new WaitForSeconds(seconds);
            _cache[seconds] = wait;
        }
        return wait;
    }
}

第二种 零GC优化版

csharp 复制代码
using System.Collections;
using UnityEngine;

namespace GameScripts.Common
{
    /// <summary>
    /// 协程等待工具 - 零GC优化版
    /// </summary>
    public static class CoroutineUtility
    {
        // ==================== 工厂方法 ====================
        public static WaitTime Wait(float seconds) => new WaitTime(seconds);
        public static WaitRealtime WaitReal(float seconds) => new WaitRealtime(seconds);
        public static WaitFrames Frames(int count) => new WaitFrames(count);

        // ==================== 基于 Time.time(受TimeScale影响)====================
        public readonly struct WaitTime : IEnumerator
        {
            private readonly float _endTime;

            public WaitTime(float seconds)
            {
                _endTime = Time.time + seconds;
            }

            public bool MoveNext() => Time.time < _endTime;
            public void Reset() { }
            public object Current => null;
        }

        // ==================== 基于真实时间(不受TimeScale影响)====================
        public readonly struct WaitRealtime : IEnumerator
        {
            private readonly float _endTime;

            public WaitRealtime(float seconds)
            {
                _endTime = Time.realtimeSinceStartup + seconds;
            }

            public bool MoveNext() => Time.realtimeSinceStartup < _endTime;
            public void Reset() { }
            public object Current => null;
        }

        // ==================== 按帧等待(修复版)====================
        public readonly struct WaitFrames : IEnumerator
        {
            private readonly int _targetFrame;

            public WaitFrames(int frames)
            {
                _targetFrame = Time.frameCount + frames;
            }

            public bool MoveNext() => Time.frameCount < _targetFrame;
            public void Reset() { }
            public object Current => null;
        }

        // ==================== 条件等待(注意:lambda 有 GC)====================
        public readonly struct WaitUntil : IEnumerator
        {
            private readonly System.Func<bool> _predicate;

            public WaitUntil(System.Func<bool> predicate)
            {
                _predicate = predicate;
            }

            public bool MoveNext() => !_predicate();
            public void Reset() { }
            public object Current => null;
        }

        public readonly struct WaitWhile : IEnumerator
        {
            private readonly System.Func<bool> _predicate;

            public WaitWhile(System.Func<bool> predicate)
            {
                _predicate = predicate;
            }

            public bool MoveNext() => _predicate();
            public void Reset() { }
            public object Current => null;
        }
    }
}
相关推荐
超级大只老咪21 小时前
数组相邻元素比较的循环条件(Java竞赛考点)
java
小浣熊熊熊熊熊熊熊丶21 小时前
《Effective Java》第25条:限制源文件为单个顶级类
java·开发语言·effective java
毕设源码-钟学长21 小时前
【开题答辩全过程】以 公交管理系统为例,包含答辩的问题和答案
java·eclipse
啃火龙果的兔子21 小时前
JDK 安装配置
java·开发语言
星哥说事21 小时前
应用程序监控:Java 与 Web 应用的实践
java·开发语言
派大鑫wink21 小时前
【JAVA学习日志】SpringBoot 参数配置:从基础到实战,解锁灵活配置新姿势
java·spring boot·后端
xUxIAOrUIII1 天前
【Spring Boot】控制器Controller方法
java·spring boot·后端
Dolphin_Home1 天前
从理论到实战:图结构在仓库关联业务中的落地(小白→中级,附完整代码)
java·spring boot·后端·spring cloud·database·广度优先·图搜索算法
醇氧1 天前
org.jetbrains.annotations的@Nullable 学习
java·开发语言·学习·intellij-idea
Java&Develop1 天前
Aes加密 GCM java
java·开发语言·python