using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class CoroutineTest : MonoBehaviour
{
//1s/3s/5s 分别去做某一件事
float timer = 1;
float timer3 = 3;
/*
* 1.什么是协程?
* 协程是一段在主线程中执行的代码逻辑
* 协程在每帧结束后都去检测yield return的条件是否满足,并且始终尝试执行完协程里面所有的代码,当执行完毕后,停止协程
*/
IEnumerator test(){
for (float i = 1; i >= 0; i-=Time.deltaTime)
{
yield return null;//现在停止这个方法,然后下一帧从这里继续开始!null
}
//表示刚好过了1s
Debug.Log("1s后...");
}
/*
* 1.打印5次 我要学游戏开发!
* 2.每帧打印1次 我要学游戏开发!
* 3.每隔1s 打印一次 我要学游戏开发!
*/
IEnumerator Say5Hello(string str){
//for (int i = 0; i < 5; i++)
//{
// Debug.Log("我要学游戏开发");
// yield return 0;
//}
//while (true)
//{
// Debug.Log("我要学游戏开发");
// yield return 0;
//}
//等同于 update
int second = 0;
while (true)
{
for (float timer = 0; timer < 1; timer+=Time.deltaTime)
{
yield return 0;
}
second++;
Debug.Log(str+"时间已经过了"+second+"s");
}
}
/*
* 这个方法突出牛逼的地方:方法的状态被存储了,这使得方法中定义的所有变量 都会被保存起来
*
* 2.协程的开始和终止
* StartCoroutine(Say5Hello());
* StopAllCoroutines() 来停止协程
* 注意:这会终止当前类中开启的所有协程,对于其他MonoBehavior类中运行的协程不起作用
*
* 如果想终止指定的协程,那么需在开启协程的时候 使用字符串名字来开启
* StartCoroutine("Say5Hello");//以字符串形式开启
* StopCoroutine("Say5Hello");//以字符串形式终止
*
* 3.协程的参数
* 如果用字符串形式开启,只能传递一个参数
* 如果想传递多个参数,必须通过方法名()形式开启
*
* 输出的内容hello,次数n,----------------------时间间隔,n秒
*/
void Start1()
{
//StartCoroutine(test());//开启名为test的协程
//StartCoroutine(Say5Hello("hello"));
//StartCoroutine("Say5Hello","hello");//以字符串形式开启
//StopCoroutine("Say5Hello");//以字符串形式终止
StartCoroutine(RepeatMessage(10, 2, "hello"));
}
IEnumerator RepeatMessage(int count,float frequency,string message){
for (int i = 0; i < count; i++)
{
for (float timer = 0; timer < frequency; timer+=Time.deltaTime)
{
yield return 0;
}
Debug.Log(message);
}
}
/*
* 4.协程的返回值
* 协程之间可以用yield 语句嵌套
*
* 隔一定时间去做某件事
* 距离上一次执行完毕1s后....
*
* yield return 遇到StartCoroutine(Wait(1.0f)) 表示剩余的代码将在子协程执行完毕后才能继续向下执行
* yield return 遇到 new WaitForSeconds(n); 表示剩余代码将在n秒后继续执行
* yield return 遇到 new WaitForFixedUpdate;表示剩余代码将在FixedUpdate执行完毕后执行
* yield return 遇到 WWW; 表示在www类下载文件结束后继续向下执行
* yield return 遇到 gameObject;表示在gameObject不为空的前提下 才继续向下执行
*/
IEnumerator DoSomethingTimer(){
Debug.Log("协程开始执行");
//yield return StartCoroutine(Wait(1.0f));//不在执行本程序,直到wait的程序结束
yield return new WaitForSeconds(1.0f);
Debug.Log("距离上一条消息记录已经过去了1s");
yield return StartCoroutine(Wait(3.5f));
Debug.Log("距离上一条消息记录已经过去了3.5s");
yield return new WaitForFixedUpdate();
Debug.Log("在本帧Fixedupdate执行完毕后");
}
//通过协程 控制对象的行为
//用给定的速度让一个小球 运动到指定的targetPos位置
//让小球在指定的几个位置之间移动
//用bool 来控制小球是否循环在路径上移动
//当小球移动到指定的位置时,停留2s后 继续向下一个目标点移动
//用到协程的嵌套
public Transform[] path;//存储路径上点关键点的位置信息
//public Transform targetPos;
public float moveSpeed;
private void Start()
{
//StartCoroutine(DoSomethingTimer());
//StartCoroutine(MoveToPos(targetPos.position));
StartCoroutine(MoveOnPath(true));
}
IEnumerator MoveOnPath(bool loop){
do
{
foreach (Transform item in path)
{
yield return StartCoroutine(MoveToPos(item.position));
}
}
while (loop);
}
IEnumerator MoveToPos(Vector3 target){
while (transform.position != target)
{
transform.position = Vector3.MoveTowards(transform.position, target, moveSpeed * Time.deltaTime);
yield return 0;
}
}
/*
* l 在程序中调用StopCoroutine()方法只能终止以字符串形式启动(开始)的协程;
l 多个协程可以同时运行,它们会根据各自的启动顺序来更新;
l 协程可以嵌套任意多层(在这个例子中我们只嵌套了一层);
l 如果你想让多个脚本访问一个协程,那么你可以定义静态的协程;
l 协程不是多线程(尽管它们看上去是这样的),它们运行在同一线程中,跟普通的脚本一样;
l 如果你的程序需要进行大量的计算,那么可以考虑在一个随时间进行的协程中处理它们;
l IEnumerator类型的方法不能带ref或者out型的参数,但可以带被传递的引用;
l 目前在Unity中没有简便的方法来检测作用于对象的协程数量以及具体是哪些协程作用在对象上。
*/
private void FixedUpdate()
{
Debug.Log("FixedUpdate");
}
IEnumerator Wait(float duration){
for (float timer = 0; timer < duration; timer+=Time.deltaTime)
{
yield return 0;
}
//表示该协程在duration时间后执行完毕
}
void Update()
{
Debug.Log("Update");
//timer -= Time.deltaTime;
//if (timer <= 0)
//{
// Debug.Log("1s后");
// timer = 100000;
//}
//timer3 -= Time.deltaTime;
//if (timer3 <= 0)
//{
// Debug.Log("3s后");
// timer = 100000;
//}
}
}
Unity协程
最新推荐文章于 2024-07-11 16:15:16 发布
这篇博客详细介绍了Unity中协程的工作原理及其应用。通过示例代码展示了如何使用协程实现延迟操作、循环执行以及在游戏开发中的各种实用场景,如定时任务和对象行为控制。协程允许在主线程中按需暂停和恢复执行,简化了复杂的时间序列逻辑。
摘要由CSDN通过智能技术生成