【问题标题】:Stop co-routine with a toggle button使用切换按钮停止协同程序
【发布时间】:2017-03-25 18:44:10
【问题描述】:

我有一个协同程序,当切换按钮的布尔值改变时触发,当布尔值再次改变时,应该停止协同程序并启动另一个。这是我的代码:

    public class thermoPowerControlPanel : MonoBehaviour {

    private ThermoElectric thermo;

    public bool toggleBool1;
    public int temperature;
    private int tempUp = 10;
    private int tempDown = 1;


    public thermoPowerControlPanel (){
        temperature = 100;
    }


    public void turbine1State (bool toggleBool1) {

        if (toggleBool1 == false) {
            Debug.Log (toggleBool1);
            Invoke("ReduceTemperatureEverySecond", 1f);
        }

        if (toggleBool1 == true) {
            Debug.Log (toggleBool1);
            Invoke("IncreaseTemperatureEverySecond", 1f);
        }
    }


    private void ReduceTemperatureEverySecond()
    {
        if (toggleBool1 == true)
        {
            Debug.Log("I was told to stop reducing the temperature.");
            return;
        }
        temperature = temperature - tempDown;
        Debug.Log (temperature);
            Invoke("ReduceTemperatureEverySecond", 1f);
    }

    private void IncreaseTemperatureEverySecond()
    {
        if (toggleBool1 == false)
        {
            Debug.Log("I was told to stop increasing the temperature.");
            return;
        }
        temperature = temperature + tempUp;
        Debug.Log (temperature);
        Invoke("ReduceTemperatureEverySecond", 1f);
    }
}

当功能turbo1State(bool t1)接收到第一个bool(false)时,程序defineTemperatureEverySecond()开始但它立即停止,发送Debug.Log消息,它应该继续降低温度直到bool(由激活切换按钮)变为真。 .

你能帮忙吗?

【问题讨论】:

  • 搜索关于“停止协程”的 1000 个问题
  • 我认为问题出在 bool if 条件下,我有停止它的代码,但它从未激活
  • 根据 Joe Blow 的建议,我更改了代码以停止使用协同程序。
  • 由于某些未知的原因,当调用 ReduceTemperatureEverySecond() 时,toggleBool1 似乎更改为 true..
  • 嗨@growanimation ..你能用简单的语言解释一下,你想要实现什么那么编写代码很容易

标签: c# unity3d


【解决方案1】:

就这么简单!

 public Toggle tog;  // DONT FORGET TO SET IN EDITOR

Start ...

 InvokeRepeating( "Temp", 1f, 1f );

...然后...

private void Temp()
 {
 if (tog.isOn)
     temperature = temperature + 1;
 else
     temperature = temperature - 1;
 
 // also, ensure it is never outside of 0-100
 temperature = Mathf.Clamp(temperature, 0,100);
 }

如果您需要“完全停止”该操作(向上和向下),只需执行此操作

 CancelInvoke("Temp");

这么简单!


注意仅供参考,我解释的另一种模式是这样的:

 bool some flag;
 Invoke("Temp", 1f);
 private void Temp()
   {
   if (some flag is tripped) stop doing this
   .. do something ..
   Invoke( ..myself again after a second .. )
   }

在现实生活中,“不断调用自己”通常比使用 InvokeRepeating 更好。

在这个简单的示例中,只需使用 InvokeRepeating,然后使用 CancelInvoke。

【讨论】:

  • 但是我不需要再次调用函数本身,因为现在我使用的 InvokeRepeating 正确吗?
  • 是的,这是 10 亿% 的正确率。我只是出于好奇才提到“其他方法”。
【解决方案2】:

你只能通过它的名字来停止协程。

试试StartCoroutine("increaseTemperature"); 然后StopCoroutine("increaseTemperature");

http://docs.unity3d.com/Manual/Coroutines.html

有多种方法可以拨打StartCoroutine。你可以“停止”一个协程如果你用“字符串”方法启动它,像这样StartCoroutine("FunctionNameAsStringHere"); 如果你像这样启动它StartCoroutine(FunctionNameAsStringHere());你不能通过名字来停止它。

(您也可以访问实际的枚举器来停止协程,但这远远超出了使用协程的初学者的范围。)

【讨论】:

  • 我刚做了,但还是不行,我也有同样的问题。我将使用新代码编辑问题。
  • 这个答案不正确。您也可以通过引用Coroutine 对象或当前返回的IEnumerator 来停止协程。检查重载方法MonoBehaviour.StopCoroutine
  • 更新了在您的代码中调用的启动/停止协程,据我所知 - 它可以工作 pastebin.com/k9R5YgFr
  • jezz 你可能对这个微妙的问题感兴趣answers.unity3d.com/answers/1119978/view.html@JerrySwitalski 令人着迷!
猜你喜欢
  • 2013-08-14
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2019-06-25
  • 2013-05-18
相关资源
最近更新 更多