我在用携程等待执行的方法的时候,因为是多个方法调用,所以每执行一次,都要写一次携程等待的方法,麻烦更不好管理,在这里,我重新封装了一遍携程等待的方法,可以将要等待的方法传递给等待的携程,并且可以给传递的方法传递参数。
下面贴出我整理的代码
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 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 method,string name,int a)
{
yield return new WaitForSeconds(time);
method(name,a);
}
}
以上代码便是我整理的方法,有什么意见欢迎进群一起讨论。