Unity协程简单使用
2018-02-04 18:45
316 查看
public class Coroutine : MonoBehaviour
{
private int Ret = 0;
//协程的简单写法
void Start()
{
//开启Test1协程
StartCoroutine(Test1());
//开启Test协程
StartCoroutine("Tes2");
//启动协程时就可以吧参数传递进去
StartCoroutine(Test3(5, 8));
}
//协程固定格式
private IEnumerator Test1()
{
//协程等待指定的秒数
yield return new WaitForSeconds(2f);
Debug.Log("Text1执行完毕");
//yield return 0;
//Debug.Log("协程执行完毕");
}
private IEnumerator Test2()
{
//协程等待指定的秒数
yield return new WaitForSeconds(5f);
Debug.Log("Test2协程执行完毕");
}
//协程也是可以传递参数的
private IEnumerator Test3(int x, int y)
{
//协程等待指定的秒数
yield return new WaitForSeconds(5f);
Ret = x * y;
Debug.Log("Test2协程执行完毕");
}
void Update()
{
//键盘输入A
if (Input.GetKeyUp(KeyCode.A))
{
Debug.Log("Ret=" + Ret);
}
//键盘输入B
if (Input.GetKeyUp(KeyCode.B))
{
//终止协程
StopAllCoroutines();
//第二种终止协程的方法
StopCoroutine("Test2");
}
}
}
{
private int Ret = 0;
//协程的简单写法
void Start()
{
//开启Test1协程
StartCoroutine(Test1());
//开启Test协程
StartCoroutine("Tes2");
//启动协程时就可以吧参数传递进去
StartCoroutine(Test3(5, 8));
}
//协程固定格式
private IEnumerator Test1()
{
//协程等待指定的秒数
yield return new WaitForSeconds(2f);
Debug.Log("Text1执行完毕");
//yield return 0;
//Debug.Log("协程执行完毕");
}
private IEnumerator Test2()
{
//协程等待指定的秒数
yield return new WaitForSeconds(5f);
Debug.Log("Test2协程执行完毕");
}
//协程也是可以传递参数的
private IEnumerator Test3(int x, int y)
{
//协程等待指定的秒数
yield return new WaitForSeconds(5f);
Ret = x * y;
Debug.Log("Test2协程执行完毕");
}
void Update()
{
//键盘输入A
if (Input.GetKeyUp(KeyCode.A))
{
Debug.Log("Ret=" + Ret);
}
//键盘输入B
if (Input.GetKeyUp(KeyCode.B))
{
//终止协程
StopAllCoroutines();
//第二种终止协程的方法
StopCoroutine("Test2");
}
}
}
相关文章推荐
- unity---EZGUI简单的使用1
- Unity V3 初步使用 —— 为我的.NET项目从简单三层架构转到IOC做准备
- Unity4.3新功能:2D工具 的简单使用教程
- Unity上使用Linq To XML——简单易维护的代码
- Unity协程使用经验
- Unity4.3新功能:2D工具 的简单使用教程
- Unity 学习笔记(1) — Unity简介及简单使用
- 理解Unity中的协程的使用
- Unity 3D之使用GameObject创建简单的可移动的对象
- Unity的协程,多线程,处理与使用方法
- [Unity框架]PureMVC在unity中的简单使用
- 「Unity」使用协程延时执行代码
- 使用Unity制作简单时钟动画
- Unity中使用协程实现倒计时功能
- Unity4.3 2D工具 的简单使用教程
- Metaio在Unity中的简单使用
- unity---EZGUI简单的使用1
- unity中使用GUI按钮实现简单的猜拳小游戏:
- 使用Unity2.0的Interceptor实现简单AOP
- 用Unity开发OculusRift体验演示教程(一)——搭建简单场景并使用Oculus提供的SDK包