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;
        }
    }
}
相关推荐
叫致寒吧6 小时前
Tomcat详解
java·tomcat
一线灵9 小时前
跨平台游戏引擎 Axmol-2.10.0 发布
游戏引擎
S***267510 小时前
基于SpringBoot和Leaflet的行政区划地图掩膜效果实战
java·spring boot·后端
马剑威(威哥爱编程)10 小时前
鸿蒙6开发视频播放器的屏幕方向适配问题
java·音视频·harmonyos
JIngJaneIL10 小时前
社区互助|社区交易|基于springboot+vue的社区互助交易系统(源码+数据库+文档)
java·数据库·vue.js·spring boot·论文·毕设·社区互助
V***u45310 小时前
MS SQL Server partition by 函数实战二 编排考场人员
java·服务器·开发语言
这是程序猿11 小时前
基于java的ssm框架旅游在线平台
java·开发语言·spring boot·spring·旅游·旅游在线平台
i***t91911 小时前
基于SpringBoot和PostGIS的云南与缅甸的千里边境线实战
java·spring boot·spring
k***082911 小时前
【监控】spring actuator源码速读
java·spring boot·spring
麦麦鸡腿堡11 小时前
Java_网络编程_InetAddress类与Socket类
java·服务器·网络