Skip to content

Instantly share code, notes, and snippets.

@Marneus68
Created November 6, 2015 20:47
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save Marneus68/0d3b4ad86414dce9fb21 to your computer and use it in GitHub Desktop.
Save Marneus68/0d3b4ad86414dce9fb21 to your computer and use it in GitHub Desktop.
using UnityEngine;
using System.Collections.Generic;
public class ClosureTest : MonoBehaviour {
public delegate void TestAction();
void Start () {
Dictionary<string, List<string>> dict = new Dictionary<string, List<string>>() {
{ "Foo" , new List<string>() { "Goo", "Hoo", "Ioo" } },
{ "Bar" , new List<string>() { "Car", "Dar", "Ear" } }
};
Dictionary<string, TestAction> acts = new Dictionary<string, TestAction>();
foreach (var l in dict) {
// Adding action with the help of a lambda expression
acts.Add(l.Key, () => {
foreach (var s in l.Value) {
Debug.Log("Submenu : " + s );
}
});
}
foreach (var l in acts) {
Debug.Log("Menu name : " + l.Key);
l.Value();
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment