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;
        }
    }
}
相关推荐
怒放吧德德34 分钟前
Netty 4.2 入门指南:从概念到第一个程序
java·后端·netty
雨中飘荡的记忆2 小时前
大流量下库存扣减的数据库瓶颈:Redis分片缓存解决方案
java·redis·后端
心之语歌5 小时前
基于注解+拦截器的API动态路由实现方案
java·后端
华仔啊6 小时前
Stream 代码越写越难看?JDFrame 让 Java 逻辑回归优雅
java·后端
ray_liang6 小时前
用六边形架构与整洁架构对比是伪命题?
java·架构
Ray Liang7 小时前
用六边形架构与整洁架构对比是伪命题?
java·python·c#·架构设计
Java水解7 小时前
Java 中间件:Dubbo 服务降级(Mock 机制)
java·后端
SimonKing12 小时前
OpenCode AI辅助编程,不一样的编程思路,不写一行代码
java·后端·程序员
FastBean12 小时前
Jackson View Extension Spring Boot Starter
java·后端
Seven9713 小时前
剑指offer-79、最⻓不含重复字符的⼦字符串
java