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;
        }
    }
}
相关推荐
我要去腾讯2 小时前
Springcloud核心组件之Sentinel详解
java·spring cloud·sentinel
czhc11400756632 小时前
Java117 最长公共前缀
java·数据结构·算法
weixin_307779133 小时前
AWS Elastic Beanstalk 实现 Java 应用高可用部署指南
java·开发语言·云计算·aws·web app
黄思搏3 小时前
Unity SpriteRenderer 进度条 Shader 实现
unity·游戏引擎
萝卜白菜。3 小时前
关于Java EE应用中xml解析类的问题
xml·java·java-ee
一米阳光zw3 小时前
Spring Boot中使用 MDC实现请求TraceId全链路透传
java·spring boot·后端·traceid·mdc
王元_SmallA3 小时前
pgsql:connection failed connection to server at
java·后端
高山上有一只小老虎3 小时前
购物消费打折
java·算法
tuokuac3 小时前
@Configuration类中定义的@Bean方法
java