本文由博主(YinaPan)原创,转载请注明出处:http://www.cnblogs.com/YinaPan/p/Unity_Coroutine.html 
using UnityEngine;
using System.Collections; public class CoroutineTest : MonoBehaviour { void Start () {
print("Starting " + Time.time);
StartCoroutine(WaitAndPrint(0.2F));
print("Before WaitAndPrint Finishes " + Time.time);
} IEnumerator WaitAndPrint(float waitTime) {
print("StartCoroutine1 " + Time.time);
StartCoroutine("DoSomething", 2.0f);
print("StartCoroutine2 " + Time.time);
yield return new WaitForSeconds(waitTime);
print("StopCoroutine1 " + Time.time);
StopCoroutine("DoSomething");
print("StopCoroutine2 " + Time.time);
} IEnumerator DoSomething(float someParameter) {
while (true) {
print("DoSomething Loop");
yield return null;
}
} void Projectile() {
print("Projectile " + Time.time);
} void LaunchProjectile() {
print("LaunchProjectile " + Time.time);
} // Update is called once per frame
void Update () {
if (Input.GetKeyDown(KeyCode.Space) && !IsInvoking("LaunchProjectile")) {
InvokeRepeating("LaunchProjectile", 2f, 0.5f);
Invoke("Projectile", 2f);
}
if (Input.GetKeyDown(KeyCode.X)) {
Debug.Log("CancelInvoke");
CancelInvoke("LaunchProjectile");
}
}
}
#region Assembly UnityEngine.dll, v0.0.0.0
// D:\Program\Unity Project\test\Library\UnityAssemblies\UnityEngine.dll
#endregion using System;
using System.Collections;
using UnityEngine.Internal;
using UnityEngine.Scripting; namespace UnityEngine {
// Summary:
// MonoBehaviour is the base class every script derives from.
[RequiredByNativeCode]
public class MonoBehaviour : Behaviour {
[WrapperlessIcall]
public MonoBehaviour(); // Summary:
// Disabling this lets you skip the GUI layout phase.
public bool useGUILayout { get; set; } // Summary:
// Cancels all Invoke calls on this MonoBehaviour.
public void CancelInvoke();
//
// Summary:
// Cancels all Invoke calls with name methodName on this behaviour.
//
// Parameters:
// methodName:
[WrapperlessIcall]
public void CancelInvoke(string methodName);
//
// Summary:
// Invokes the method methodName in time seconds.
//
// Parameters:
// methodName:
//
// time:
[WrapperlessIcall]
public void Invoke(string methodName, float time);
//
// Summary:
// Invokes the method methodName in time seconds, then repeatedly every repeatRate
// seconds.
//
// Parameters:
// methodName:
//
// time:
//
// repeatRate:
[WrapperlessIcall]
public void InvokeRepeating(string methodName, float time, float repeatRate);
//
// Summary:
// Is any invoke pending on this MonoBehaviour?
public bool IsInvoking();
//
// Summary:
// Is any invoke on methodName pending?
//
// Parameters:
// methodName:
[WrapperlessIcall]
public bool IsInvoking(string methodName);
//
// Summary:
// Logs message to the Unity Console (identical to Debug.Log).
//
// Parameters:
// message:
public static void print(object message);
//
// Summary:
// Starts a coroutine.
//
// Parameters:
// routine:
public Coroutine StartCoroutine(IEnumerator routine);
//
// Summary:
// Starts a coroutine named methodName.
//
// Parameters:
// methodName:
//
// value:
[ExcludeFromDocs]
public Coroutine StartCoroutine(string methodName);
//
// Summary:
// Starts a coroutine named methodName.
//
// Parameters:
// methodName:
//
// value:
[WrapperlessIcall]
public Coroutine StartCoroutine(string methodName, object value);
[WrapperlessIcall]
public Coroutine StartCoroutine_Auto(IEnumerator routine);
//
// Summary:
// Stops all coroutines running on this behaviour.
[WrapperlessIcall]
public void StopAllCoroutines();
public void StopCoroutine(Coroutine routine);
//
// Summary:
// Stops the first coroutine named methodName, or the coroutine stored in routine
// running on this behaviour.
//
// Parameters:
// methodName:
// Name of coroutine.
//
// routine:
// Name of the function in code.
public void StopCoroutine(IEnumerator routine);
//
// Summary:
// Stops the first coroutine named methodName, or the coroutine stored in routine
// running on this behaviour.
//
// Parameters:
// methodName:
// Name of coroutine.
//
// routine:
// Name of the function in code.
[WrapperlessIcall]
public void StopCoroutine(string methodName);
}
}
  • yield; The coroutine will continue after all Update functions have been called on the next frame.
    yield:协程在所有的Update函数于下一帧调用后继续执行。
  • yield WaitForSeconds(2); Continue after a specified time delay, after all Update functions have been called for the frame
    yield WaitForSeconds(2):在一个指定的时间延迟后继续执行,在所有的Update函数被调用之后。
  • yield WaitForFixedUpdate(); Continue after all FixedUpdate has been called on all scripts
    yield WaitForFixedUpdate():在所有脚本上所有的FixedUpdate被调用之后继续执行。
  • yield WWW Continue after a WWW download has completed.
    yield WWW:在WWW加载完成之后继续执行。
  • yield StartCoroutine(MyFunc); Chains the coroutine, and will wait for the MyFunc coroutine to complete first.
    yield StartCoroutine(MyFunc):用于链接协程,此将等待MyFunc协程完成先

yield  WaitForEndOfFrame();  用于本帧渲染完

yield return new WaitForEndOfFrame(); // 本帧渲染完后

StartCoroutine/StopCoroutineInvoke的更多相关文章

  1. 关于StartCoroutine的简单线程使用

    StartCoroutine在unity3d的帮助中叫做协程,意思就是启动一个辅助的线程. 在C#中直接有Thread这个线程,但是在unity中有些元素是不能操作的.这个时候可以使用协程来完成. 使 ...

  2. 9. MonoBehaviour.StartCoroutine 开始协同程序

    function StartCoroutine (routine : IEnumerator) : Coroutine 描述:开始协同程序. 一个协同程序在执行过程中,可以在任意位置使用yield语句 ...

  3. 使用startCoroutine制定倒计时

    使用startCoroutine制定倒计时 using UnityEngine; using System.Collections; public class TimerCoroutine : Mon ...

  4. 【错误总结1:unity StartCoroutine 报 NullReferenceException 错误】

    今天在一个项目中,写了一个单例的全局类,该类的作用是使用协程加载场景.但在StartCoroutine 这一步报了NullReferenceException 的错.仔细分析和搜索之后,得到错误原因. ...

  5. StartCoroutine 和 StopCoroutine

    我的Unity版本是2017.2.0p4(64-bit) StartCoroutine的两个版本: StartCoroutine(string methodName) StartCoroutine(I ...

  6. 问题记录,StartCoroutine(“str")问题

    StartCoroutine参数为函数字符串名,运行时出错,错误是:无法启动协程函数. 调用格式如下: gameManager.StartCoroutine(LuaOnLevelwasloaded() ...

  7. unity, StartCoroutine and StopCoroutine

    startCoroutine("func",1.0f)可以用stopCoroutine("func")来停. startCoroutine(func(1.0f) ...

  8. unity关于StartCoroutine的简单线程使用

    StartCoroutine在unity3d的帮助中叫做协程,意思就是启动一个辅助的线程. 在C#中直接有Thread这个线程,但是在unity中有些元素是不能操作的.这个时候可以使用协程来完成. 使 ...

  9. StartCoroutine的使用

    StartCoroutine在unity3d的帮助中叫做协程,意思就是启动一个辅助的线程. 在C#中直接有Thread这个线程,可是在unity中有些元素是不能操作的.这个时候能够使用协程来完毕. 使 ...

随机推荐

  1. 获取Delphi所有类的类信息

    Delphi遍历进程中所有Class的TypeInfo,即便是在implementation中的class或者其他 class的private的子class. 一般普通EXE中的TypeInfo存放在 ...

  2. java学习进制转换之查表法

    10进制转16进制,以及10进制转2进制,还有10进制转8进制,这些转换如果按照常规思路的话,会灰常的麻烦. 我们来看一下 10进制转16进制: 假如这里有一个十进制数字:35,我们的需求就是把这个3 ...

  3. poj1980

    首先想到费用流,但m<=100000还是算了吧那就感觉要用dp了,首先将a,b排序贪心一下可知,a,b的配对肯定不可能出现交叉这样就可以dp了,复杂度O(nm)还是过不去在贪心一下会发现,对于a ...

  4. Linux Shell编程(5)——shell特殊字符(下)

    {}代码块[花括号]. 这个结构也是一组命令代码块,事实上,它是匿名的函数.然而与一个函数所不同的,在代码块里的变量仍然能被脚本后面的代码访问. bash$ { local a;      a=123 ...

  5. 【最短路】NEERC15 F Froggy Ford(2015-2016 ACM-ICPC)(Codeforces GYM 100851)

    题目链接: http://codeforces.com/gym/100851 题目大意: 一只青蛙跳过宽为W的河,河中游N个石头,坐标xi,yi,现在往河中间添加一个石头,使得每次跳跃的最大的距离最小 ...

  6. 网络编程之TCP异步群聊:服务器端代码

    最近朋友建议我写一些关于微软云技术的博客留给学校下一届的学生们看,怕下一届的MSTC断档.于是我也觉的有这个必要.写了几篇博客之后,我觉得也有必要把这一年的学习内容放在博客做个纪念,就这样写了本篇博客 ...

  7. 9个Java初始化和回收的面试题

    1.Java中是如何区分重载方法的? 通过重载方法的参数类型和顺序来进行区分的. 注意:若参数类型和顺序均相同时,不管参数名是否相同,编译器均会报错,提示方法已经被定义.且不能根据返回值类型来区分,如 ...

  8. windows设置多长时间后自动关机 分类: windows常用小技巧 2014-04-15 09:35 230人阅读 评论(0) 收藏

    分二步: 第一步:点击windows键,在"搜索程序和文件"的文本框输入:cmd 第二步:输入:shutdown -s -t          (设置电脑一小时后自动关机) 备注: ...

  9. mysql 字符串类型数字排序

    排序字段+0,类似  Java 把 其他类型转换成字符串 比如 +“”: SELECT    b.tag_value AS NAME,    sum(b.uv) ASVALUE FROM    met ...

  10. 数据库系统——B+树索引

    原文来自于:http://dblab.cs.toronto.edu/courses/443/2013/05.btree-index.html 1. B+树索引概述 在上一篇文章中,我们讨论了关于ind ...