Created
July 11, 2012 22:52
-
-
Save jakevsrobots/3094301 to your computer and use it in GitHub Desktop.
yield/delegate/loop closure issue
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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