1.大概意思执行其它任务时,此任务也同时执行
2.可以用于摆动效果,此脚本对依附物体做了左右摇摆控制
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class IEnumumerator : MonoBehaviour {
private Transform m_transform;
private int flag =1 ;
// Use this for initialization
void Start () {
m_transform = gameObject.GetComponent<Transform>();
Debug.Log("任务一");
Debug.Log("任务二");
StartCoroutine("task3");
Debug.Log("任务四");
Debug.Log("任务五");
Debug.Log("任务六");
}
// Update is called once per frame
void Update () { //如果按下S键则停止任务三
if (Input.GetKey(KeyCode.S)) {
StopCoroutine("task3");
}
}
IEnumerator task3()
{
while (true)
{
yield return new WaitForSeconds(2); //每2秒执行一次
//yield return null; //不等待持续执行
Debug.Log("任务三");
m_transform.Translate(Vector3.left * flag);
flag = -flag;
}
}
}