unity延时方法Invoke和InvokeRepeating

http://www.cnblogs.com/louissong/p/3832960.html?plg_nld=1&plg_uin=1&plg_auth=1&plg_nld=1&plg_usr=1&plg_vkey=1&plg_dev=1

MonoBehaviour里面有两个内置的延时方法
Invoke
1
2
3

Invoke(methodName:
string
, time:
float
):
void
;

methodName:方法名

time:多少秒后执行

InvokeRepeating
1
2
3
4

InvokeRepeating(methodName:
string
, time:
float
, repeatRate:
float
):
void
;

methodName:方法名

time:多少秒后执行

repeatRate:重复执行间隔

还有两个重要的方法:
IsInvoking:用来判断某方法是否被延时,即将执行
CancelInvoke:取消该脚本上的所有延时方法

下面是代码笔记:


复制代码

using UnityEngine;using System.Collections;public class DelayScript : MonoBehaviour { //当前时间 private float nowTime; //执行重复方法的次数 private int count; // Use this for initialization void Start () { nowTime = Time.time; Debug.Log("时间点:"+nowTime); this.Invoke("setTimeOut", 3.0f); this.InvokeRepeating("setInterval", 2.0f, 1.0f); } private void setTimeOut() { nowTime = Time.time; Debug.Log("执行延时方法:" + nowTime); } private void setInterval() { nowTime = Time.time; Debug.Log("执行重复方法:" + nowTime); count += 1; if(count==5) this.CancelInvoke(); }}


复制代码
最后编辑于
©著作权归作者所有,转载或内容合作请联系作者
【社区内容提示】社区部分内容疑似由AI辅助生成,浏览时请结合常识与多方信息审慎甄别。
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。

相关阅读更多精彩内容

  • Invoke Invoke(methodName:string, time:float):void;methodN...
    Codk阅读 863评论 0 0
  • 泛型 对象和实例是一个意思,类与对象的关系就像数据类型和变量一样。 泛型的主要目的之一就是用来指定类(如:容器)要...
    yueyue_projects阅读 641评论 0 0
  • 属性动画的集成关系Paste_Image.png 看一段属性动画的使用代码 跟进ObjectAnimator.of...
    明朗__阅读 7,817评论 0 2
  • 当第一缕桂花的幽香飘进了我的鼻子,我知道秋天来了。 校园里的桂花树枝繁叶茂,在浓浓的秋意中显得格外挺拔,生意盎然。...
    ventvs阅读 276评论 0 2
  • 2012年,初到上海,从一开始挤在女生四人间的窘迫,搬离四人间后,租住在上海老头老太的房子内,每天感受着上海房东老...
    王小姐要努力阅读 304评论 0 1

友情链接更多精彩内容