可能有多个参数的动作参数
Action parameter with possible multiple args
我有一个WaitAndRun
函数。我希望这个方法非常通用。
public static IEnumerator WaitAndRun<T>(float time, Action<T> action, object arg)
{
yield return new WaitForSeconds(time);
action((T)arg);
}
它与以下示例完美配合:
void an_action(int x)
{
Debug.Log(x);
}
//...
WaitAndRun<int>(1, an_action, 2);
问题是当我也需要多个参数时我想使用相同的定义。如何让这个函数支持同时调用单个和多个参数?例如:
void another_action(int x, int y);
如果我使用object[] args
而不是object arg
,我不知道如何修改剩余部分。
编辑:将功能更改为动作
这是在框架中完成的标准方法:
public static IEnumerator WaitAndRun<T>(float time, Action<T> action, T arg)
{
yield return new WaitForSeconds(time);
action(arg);
}
public static IEnumerator WaitAndRun<T1, T2>(float time, Action<T1, T2> action, T1 arg1, T2 arg2)
{
yield return new WaitForSeconds(time);
action(arg1, arg2);
}
public static IEnumerator WaitAndRun<T1, T2, T3>(float time, Action<T1, T2, T3> action, T1 arg1, T2 arg2, T3 arg3)
{
yield return new WaitForSeconds(time);
action(arg1, arg2, arg3);
}
public static IEnumerator WaitAndRun<T1, T2, T3, T4>(float time, Action<T1, T2, T3, T4> action, T1 arg1, T2 arg2, T3 arg3, T4 arg4)
{
yield return new WaitForSeconds(time);
action(arg1, arg2, arg3, arg4);
}
public static IEnumerator WaitAndRun<T1, T2, T3, T4, T5>(float time, Action<T1, T2, T3, T4, T5> action, T1 arg1, T2 arg2, T3 arg3, T4 arg4, T5 arg5)
{
yield return new WaitForSeconds(time);
action(arg1, arg2, arg3, arg4, arg5);
}
public static IEnumerator WaitAndRun<T1, T2, T3, T4, T5, T6>(float time, Action<T1, T2, T3, T4, T5, T6> action, T1 arg1, T2 arg2, T3 arg3, T4 arg4, T5 arg5, T6 arg6)
{
yield return new WaitForSeconds(time);
action(arg1, arg2, arg3, arg4, arg5, arg6);
}
public static IEnumerator WaitAndRun<T1, T2, T3, T4, T5, T6, T7>(float time, Action<T1, T2, T3, T4, T5, T6, T7> action, T1 arg1, T2 arg2, T3 arg3, T4 arg4, T5 arg5, T6 arg6, T7 arg7)
{
yield return new WaitForSeconds(time);
action(arg1, arg2, arg3, arg4, arg5, arg6, arg7);
}
public static IEnumerator WaitAndRun<T1, T2, T3, T4, T5, T6, T7, T8>(float time, Action<T1, T2, T3, T4, T5, T6, T7, T8> action, T1 arg1, T2 arg2, T3 arg3, T4 arg4, T5 arg5, T6 arg6, T7 arg7, T8 arg8)
{
yield return new WaitForSeconds(time);
action(arg1, arg2, arg3, arg4, arg5, arg6, arg7, arg8);
}
public static IEnumerator WaitAndRun<T1, T2, T3, T4, T5, T6, T7, T8, T9>(float time, Action<T1, T2, T3, T4, T5, T6, T7, T8, T9> action, T1 arg1, T2 arg2, T3 arg3, T4 arg4, T5 arg5, T6 arg6, T7 arg7, T8 arg8, T9 arg9)
{
yield return new WaitForSeconds(time);
action(arg1, arg2, arg3, arg4, arg5, arg6, arg7, arg8, arg9);
}
public static IEnumerator WaitAndRun<T1, T2, T3, T4, T5, T6, T7, T8, T9, T10>(float time, Action<T1, T2, T3, T4, T5, T6, T7, T8, T9, T10> action, T1 arg1, T2 arg2, T3 arg3, T4 arg4, T5 arg5, T6 arg6, T7 arg7, T8 arg8, T9 arg9, T10 arg10)
{
yield return new WaitForSeconds(time);
action(arg1, arg2, arg3, arg4, arg5, arg6, arg7, arg8, arg9, arg10);
}
public static IEnumerator WaitAndRun<T1, T2, T3, T4, T5, T6, T7, T8, T9, T10, T11>(float time, Action<T1, T2, T3, T4, T5, T6, T7, T8, T9, T10, T11> action, T1 arg1, T2 arg2, T3 arg3, T4 arg4, T5 arg5, T6 arg6, T7 arg7, T8 arg8, T9 arg9, T10 arg10, T11 arg11)
{
yield return new WaitForSeconds(time);
action(arg1, arg2, arg3, arg4, arg5, arg6, arg7, arg8, arg9, arg10, arg11);
}
public static IEnumerator WaitAndRun<T1, T2, T3, T4, T5, T6, T7, T8, T9, T10, T11, T12>(float time, Action<T1, T2, T3, T4, T5, T6, T7, T8, T9, T10, T11, T12> action, T1 arg1, T2 arg2, T3 arg3, T4 arg4, T5 arg5, T6 arg6, T7 arg7, T8 arg8, T9 arg9, T10 arg10, T11 arg11, T12 arg12)
{
yield return new WaitForSeconds(time);
action(arg1, arg2, arg3, arg4, arg5, arg6, arg7, arg8, arg9, arg10, arg11, arg12);
}
public static IEnumerator WaitAndRun<T1, T2, T3, T4, T5, T6, T7, T8, T9, T10, T11, T12, T13>(float time, Action<T1, T2, T3, T4, T5, T6, T7, T8, T9, T10, T11, T12, T13> action, T1 arg1, T2 arg2, T3 arg3, T4 arg4, T5 arg5, T6 arg6, T7 arg7, T8 arg8, T9 arg9, T10 arg10, T11 arg11, T12 arg12, T13 arg13)
{
yield return new WaitForSeconds(time);
action(arg1, arg2, arg3, arg4, arg5, arg6, arg7, arg8, arg9, arg10, arg11, arg12, arg13);
}
public static IEnumerator WaitAndRun<T1, T2, T3, T4, T5, T6, T7, T8, T9, T10, T11, T12, T13, T14>(float time, Action<T1, T2, T3, T4, T5, T6, T7, T8, T9, T10, T11, T12, T13, T14> action, T1 arg1, T2 arg2, T3 arg3, T4 arg4, T5 arg5, T6 arg6, T7 arg7, T8 arg8, T9 arg9, T10 arg10, T11 arg11, T12 arg12, T13 arg13, T14 arg14)
{
yield return new WaitForSeconds(time);
action(arg1, arg2, arg3, arg4, arg5, arg6, arg7, arg8, arg9, arg10, arg11, arg12, arg13, arg14);
}
public static IEnumerator WaitAndRun<T1, T2, T3, T4, T5, T6, T7, T8, T9, T10, T11, T12, T13, T14, T15>(float time, Action<T1, T2, T3, T4, T5, T6, T7, T8, T9, T10, T11, T12, T13, T14, T15> action, T1 arg1, T2 arg2, T3 arg3, T4 arg4, T5 arg5, T6 arg6, T7 arg7, T8 arg8, T9 arg9, T10 arg10, T11 arg11, T12 arg12, T13 arg13, T14 arg14, T15 arg15)
{
yield return new WaitForSeconds(time);
action(arg1, arg2, arg3, arg4, arg5, arg6, arg7, arg8, arg9, arg10, arg11, arg12, arg13, arg14, arg15);
}
public static IEnumerator WaitAndRun<T1, T2, T3, T4, T5, T6, T7, T8, T9, T10, T11, T12, T13, T14, T15, T16>(float time, Action<T1, T2, T3, T4, T5, T6, T7, T8, T9, T10, T11, T12, T13, T14, T15, T16> action, T1 arg1, T2 arg2, T3 arg3, T4 arg4, T5 arg5, T6 arg6, T7 arg7, T8 arg8, T9 arg9, T10 arg10, T11 arg11, T12 arg12, T13 arg13, T14 arg14, T15 arg15, T16 arg16)
{
yield return new WaitForSeconds(time);
action(arg1, arg2, arg3, arg4, arg5, arg6, arg7, arg8, arg9, arg10, arg11, arg12, arg13, arg14, arg15, arg16);
}
注意 Action<...>
只定义了 16 个参数。
我也把func
改成action
作为一个函数严格returns一个值,我想说的很清楚。
这里有一个替代建议:使用 lambda 表达式及其捕获局部变量的能力:
public static IEnumerator WaitAndRun(float time, Action action)
{
yield return new WaitForSeconds(time);
action();
}
用法:
WaitAndRun(1, () => an_action(2));
WaitAndRun(1, () => another_action(2, 3));
我有一个WaitAndRun
函数。我希望这个方法非常通用。
public static IEnumerator WaitAndRun<T>(float time, Action<T> action, object arg)
{
yield return new WaitForSeconds(time);
action((T)arg);
}
它与以下示例完美配合:
void an_action(int x)
{
Debug.Log(x);
}
//...
WaitAndRun<int>(1, an_action, 2);
问题是当我也需要多个参数时我想使用相同的定义。如何让这个函数支持同时调用单个和多个参数?例如:
void another_action(int x, int y);
如果我使用object[] args
而不是object arg
,我不知道如何修改剩余部分。
编辑:将功能更改为动作
这是在框架中完成的标准方法:
public static IEnumerator WaitAndRun<T>(float time, Action<T> action, T arg)
{
yield return new WaitForSeconds(time);
action(arg);
}
public static IEnumerator WaitAndRun<T1, T2>(float time, Action<T1, T2> action, T1 arg1, T2 arg2)
{
yield return new WaitForSeconds(time);
action(arg1, arg2);
}
public static IEnumerator WaitAndRun<T1, T2, T3>(float time, Action<T1, T2, T3> action, T1 arg1, T2 arg2, T3 arg3)
{
yield return new WaitForSeconds(time);
action(arg1, arg2, arg3);
}
public static IEnumerator WaitAndRun<T1, T2, T3, T4>(float time, Action<T1, T2, T3, T4> action, T1 arg1, T2 arg2, T3 arg3, T4 arg4)
{
yield return new WaitForSeconds(time);
action(arg1, arg2, arg3, arg4);
}
public static IEnumerator WaitAndRun<T1, T2, T3, T4, T5>(float time, Action<T1, T2, T3, T4, T5> action, T1 arg1, T2 arg2, T3 arg3, T4 arg4, T5 arg5)
{
yield return new WaitForSeconds(time);
action(arg1, arg2, arg3, arg4, arg5);
}
public static IEnumerator WaitAndRun<T1, T2, T3, T4, T5, T6>(float time, Action<T1, T2, T3, T4, T5, T6> action, T1 arg1, T2 arg2, T3 arg3, T4 arg4, T5 arg5, T6 arg6)
{
yield return new WaitForSeconds(time);
action(arg1, arg2, arg3, arg4, arg5, arg6);
}
public static IEnumerator WaitAndRun<T1, T2, T3, T4, T5, T6, T7>(float time, Action<T1, T2, T3, T4, T5, T6, T7> action, T1 arg1, T2 arg2, T3 arg3, T4 arg4, T5 arg5, T6 arg6, T7 arg7)
{
yield return new WaitForSeconds(time);
action(arg1, arg2, arg3, arg4, arg5, arg6, arg7);
}
public static IEnumerator WaitAndRun<T1, T2, T3, T4, T5, T6, T7, T8>(float time, Action<T1, T2, T3, T4, T5, T6, T7, T8> action, T1 arg1, T2 arg2, T3 arg3, T4 arg4, T5 arg5, T6 arg6, T7 arg7, T8 arg8)
{
yield return new WaitForSeconds(time);
action(arg1, arg2, arg3, arg4, arg5, arg6, arg7, arg8);
}
public static IEnumerator WaitAndRun<T1, T2, T3, T4, T5, T6, T7, T8, T9>(float time, Action<T1, T2, T3, T4, T5, T6, T7, T8, T9> action, T1 arg1, T2 arg2, T3 arg3, T4 arg4, T5 arg5, T6 arg6, T7 arg7, T8 arg8, T9 arg9)
{
yield return new WaitForSeconds(time);
action(arg1, arg2, arg3, arg4, arg5, arg6, arg7, arg8, arg9);
}
public static IEnumerator WaitAndRun<T1, T2, T3, T4, T5, T6, T7, T8, T9, T10>(float time, Action<T1, T2, T3, T4, T5, T6, T7, T8, T9, T10> action, T1 arg1, T2 arg2, T3 arg3, T4 arg4, T5 arg5, T6 arg6, T7 arg7, T8 arg8, T9 arg9, T10 arg10)
{
yield return new WaitForSeconds(time);
action(arg1, arg2, arg3, arg4, arg5, arg6, arg7, arg8, arg9, arg10);
}
public static IEnumerator WaitAndRun<T1, T2, T3, T4, T5, T6, T7, T8, T9, T10, T11>(float time, Action<T1, T2, T3, T4, T5, T6, T7, T8, T9, T10, T11> action, T1 arg1, T2 arg2, T3 arg3, T4 arg4, T5 arg5, T6 arg6, T7 arg7, T8 arg8, T9 arg9, T10 arg10, T11 arg11)
{
yield return new WaitForSeconds(time);
action(arg1, arg2, arg3, arg4, arg5, arg6, arg7, arg8, arg9, arg10, arg11);
}
public static IEnumerator WaitAndRun<T1, T2, T3, T4, T5, T6, T7, T8, T9, T10, T11, T12>(float time, Action<T1, T2, T3, T4, T5, T6, T7, T8, T9, T10, T11, T12> action, T1 arg1, T2 arg2, T3 arg3, T4 arg4, T5 arg5, T6 arg6, T7 arg7, T8 arg8, T9 arg9, T10 arg10, T11 arg11, T12 arg12)
{
yield return new WaitForSeconds(time);
action(arg1, arg2, arg3, arg4, arg5, arg6, arg7, arg8, arg9, arg10, arg11, arg12);
}
public static IEnumerator WaitAndRun<T1, T2, T3, T4, T5, T6, T7, T8, T9, T10, T11, T12, T13>(float time, Action<T1, T2, T3, T4, T5, T6, T7, T8, T9, T10, T11, T12, T13> action, T1 arg1, T2 arg2, T3 arg3, T4 arg4, T5 arg5, T6 arg6, T7 arg7, T8 arg8, T9 arg9, T10 arg10, T11 arg11, T12 arg12, T13 arg13)
{
yield return new WaitForSeconds(time);
action(arg1, arg2, arg3, arg4, arg5, arg6, arg7, arg8, arg9, arg10, arg11, arg12, arg13);
}
public static IEnumerator WaitAndRun<T1, T2, T3, T4, T5, T6, T7, T8, T9, T10, T11, T12, T13, T14>(float time, Action<T1, T2, T3, T4, T5, T6, T7, T8, T9, T10, T11, T12, T13, T14> action, T1 arg1, T2 arg2, T3 arg3, T4 arg4, T5 arg5, T6 arg6, T7 arg7, T8 arg8, T9 arg9, T10 arg10, T11 arg11, T12 arg12, T13 arg13, T14 arg14)
{
yield return new WaitForSeconds(time);
action(arg1, arg2, arg3, arg4, arg5, arg6, arg7, arg8, arg9, arg10, arg11, arg12, arg13, arg14);
}
public static IEnumerator WaitAndRun<T1, T2, T3, T4, T5, T6, T7, T8, T9, T10, T11, T12, T13, T14, T15>(float time, Action<T1, T2, T3, T4, T5, T6, T7, T8, T9, T10, T11, T12, T13, T14, T15> action, T1 arg1, T2 arg2, T3 arg3, T4 arg4, T5 arg5, T6 arg6, T7 arg7, T8 arg8, T9 arg9, T10 arg10, T11 arg11, T12 arg12, T13 arg13, T14 arg14, T15 arg15)
{
yield return new WaitForSeconds(time);
action(arg1, arg2, arg3, arg4, arg5, arg6, arg7, arg8, arg9, arg10, arg11, arg12, arg13, arg14, arg15);
}
public static IEnumerator WaitAndRun<T1, T2, T3, T4, T5, T6, T7, T8, T9, T10, T11, T12, T13, T14, T15, T16>(float time, Action<T1, T2, T3, T4, T5, T6, T7, T8, T9, T10, T11, T12, T13, T14, T15, T16> action, T1 arg1, T2 arg2, T3 arg3, T4 arg4, T5 arg5, T6 arg6, T7 arg7, T8 arg8, T9 arg9, T10 arg10, T11 arg11, T12 arg12, T13 arg13, T14 arg14, T15 arg15, T16 arg16)
{
yield return new WaitForSeconds(time);
action(arg1, arg2, arg3, arg4, arg5, arg6, arg7, arg8, arg9, arg10, arg11, arg12, arg13, arg14, arg15, arg16);
}
注意 Action<...>
只定义了 16 个参数。
我也把func
改成action
作为一个函数严格returns一个值,我想说的很清楚。
这里有一个替代建议:使用 lambda 表达式及其捕获局部变量的能力:
public static IEnumerator WaitAndRun(float time, Action action)
{
yield return new WaitForSeconds(time);
action();
}
用法:
WaitAndRun(1, () => an_action(2));
WaitAndRun(1, () => another_action(2, 3));