using System.Collections;
using System.Collections.Generic;
using UnityEngine;public class IEDemo : MonoBehaviour {
public float timer=0;
private void Start()
{
//启动协程
//StartCoroutine(Demo());//StartCoroutine(Demo());
//StartCoroutine(DelayPlayAudio());
//StartCoroutine(WaitForSeconds(2));
StartCoroutine(InitMonsters(3,0.5f));
}
private void Update()
{
Debug.Log("Update");
}
IEnumerator Demo() {
Debug.Log(111);
//协程执行暂停,等一帧之后恢复
yield return null;
Debug.Log(222);
}
IEnumerator Circle() {
while (true) {
Debug.Log("Circle");
yield return 3;
}
}
IEnumerator DelayPlayAudio() {
while (timer <= 3) {
timer += Time.deltaTime;
Debug.Log("DelayPlayAudio");
//等下一帧继续执行
yield return null;
}
Debug.Log("播放Audio");
}IEnumerator WaitForSeconds(float seconds) {
//等待几秒后,继续执行
yield return new WaitForSeconds(seconds);
Debug.Log("生成野怪");
}IEnumerator InitMonsters(float waveInterval,float monsterInterval) {
for (int i=0;i<5;i++) {
yield return new WaitForSeconds(waveInterval);
for (int j=0;j<10;j++) {
Debug.Log("正在生成第"+(i+1)+"波第"+(j+1)+"个怪");
yield return new WaitForSeconds(monsterInterval);
}
}
}
}
using System.Collections;
using System.Collections.Generic;
using UnityEngine;public class IEDemo : MonoBehaviour {
public float timer=0;
private Coroutine coroutine;
private IEnumerator Start()
{
//启动协程
//StartCoroutine(Demo());//StartCoroutine(Demo());
//StartCoroutine(DelayPlayAudio());
//StartCoroutine(WaitForSeconds(2));
//StartCoroutine(InitMonsters(3,0.5f));
//StartCoroutine(WaitForEndFrame());
//StartCoroutine(ParentCorotine());
//coroutine=StartCoroutine("Circle");
yield return StartCoroutine(PrintNumber());
}
private void OnGUI()
{
Debug.Log("ONGUI");
}
private void Update()
{
if (Input.GetKeyDown(KeyCode.Space)) {
StopCoroutine(coroutine);
}
}
IEnumerator Demo() {
Debug.Log(111);
//协程执行暂停,等一帧之后恢复
yield return null;
Debug.Log(222);
}
IEnumerator Circle() {
while (true) {
Debug.Log("Circle");
yield return 3;
}
}
IEnumerator DelayPlayAudio() {
while (timer <= 3) {
timer += Time.deltaTime;
Debug.Log("DelayPlayAudio");
//等下一帧继续执行
yield return null;
}
Debug.Log("播放Audio");
}IEnumerator WaitForSeconds(float seconds) {
//等待几秒后,继续执行
yield return new WaitForSeconds(seconds);
Debug.Log("生成野怪");
}IEnumerator InitMonsters(float waveInterval,float monsterInterval) {
for (int i=0;i<5;i++) {
yield return new WaitForSeconds(waveInterval);
for (int j=0;j<10;j++) {
Debug.Log("正在生成第"+(i+1)+"波第"+(j+1)+"个怪");
yield return new WaitForSeconds(monsterInterval);
}
}
}
IEnumerator WaitForEndFrame() {
Debug.Log(1111);
yield return new WaitForEndOfFrame();
Debug.Log(2222);
}
IEnumerator ParentCorotine() {
yield return StartCoroutine(SonCoroutine());
Debug.Log("Parent");
}IEnumerator SonCoroutine() {
yield return new WaitForSeconds(1);
Debug.Log("Son");}
IEnumerator PrintNumber() {
for (int i = 0; i < 10; i++) {
if (i>4)
{//停止协程
yield break;
}
Debug.Log(i);
yield return new WaitForSeconds(1);}
}
}