- Home /
 
How to make a list of Actions or methods?
Hello just thought of a way to refine my GUI system usauly i have a list of ints and a switch statement mapping each int to a method for an interface. However i thought it would be simpler to just have a list of the methods if possible and invoke them. I looked up on google how to make a list of methods and there isnt much besides using something like this (see code) and that doesn't work. I think it might need to be a delegate but i don't really understand how they work yet. Any help would be appreciated thanks.
 //list of actions(methods)??
 List<Action> CurrentMenus = new List<Action>();
 
 //no idea what () => dose just put in my method
 void Start() {
 CurrentMenus.Add(new Action(() => {GWelcome();}));
 }
 
 void OnGUI() {
 foreach (Action a in CurrentMenus) {
             a.Invoke();    
         }
 }
 
              
               Comment
              
 
               
              Answer by hvilela · Oct 26, 2012 at 11:46 PM
I guess your just missing the GUI itself. Maybe something like this:
 void OnGUI() {
     int i = 0;
     foreach (Action a in CurrentMenus) {
        if (GUILayout.Button ("Action " + ++i)) {
          a.Invoke();  
        }
     }
 }
 
              Your answer