Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
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
You can’t perform that action at this time.