C# 如何缩短合作路线?
我编写了一个协作例程,在每一帧都进行批处理,启动它没有问题,但我希望在满足特定条件时停止执行 例如:C# 如何缩短合作路线?,c#,unity3d,coroutine,C#,Unity3d,Coroutine,我编写了一个协作例程,在每一帧都进行批处理,启动它没有问题,但我希望在满足特定条件时停止执行 例如: public IEnumerator workCoroutine(){ int i = 0; if(GLOBAL_LOCK){ return; //will not work here. //we will not execute this coroutine further more, ie,shortcut it; //but how to
public IEnumerator workCoroutine(){
int i = 0;
if(GLOBAL_LOCK){
return; //will not work here.
//we will not execute this coroutine further more, ie,shortcut it;
//but how to ?
}
while( i<=1000){
doingSomeWorkHere(i);
i++;
yield return null;
}
}
public IEnumerator workCoroutine(){
int i=0;
if(全局锁定){
return;//在这里不起作用。
//我们将不再进一步执行此协同程序,即缩短它;
//但是怎么做呢?
}
while(i您需要检查while循环中的全局锁,如下所示-
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class TestCoroutine : MonoBehaviour {
private bool GLOBAL_LOCK = true;
public IEnumerator workCoroutine()
{
int i = 0;
while (i <= 10)
{
if (GLOBAL_LOCK)
{
Debug.Log(" I am in if condition");
Debug.Log(" will exit the coroutine");
yield break;
}else{
Debug.Log(i);
i++;
yield return null;
}
}
}
// Use this for initialization
void Start () {
StartCoroutine(workCoroutine());
}
}
使用系统集合;
使用System.Collections.Generic;
使用UnityEngine;
公共类TestCorroutine:MonoBehavior{
private bool GLOBAL_LOCK=true;
公共IEnumerator工作协同程序()
{
int i=0;
(i在@MukeshSaini旁边,您还可以使用函数停止执行协同程序
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class TestCoroutine : MonoBehaviour {
private bool GLOBAL_LOCK = true;
// keep a copy of the executing script
private IEnumerator coroutine;
public IEnumerator workCoroutine()
{
int i = 0;
while (i <= 10)
{
Debug.Log(i);
i++;
yield return null;
}
}
// Use this for initialization
void Start () {
StartCoroutine(workCoroutine());
}
void Update() {
if (GLOBAL_LOCK )//your specific condition
{
StopCoroutine(coroutine);
}
}
}
使用系统集合;
使用System.Collections.Generic;
使用UnityEngine;
公共类TestCorroutine:MonoBehavior{
private bool GLOBAL_LOCK=true;
//保留执行脚本的副本
私人IEnumerator合作项目;
公共IEnumerator工作协同程序()
{
int i=0;
虽然(i)两个代码都是错误的。即使您认为正在工作的第二个代码也不正确。如果while循环正在运行并且GLOBAL_LOCK
变为true,它甚至不会检测到它,因为If
语句位于while
循环之外,并且只会检查一次。就像您的其他问题一样,请在发布i之前测试您的脚本t在你的问题中,并声称它正在做它实际上没有做的事情。这可能会导致许多人留下错误的答案和来回的评论。如果全局锁定为真,你想退出该函数,对吗?当全局锁定变为真时,第二秒不会退出,而它仍然在while循环中。它不应该像你一样工作你在说。if语句应该在while循环中。我的评论是告诉你,第二个代码不应该像你所说的那样工作“为什么当GLOBAL\u LOCK
为真时,第二个代码段不会退出”——因为第二个代码段永远不会退出。更好的问题是“为什么当GLOBAL\u LOCK
为false时,第二个代码段不会退出?”,因为它不会退出,并且不会因为与第一个代码段不退出相同的原因退出。如果您希望方法退出,它需要检查循环的每个迭代,例如GLOBAL\u LOCK
(!GLOBAL_LOCK&&i,但它仅在循环索引达到10(或1000)时退出协程)在您最初的示例中。您的问题似乎是询问如何让它提前退出,而您最初提供的代码片段和编辑的版本中都没有这样的问题。如果您不是询问如何使用GLOBAL\u LOCK
标志使协程提前退出,那么我不知道您在问什么。您的问题不清楚,也不清楚eds有待改进。@armnotstrong请看Mukesh的答案。我本可以留下一个答案,但我不认为你会从中吸取教训。我相信你所做的是发布一些随机代码,并期望人们为你编写代码。我建议你学习基本循环结构,否则你会有困难。最后,确保你的代码与say之前所做的一样ore发帖。如果你不这样做,评论部分会很长。你知道你可以接受答案吗?我想你应该开始这样做。穆克什的答案应该可以解决你的问题。嗨,谢谢你的回答,所以收益中断
将打破进一步执行的协同程序循环,对吗?是的,收益中断
将停止进一步执行coroutine.:)谢谢,刚刚测试过,这就是我想要的。:DFurthermore,如果我把屈服中断;
放在之外,而循环(只是把它放在返回的位置;
在第一个代码段中),比如if(全局锁){yield break;}
,不会有任何问题吧?因为评论中的人真的让我困惑:这会很好,但行为会有所不同。它只会在协同路由开始时检查一次全局锁定,如果全局锁定在当时为false,那么while循环将开始执行,直到
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class TestCoroutine : MonoBehaviour {
private bool GLOBAL_LOCK = true;
public IEnumerator workCoroutine()
{
int i = 0;
while (i <= 10)
{
if (GLOBAL_LOCK)
{
Debug.Log(" I am in if condition");
Debug.Log(" will exit the coroutine");
yield break;
}else{
Debug.Log(i);
i++;
yield return null;
}
}
}
// Use this for initialization
void Start () {
StartCoroutine(workCoroutine());
}
}
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class TestCoroutine : MonoBehaviour {
private bool GLOBAL_LOCK = true;
// keep a copy of the executing script
private IEnumerator coroutine;
public IEnumerator workCoroutine()
{
int i = 0;
while (i <= 10)
{
Debug.Log(i);
i++;
yield return null;
}
}
// Use this for initialization
void Start () {
StartCoroutine(workCoroutine());
}
void Update() {
if (GLOBAL_LOCK )//your specific condition
{
StopCoroutine(coroutine);
}
}
}