Hola,
So, System.Action is a [delegate][1]. In my words, I’d say it is like declaring a variable data type for some type of function you want. Personally, I would avoid using System.Action because it seems a bit more vague and would instead create my own delegates. Alright, so here’s how you could use delegates:
// A declared delegate with two string params
public delegate void MyDelegate(string str1, string str2);
public List<MyDelegate> someList = new List<MyDelegate>();
void Start() {
// --- ADDING FUNCTIONS TO THE LIST
someList.Add(Function1); // (Add existing function)
someList.Add((string s1, string s2) => { // (Add inline function)
Debug.Log("Function2: " + s1 + " " + s2);
});
// --- CALLING THE FUNCTIONS IN THE LIST
for (int i = 0; i < someList.Count; i++) {
MyDelegate d = someList*;*
// You pass in the parameters HERE (only when you call the function itself)
d(“Hello”, “World”);
// Or you could do this:
// someList*(“Hello”, “World”);*
}
}
private void Function1(string s1, string s2) {
Debug.Log("Function1: " + s1 + " " + s2);
}
This code is going to add two different functions to your delegate list.
----------
Unfortunately, you can’t pass in the parameters EXACTLY like you were wanting to, but there are ways to kind of do it. Parameters can only be passed to it when the function is actually called. What you could do, is store those parameters that you want into a little struct or something and add it to the list instead. It could look like:
// A declared delegate with two string params
public delegate void MyDelegate(string str1, string str2);
public struct FunctionForLater {
public string str1;
public string str2;
public MyDelegate del;
// Constructor
public FunctionForLater(MyDelegate func, string s1, string s2) {
str1 = s1;
str2 = s2;
del = func;
}
}
public List someList = new List();
void Start() {
// — Add a function with params for later
someList.Add(new FunctionForLater(Function1, “Hello”, “World”));
// — Calling the stored function
FunctionForLater func = someList[0];
func.del(func.str1, func.str2);
}
Hopefully that kind of makes sense. Let me know if it doesn’t!
_*[1]: Unity Connect