using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class TestCoroutine : MonoBehaviour
{
IEnumerator coroutine;
private void Start()
{
coroutine = Function1();
StartCoroutine(coroutine);
}
IEnumerator Function1()
{
Debug.Log("1111");
yield return new WaitForSeconds(2f);
Debug.Log("222");
}
private void Update()
{
if (Input.GetKeyDown(KeyCode.A))
{
StopCoroutine(coroutine);//下面两种终止方法都不能终止协程只有这种才可以终止这种写法的协程
//StopCoroutine(Function1());
//StopCoroutine("Function1");
}
}
/*
IEnumerator function1()//代码中至少有一行yield return语句
{
Debug.Log(Time.time + "before return");
yield return new WaitForSeconds(2f);//yield return null ---间隔等待一帧的时间
//yield return 1;
//yield return "asdasd"; -----间隔等待一帧的时间 这三种写法和上面的Null结果相同都是等待一帧的时间
//yield return 10;
Debug.Log(Time.time);
yield return new WaitForSeconds(4f);
Debug.Log(Time.time + "after return");
}
// Start is called before the first frame update
void Start()
{
Debug.Log(Time.time);
StartCoroutine("function1");
//StartCoroutine(function1());
Debug.Log("====================");
}
// Update is called once per frame
void Update()
{
if (Input.GetKeyDown(KeyCode.S))
{
StopCoroutine("function1");//停止不了StartCoroutine(function1());这种写法的协同
}
if (Input.GetKeyDown(KeyCode.Escape))
{
StopAllCoroutines();
}
}*/
}