using System.Collections.Generic; using UnityEngine; /// /// WaitForSeconds缓存,避免每次yield时分配GC /// 使用方式:yield return WaitForSecondsCache.Get(1f); /// public static class WaitForSecondsCache { private static readonly Dictionary _cache = new Dictionary(); public static WaitForSeconds Get(float seconds) { if (!_cache.TryGetValue(seconds, out var wait)) { wait = new WaitForSeconds(seconds); _cache[seconds] = wait; } return wait; } // 预缓存常用值 public static readonly WaitForSeconds Wait0_1 = new WaitForSeconds(0.1f); public static readonly WaitForSeconds Wait0_5 = new WaitForSeconds(0.5f); public static readonly WaitForSeconds Wait1 = new WaitForSeconds(1f); public static readonly WaitForSeconds Wait2 = new WaitForSeconds(2f); public static readonly WaitForSeconds Wait5 = new WaitForSeconds(5f); }