重新封装携程等待执行的方法 - WaitForSeconds

我在用携程等待执行的方法的时候,因为是多个方法调用,所以每执行一次,都要写一次携程等待的方法,麻烦更不好管理,在这里,我重新封装了一遍携程等待的方法,可以将要等待的方法传递给等待的携程,并且可以给传递的方法传递参数。

下面贴出我整理的代码

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
using System;
using System.Collections;
using System.Collections.Generic;
using UnityEngine;

public class TestSprite : MonoBehaviour {

// Use this for initialization
void Start() {
WaitSeconds(2f, TestMethod);
WaitSeconds(4f, TestMethod, "string", 1);
}

// Update is called once per frame
void Update() {

}
void TestMethod()
{
print("run TestMethod()");
}
void TestMethod(string name, int a)
{
print("run TestMethod()"+" name = " +name + " int = " + a);
}
void WaitSeconds(float time, Action method)
{
StartCoroutine(IEWaitSeconds(time, method));
}
void WaitSeconds(float time, Action<string, int> method, string name, int a)
{
StartCoroutine(IEWaitSeconds(time, method,name,a));
}
IEnumerator IEWaitSeconds(float time,Action method)
{
yield return new WaitForSeconds(time);
method();
}
IEnumerator IEWaitSeconds(float time, Action<string,int> method,string name,int a)
{
yield return new WaitForSeconds(time);
method(name,a);
}
}

以上代码便是我整理的方法,有什么意见欢迎进群一起讨论。