using System;
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
/*
* 什么是协程
* 协程是一段在主线程中执行的代码逻辑
* 协程在每帧结束后都去检测yield return的条件是否满足,并且始终尝试执行完协程里面的所有的代码,当执行完毕后,停止协程
* 这个方法突出的地方 方法的状态被保存了,使得方法中的所有变量 都会被保存起来
* 协程的开始和终止 startCorotins()和 startAllCorotins()来停止协程 注意,这会终止当前类中的所有协程,
* 对于其他的monobehavior类中的运行不起作用
* 如果想种植指定的协程,那么需在开启协程的时候使用字符串的名字来开启
* 以字符串形式开启 以字符串形式终止
*
* 协程的参数,如果用字符串形式开启,只能传递一个参数
* 如果想用多个参数,只能通过方法名加()开启
*
* 协程的返回值
* 写成之间用yeild语句嵌套
*
* 隔一段时间去做某事
* 距离上一次执行完毕1s后
* yeild return遇到startcoroutine(wait(1)) 表示剩余的代码将在子协程执行完毕后才能向下继续执行
* yeild return遇到new waitforseconds(n)表示在n秒后继续执行
* yeild return遇到 new waitforfixedUpdate表示剩余代码将在fixedupdate执行完毕后执行
* yeild return遇到www 表示在www类文件下载结束后继续访问
* yeild return遇到gameobject 表示在gameobject不为空的前提下才继续向下执行
*/
public class ConTest : MonoBehaviour
{
private float timer;
private bool one;
private bool three;
private bool five;
private void Start()
{
//StartCoroutine(Test());//开启名为test的协程
//StartCoroutine(Test2());
// StartCoroutine(Test3("hello="));
//StartCoroutine(Test4("hello",6,2));
//StartCoroutine(BallMove(targetPos));
StartCoroutine(OnPath());
}
IEnumerator Test5(float duration)
{
for (float timer = 0; timer < duration; timer+=Time.deltaTime)
{
yield return 0;
}
//表示该协程在duration时间后执行完毕
}
IEnumerator DoSomethingTimer()
{
Debug.Log("协程开始执行");
yield return new WaitForSeconds(1);
Debug.Log("距离上一条消息记录已经过去了1s");
yield return new WaitForSeconds(3.5f);
Debug.Log("距离上一条消息记录已经过去了3.5s");
}
//通过协程 让一个小球以指定的速度运动到指定位置
public GameObject ball;
public float moveSpeed;
public Transform[] path;
public bool loop;
IEnumerator BallMove(Transform target)
{
while (transform.position!=target.position)
{
transform.position=Vector3.MoveTowards(transform.position,target.position,
moveSpeed*Time.deltaTime);
yield return 0;
}
}
IEnumerator OnPath()
{
do
{
foreach (Transform item in path)
{
yield return StartCoroutine(BallMove(item));
yield return new WaitForSeconds(2);
}
}
while (loop);
}
IEnumerator Test()
{
for (float i =1 ; i>=0; i-=Time.deltaTime)
{
yield return 0;//停止这个方法,下一帧继续从这开始
}
Debug.Log("1秒后");
}
//打印五次hello
IEnumerator Test1()
{
Debug.Log("hello");
yield return 0;
Debug.Log("hello");
yield return 0;
Debug.Log("hello");
yield return 0;
Debug.Log("hello");
yield return 0;
Debug.Log("hello");
// for (int i = 1; i < 5; i++)
// {
// yield return 0;
// Debug.Log("hello");
// }
}
//每帧执行一次
IEnumerator Test2()
{
// for (float i = 1; i < 5; i+=Time.deltaTime)
// {
// yield return 0;
// Debug.Log("hello");
// }
while (true)
{
//等同于自己实现了Update
Debug.Log("hello");
yield return 0;
}
}
//每隔一秒输出一次
IEnumerator Test3(string str)
{
float a1=0;
while (true)
{
for ( float i = 0; i < 1; i+=Time.deltaTime)
{
yield return 0;
}
Debug.Log(str+a1);
a1++;
}
}
IEnumerator Test4(string str,float num,float time)
{
while (time<num)
{
for ( float i = 0; i < 1; i+=Time.deltaTime)
{
yield return 0;
}
Debug.Log(str+"---"+num+">"+time);
time ++;
}
}
private void Update()
{
timer += Time.deltaTime;
if (one == false)
{
if (timer>1)
{
//Debug.Log("5465");
one = true;
}
}
if (three == false)
{
if (timer>3)
{
//Debug.Log("5465");
three = true;
}
}
if (five == false)
{
if (timer>5)
{
//Debug.Log("5465");
five = true;
}
}
}
}
协程代码...
最新推荐文章于 2024-05-02 16:43:06 发布