Unity中使用扩展方法解决foreach导致的GC

具体原因不详述了,可以见这里。http://blog.csdn.net/zhou8jie/article/details/50324475
对于List这种顺序表,我们解决的时候还是可以使用for代替foreach即可。但是对于非顺序表,比如Dictionary或者Set之类,我们可以扩展方法Foreach,ForeachKey和ForeachValue来代替原有的foreach。
关于扩展方法,可参考:https://msdn.microsoft.com/zh-cn/library/bb383977.aspx

static class DictionaryEx
{
    /// <summary>
    /// 提供一个方法遍历所有项
    /// </summary>
    public static void Foreach<TKey, TValue>(this Dictionary<TKey, TValue> dic, Action<TKey, TValue> action, int maxCount = 1000)
    {
        if (action == null) return;
        var enumerator = dic.GetEnumerator();
        int i = 0;
        while (enumerator.MoveNext() && i++ < maxCount)
        {
            action(enumerator.Current.Key, enumerator.Current.Value);
        }
    }

    /// <summary>
    /// 提供一个方法遍历所有key值
    /// </summary>
    public static void ForeachKey<TKey, TValue>(this Dictionary<TKey, TValue> dic, Action<TKey> action, int maxCount = 1000)
    {
        if (action == null) return;
        var enumerator = dic.GetEnumerator();
        int i = 0;
        while (enumerator.MoveNext() && i++ < maxCount)
        {
            action(enumerator.Current.Key);
        }
    }

    /// <summary>
    /// 提供一个方法遍历所有value值
    /// </summary>
    public static void ForeachValue<TKey, TValue>(this Dictionary<TKey, TValue> dic, Action<TValue> action, int maxCount = 1000)
    {
        if (action == null) return;
        var enumerator = dic.GetEnumerator();
        int i = 0;
        while (enumerator.MoveNext() && i++ < maxCount)
        {
            action(enumerator.Current.Value);
        }
    }
}

在调用时,我们可以使用Lambda表达式简化代码,如:

protected Dictionary<int, ReleaseOnceSkillBunchListShell> m_SkillBunchList = new Dictionary<int, ReleaseOnceSkillBunchListShell>(); // 一个技能列表
    /////////////
    public void Update(float deltaTime)
    {
        // 更新技能列表
        m_SkillBunchList.ForeachValue(
            (value) =>
            {
                value.Update(deltaTime);
            });
    }

本以为这样就解决问题了,结果打开profiler一看,妈蛋,竟然还有GC,仔细对比后发现,原来是lambda表达式的问题。没办法,之后把lambda表达式拆开了,拆完如下。

    protected float m_deltaTime;
    public void Update(float deltaTime)
    {
        m_deltaTime = deltaTime;
        m_SkillBunchList.ForeachValue(OnUpdateSkillBunch);
    }
    public void OnUpdateSkillBunch(ReleaseOnceSkillBunchListShell shell)
    {
        shell.Update(m_deltaTime);
    }

拆完不爽的地方就是,原来作用域里面的deltaTime,需要放到更高层的作用域里,否则不好访问。
所以,小心了,lambda表达式也会产生GC!

  • 0
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 3
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 3
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值