Unity中使用扩展方法解决foreach导致的GC
对于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!
Unity中使用扩展方法解决foreach导致的GC的更多相关文章
- Unity中自定义扩展方法
问题背景 在使用unity开发过程中,通常会遇到一种情况,比如说给物体重新赋值坐标的问题, Transfrom tran: ,pos_y=,pos_z=; tran.position=new Vect ...
- C#编程(六十一)------------LINQ中的扩展方法
原文链接: http://blog.csdn.net/shanyongxu/article/details/47208401 LINQ中的扩展方法 LINQ中where扩展方法,要想使用,必须导入us ...
- C#中的扩展方法(向已有类添加方法,但无需创建新的派生类型)
C#中的扩展方法 扩展方法使你能够向现有类型"添加"方法,而无需创建新的派生类型.重新编译或以其他方式修改原始类型. 扩展方法是一种特殊的静态方法,但可以像扩展类型上的实例方法一样 ...
- jQuery中效果animate方法解决width是百分比出现的问题
jQuery中效果animate方法解决width是百分比出现的问题 http://www.mafutian.net/131.html 问题描述: 效果如图,初始化,每个层宽20%,采用animate ...
- (转)再不用担心DataRow类型转换和空值了(使用扩展方法解决高频问题)
再不用担心DataRow类型转换和空值了(使用扩展方法解决高频问题) XML文档操作集锦(C#篇) webapi文档描述-swagger
- C#3.0中的扩展方法
在实际应用中,开发者完成代码的编译后,除非重新编译更改后的代码,否则开发者很难在原有代码中添加新的功能. 在C#3.0中,提供了一个扩展方法的新特性,可以使得开发者在编译后的程序集里边添加相关的方法, ...
- 记录C#中的扩展方法
C#中的扩展方法. 系统自带的类型,我们无法去修改: 修改源代码需要较大的精力,而且可能会带来错误: 我们只是需要一个或者较少的几个方法,修改源代码费时费力: 被扩展的类是sealed的,不能被继承: ...
- objective-C中的扩展方法与partial class
在c#中要扩展一个现有类非常easy,比方这样: ? 1 2 3 4 5 6 7 public static class Utils { public static void PrintTo ...
- C#中的扩展方法
扩展方法使你能够向现有类型“添加”方法,而无需创建新的派生类型.重新编译或以其他方式修改原始类型. 扩展方法是一种特殊的静态方法,但可以像扩展类型上的实例方法一样进行调用. 以上是msdn官网对扩展方 ...
随机推荐
- Nginx将通过IP访问重定向
server { listen 80 default_server; server_name localhost; location / { rewrite ^ http://www.xxx.com/ ...
- 常用的dos命名
注销:logoff 注册表:regedit 查看显存:dxdiag 删除文件: del +文件 删除文件夹: rd+(/s)+文件名 (/s表示删除文件下的子目录和文件); 系统配置实用程序: msc ...
- JfreeCHart 异常:Chart image not found
http://bbs.justep.com/thread-54775-1-1.html java.lang.IllegalArgumentException: Width (0) and height ...
- c# 动态产生控件 注册动态控件事件
用CheckEdit演示 其他控件类推 CheckEdit AllSele = new CheckEdit(); AllSele.Location = new System.Drawing.Point ...
- HDU 5487 Difference of Languages(BFS)
HDU 5487 Difference of Languages 这题从昨天下午2点开始做,到现在才AC了.感觉就是好多题都能想出来,就是写完后debug很长时间,才能AC,是不熟练的原因吗?但愿孰能 ...
- Android4.4KitKat支持u盘功能
Android4.4KitKat支持u盘功能 作者: 发布日期:2014-05-14 23:16:13 我来说两句(0) 0 Tag标签:功能 Android U 盘功能实现和分析 u 盘功能实 ...
- angularJs-UI-bootstrap系列教程1(使用前的准备)
之前一直想看看angular中Ui-bootstrap是如何使用的,但是苦于网站被墙了,一直看不到,最近偷偷的到墙外面看了一下文档,大致的了解了如何使用,在这里写这边文章主要就是为了那些被墙了的ang ...
- (H5)FormData+AJAX+SpringMVC跨域异步上传文件
最近都没时间整理资料了,一入职就要弄懂业务,整天被业务弄得血崩. 总结下今天弄了一个早上的跨域异步上传文件.主要用到技术有HTML5的FormData,AJAX,Spring MVC. 首先看下上传页 ...
- 移动web开发资源大整合
移动web开发资源大整合 http://www.cnblogs.com/PeunZhang/p/3407453.html
- iBATIS 3 试用手记 - The FUTURE - ITeye技术网站
body { font-family: "Microsoft YaHei UI","Microsoft YaHei",SimSun,"Segoe UI ...