Unity里如何停掉Coroutine, Invoke的函数的执行,以及脚本的Start()和Update()
2018-03-27 11:41
926 查看
先列出所有可选的方法:StopAllCoroutines(); //只对Coroutine起作用,会停掉此脚本里所有的Coroutine。this.enable = false; // 禁掉脚本,其实只是禁掉Start(), Update()函数,脚本里其他的东东还是有效。this.gameobject.SetActive(false); //脚本所挂靠的gameobject被禁掉了,在场景中消失不见,不可访问了。用GameObject.Find()也找不到了。Destory(gameObject); //终极解决方案,gameObject被彻底从内存干掉了,依赖于gameObject存在的Invoke,Coroutine,其他的各种函数自然也被干掉了。
终极解决方案适用于所有。下面列出各自的独特适用方法。
停掉Coroutine:方法1:StopAllCoroutines();方法2:this.gameObject.SetActive(false);Coroutine的运行前提是:gameObject存在,且gameObject.SetActive(true)。当gameObject.SetActive(false)时,这时StartCoroutine()会直接报错,已经被StartCoroutine()调用的Coroutine会被强制停掉。
停掉Invoke(string methodname, float time):方法1:CancelInvoke();Invoke的运行前提是:gameObject存在,且gameObject.SetActive(true)时,Invoke()调用才有效。但对于已经被调用的Invoke(), gameObject.SetActive(false)无法影响之。
停掉Start()、Update():方法1:this.enable = false;方法2:this.gameobject.SetActive(false); 脚本作为场景中某个Gameobject的Component,当脚本中有Awake(),Start(),Update()函数时,在该GameObject的Inspector栏里,脚本前会有一个勾,this.enable=false,只是禁掉Start(), Update()函数的执行而已。但对于已经开启执行的Coroutine, Invoke的函数,不会干扰其执行。
需要注意的是,使用这些方法的细节。Destroy(gameObject); this.gameobject.SetActive(false); this.enable = false; StopAllCoroutines(); 都不是马上执行的,具体生效时间未知,不过知道是在此frame里函数执行完之后到下一个frame函数开始执行之前。因此,可以看到:
运行结果是:
![](https://img-blog.csdn.net/20140825184756536)
尽管在Start()函数里已然Destroy(gameObject),StartCoroutine(Coroutine1())依然会被执行,Invoke("ShowMsg", 2.0f)也会被执行。不过当下一帧开始时,Destroy(gameObject)生效了,于是Coroutine1()后面的信息就无法输出了,Invoke的ShowMsg()函数也来不及执行。不过,将Start()里的Invoke("ShowMsg", 2.0f)改为Invoke("ShowMsg", 0.0f),运行结果也不变,推测是因为Invoke(string methodname, float time),time为float,计算机很难计量float,无法立即执行。有待核实Invoke()的运作机理。从另一方面去验证我的观点。
运行结果为:
![](https://img-blog.csdn.net/20140825184651156)
由此,可以进一步推测,"Destroy(gameObject); this.gameobject.SetActive(false); this.enable = false; StopAllCoroutines(); 都不是马上执行的,具体生效时间未知,不过知道是在此frame里函数执行完之后到下一个frame函数开始执行之前。" ==> 此处的Destroy(gameObject)的生效时机是在Start()函数第一帧的执行结束后,在进入Update()函数之前。(很可能是第一帧的Start()函数的内容执行完毕后生效的,即yield return null后立马生效。)因此也容易理解下面的结果:
结果是:
![](https://img-blog.csdn.net/20140825184724500)
将Start()改为
结果变为:
![](https://img-blog.csdn.net/20140825184933318)
运行结果为:
![](https://img-blog.csdn.net/20140825184833015)
运行结果为:
![](https://img-blog.csdn.net/20140825184909140)
运行结果为:
![](https://img-blog.csdn.net/20140825185123782)
当然,Unity也提供让更改立即生效的接口,如DestroyImmediate(Object obj),会立即生效,此语句后,再访问这个obj会报错。
以上内容基于https://blog.csdn.net/u010153703/article/details/38824521整理而成。
终极解决方案适用于所有。下面列出各自的独特适用方法。
停掉Coroutine:方法1:StopAllCoroutines();方法2:this.gameObject.SetActive(false);Coroutine的运行前提是:gameObject存在,且gameObject.SetActive(true)。当gameObject.SetActive(false)时,这时StartCoroutine()会直接报错,已经被StartCoroutine()调用的Coroutine会被强制停掉。
停掉Invoke(string methodname, float time):方法1:CancelInvoke();Invoke的运行前提是:gameObject存在,且gameObject.SetActive(true)时,Invoke()调用才有效。但对于已经被调用的Invoke(), gameObject.SetActive(false)无法影响之。
停掉Start()、Update():方法1:this.enable = false;方法2:this.gameobject.SetActive(false); 脚本作为场景中某个Gameobject的Component,当脚本中有Awake(),Start(),Update()函数时,在该GameObject的Inspector栏里,脚本前会有一个勾,this.enable=false,只是禁掉Start(), Update()函数的执行而已。但对于已经开启执行的Coroutine, Invoke的函数,不会干扰其执行。
需要注意的是,使用这些方法的细节。Destroy(gameObject); this.gameobject.SetActive(false); this.enable = false; StopAllCoroutines(); 都不是马上执行的,具体生效时间未知,不过知道是在此frame里函数执行完之后到下一个frame函数开始执行之前。因此,可以看到:
void Start() { Debug.Log ("Enter Start()"); Destroy(gameObject); StartCoroutine(Coroutine1()); Invoke("ShowMsg", 2.0f); } IEnumerator Coroutine1() { Debug.Log("Coroutine1 Enter."); yield return new WaitForSeconds(1.0f); Debug.Log("Hello, I am Coroutine1."); } void ShowMsg() { if (bShowMsg) { Debug.Log("Hello from ShowMsg()"); bShowMsg = false; } } void Update() { Debug.Log("Enter Update()"); }
运行结果是:
尽管在Start()函数里已然Destroy(gameObject),StartCoroutine(Coroutine1())依然会被执行,Invoke("ShowMsg", 2.0f)也会被执行。不过当下一帧开始时,Destroy(gameObject)生效了,于是Coroutine1()后面的信息就无法输出了,Invoke的ShowMsg()函数也来不及执行。不过,将Start()里的Invoke("ShowMsg", 2.0f)改为Invoke("ShowMsg", 0.0f),运行结果也不变,推测是因为Invoke(string methodname, float time),time为float,计算机很难计量float,无法立即执行。有待核实Invoke()的运作机理。从另一方面去验证我的观点。
IEnumerator Start() { Debug.Log ("Enter Start()"); Destroy(gameObject); yield return null; StartCoroutine(Coroutine1()); Invoke("ShowMsg", 2.0f); }
运行结果为:
由此,可以进一步推测,"Destroy(gameObject); this.gameobject.SetActive(false); this.enable = false; StopAllCoroutines(); 都不是马上执行的,具体生效时间未知,不过知道是在此frame里函数执行完之后到下一个frame函数开始执行之前。" ==> 此处的Destroy(gameObject)的生效时机是在Start()函数第一帧的执行结束后,在进入Update()函数之前。(很可能是第一帧的Start()函数的内容执行完毕后生效的,即yield return null后立马生效。)因此也容易理解下面的结果:
void Start() { Debug.Log ("Enter Start()"); gameObject.SetActive(false); Invoke("ShowMsg", 2.0f); } void ShowMsg() { if (bShowMsg) { Debug.Log("Hello from ShowMsg()"); bShowMsg = false; } } void Update() { Debug.Log("Enter Update()"); }
结果是:
将Start()改为
IEnumerator Start() { Debug.Log ("Enter Start()"); gameObject.SetActive(false); yield return null; Invoke("ShowMsg", 2.0f); }
结果变为:
IEnumerator Start() { Debug.Log ("Enter Start()"); StartCoroutine(Coroutine1()); StopAllCoroutines(); yield return null; } IEnumerator Coroutine1() { Debug.Log("Coroutine1 Enter."); yield return null; //效果同yield return new WaitForSeconds(0.0f) Debug.Log("Hello, I am Coroutine1."); }
运行结果为:
IEnumerator Start() { Debug.Log ("Enter Start()"); StartCoroutine(Coroutine1()); yield return null; StopAllCoroutines(); } IEnumerator Coroutine1() { Debug.Log("Coroutine1 Enter."); yield return null; //效果同yield return new WaitForSeconds(0.0f) Debug.Log("Hello, I am Coroutine1."); }
运行结果为:
IEnumerator Start() { Debug.Log ("Enter Start()"); StartCoroutine(Coroutine1()); yield return null; StopAllCoroutines(); } IEnumerator Coroutine1() { Debug.Log("Coroutine1 Enter."); yield return new WaitForSeconds(1.0f); Debug.Log("Hello, I am Coroutine1."); }
运行结果为:
当然,Unity也提供让更改立即生效的接口,如DestroyImmediate(Object obj),会立即生效,此语句后,再访问这个obj会报错。
以上内容基于https://blog.csdn.net/u010153703/article/details/38824521整理而成。
相关文章推荐
- Tip8:Unity中诸如 Awake() Start() Update()等函数的 执行顺序
- Unity事件函数的执行顺序 例如:Awake()/OnEnable()/Start()/FixedUpdate()/Update()/等
- unity脚本自带函数执行顺序
- 如何在Linux ELF格式的文件(可执行binary,以及so文件)中定位到对应的函数位置
- SQL server如何定时执行SQl脚本(函数)
- 如何设置WebView使得加载网页时能够执行JavaScript脚本中的alert()函数
- Android系统Recovery工作原理之使用update.zip升级过程分析(九)---updater-script脚本语法简介以及执行流程【转】
- Unity初始化函数以及退出函数的执行顺序
- VB.net | 如何在程序中执行用户脚本以及进行代数式计算
- Unity脚本自带函数的执行顺序
- Android系统Recovery工作原理之使用update.zip升级过程---updater-script脚本语法简介以及执行流程
- Linux系列教程(二十)——Linux的shell概述以及如何执行脚本
- Android系统Recovery工作原理之使用update.zip升级过程分析(九)---updater-script脚本语法简介以及执行流程
- Android系统Recovery工作原理之使用update.zip升级过程分析(九)---updater-script脚本语法简介以及执行流程
- 认识DOM和DOM加载过程以及如何让DOM加载完成后再执行js脚本文件
- Unity中同一个物体上加载的脚本的执行顺序以及点击Button时触发事件的顺序
- Android系统Recovery工作原理之使用update.zip升级过程分析(九)---updater-script脚本语法简介以及执行流程
- Unity后台是如何执行脚本的
- 禁用脚本仅仅是禁用了awake(),start(),update(),fixedupdate(),ongui()五个函数,其它的还是有效的
- [Unity]时间管理脚本Update函数只调用一次相关的函数