http://stackoverflow.com/questions/235455/access-to-modified-closure
1.使用dotween处理事件的时候如果写了类似代码
for (int i = 0; i < MazeController.Instance.CurrentData.StarNum; i++) { //var index = i; starPositon[i].DOLocalMove(new Vector3(630f, 390f, 0), 1f).From().SetDelay(0.3f*(i+1)).OnComplete( delegate { this.GlowSofts[i].SetActive(true); this.StarFulls[i].SetActive(true); }); }
会提示Access to Modified Closure 然后设置active的所有都会是固定的最后一个值甚至报出异常。同样的问题也可能出现在LINQ的使用中。
http://www.cnblogs.com/wintersun/archive/2010/06/15/1758628.html
2.记录i值,不去访问闭包即可
for (int i = 0; i < MazeController.Instance.CurrentData.StarNum; i++) { var index = i; starPositon[i].DOLocalMove(new Vector3(630f, 390f, 0), 1f).From().SetDelay(0.3f*(i+1)).OnComplete( delegate { this.GlowSofts[index].SetActive(true); this.StarFulls[index].SetActive(true); }); }
网友评论