您的位置:首页 > 移动开发 > Unity3D

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函数开始执行之前。因此,可以看到:     
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整理而成。
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: 
相关文章推荐