Skip to content

Instantly share code, notes, and snippets.

@jakevsrobots
Created July 11, 2012 22:52
Show Gist options
  • Save jakevsrobots/3094301 to your computer and use it in GitHub Desktop.
Save jakevsrobots/3094301 to your computer and use it in GitHub Desktop.
yield/delegate/loop closure issue
using UnityEngine;
using System;
using System.Collections;
using System.Collections.Generic;
public class ClosureTest : MonoBehaviour {
void Start () {
List<int> sourceData = new List<int>();
for(int i=0; i < 10; i++) {
sourceData.Add(i);
}
Action<int> dataProcessor = delegate(int datum) {
Debug.Log(datum);
};
StartCoroutine(RunClosureTest(sourceData, dataProcessor));
}
private IEnumerator RunClosureTest(List<int> data, Action<int> dataProcessorAction) {
List<Action> todo = new List<Action>();
for(int i=0; i < data.Count; i++) {
todo.Add(delegate() {
dataProcessorAction(i);
});
yield return null;
}
foreach(Action action in todo) {
action();
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment