测试一:
public class TestCoroutine1 : MonoBehaviour
{}
2.测试二:
public class TestCoroutine2 : MonoBehaviour
{
private IEnumerator coroutine;
void Start ()
{
coroutine = Test(new float[]{1.2f, 4}, "dd", 2);
StartCoroutine(coroutine);
//StartCoroutine(Test(1, "dd"));
//StartCoroutine("Test", 2);
}
void Update ()
{
if(Input.GetKeyDown(KeyCode.A))
{
StopCoroutine(coroutine);
// StopCoroutine("Test");// 可以阻止StartCoroutine(“Test”);执行,,但性能开销大,且只能传递一个参数
//StopCoroutine(Test());不能够阻止协同继续执行,
Debug.Log("hello");
//StopAllCoroutines();//可以阻止各种协同继续执行
}
}
IEnumerator Test(float[] a, string str, int ? w = 0)
{
//while(Time.time < 8f)
{
yield return new WaitForSeconds(1f);
Debug.Log(a[1] + Time.time + str);
}
yield return new WaitForSeconds(1f);
Debug.Log(Time.time);
yield return new WaitForSeconds(1f);
Debug.Log(Time.time + str);
yield return new WaitForSeconds(1f);
Debug.Log(Time.time + "==" + w);
yield return new WaitForSeconds(1f);
Debug.Log(Time.time);
yield return new WaitForSeconds(1f);
Debug.Log(Time.time + str);
yield return new WaitForSeconds(1f);
Debug.Log(Time.time);
}
}