【问题标题】:How do I call this IEnumerator in unity c#如何在统一 c# 中调用此 IEnumerator
【发布时间】:2016-03-01 18:17:20
【问题描述】:

所以我试图通过我的 PTick 变量每 3 秒增加一次总资源我尝试通过 IEnumerator 使用它并在 start 方法中调用它,但它只运行一次,所以我在更新中尝试了它,它运行为尽快。有什么办法可以让它每 3 秒运行一次。我很乐意尝试替代方案,只要我可以让它每 3 秒运行一次。

这是我正在尝试的脚本

using UnityEngine;
using System.Collections;

public class Resources : MonoBehaviour {

private int foodPtick;
private int PowerPtick;
private int HappinessPtick;
private int MoneyPtick;
private int PopulationPtick;

public int foodTotal;
public int PowerTotal;
public int HappinessTotal;
public int MoneyTotal;
public int PopulationTotal;

void Start () {
    StartCoroutine(ResourceTickOver(3.0f));
}

void Update () {

}

IEnumerator ResourceTickOver(float waitTime){
    yield return new WaitForSeconds(waitTime);
    foodTotal += foodPtick;
    PowerTotal += PowerPtick;
    HappinessTotal += HappinessPtick;
    MoneyTotal += MoneyPtick;
    PopulationTotal += PopulationPtick;
    Debug.Log("Resources" + "food" + foodTotal + "power" + PowerTotal + "Happiness" + HappinessTotal + "Money" + MoneyTotal + "Population" + PopulationTotal);

}

public void ChangeResource(int food,int power, int happy, int money,int pop)
{
    Debug.Log("Old Per Tick" + "food" + foodPtick + "power" + PowerPtick + "Happiness" + HappinessPtick + "Money" + MoneyPtick + "Power" + PopulationPtick);
    foodPtick += food;
    PowerPtick += power;
    HappinessPtick += happy;
    MoneyPtick += money;
    PopulationPtick += pop;
    Debug.Log("New Per Tick" + "food" + foodPtick + "power" + PowerPtick + "Happiness" + HappinessPtick + "Money" + MoneyPtick + "Power" + PopulationPtick);
}

【问题讨论】:

  • 您看过 WaitForSeconds Yield 指令吗? docs.unity3d.com/ScriptReference/WaitForSeconds.html
  • 你好安德鲁。只需使用InvokeRepeating。所以,InvokeRepeating( "YourFunction", .1f, 3f); 这就是它的全部内容。不要使用协程。 (除了极其高级的情况外,几乎没有理由在 Unity 中使用协程)。要取消调用,只需 CancelInvoke .. 检查简单文档
  • 不要使用协程,因为它们会从 c# 开发人员的角度教坏习惯,并且很可能会导致常规 c# 工作中的私刑

标签: c# unity3d ienumerator


【解决方案1】:

看起来你在 Start 中调用 Coroutine 后并没有保持它的活力。对 StartCoroutine 的调用将执行一次然后返回,因此如果没有某种循环,您将只能调用一次协程主体。

将 yield 语句包含在一个循环中将为您提供每个指定的 waitTime 一次迭代。在下面的示例中,您可以看到控制台每 3 秒记录一次时间戳更新。

using UnityEngine;
using System.Collections;

public class NewBehaviourScript : MonoBehaviour {

    // Use this for initialization
    void Start () {
        StartCoroutine(ResourceTickOver(3.0f));
    }

    // Update is called once per frame
    void Update () {

    }

    IEnumerator ResourceTickOver(float waitTime)
    {
        while (true) // Do this as long this script is running.
        {
            print (Time.time);
            yield return new WaitForSeconds(waitTime);
            print (Time.time);

            // Update Resources inside this loop or call something that will.

        }
    }
}

【讨论】:

    【解决方案2】:

    使用协程的第一种方式:

    void Start () {
        StartCoroutine(ResourceTickOver(3.0f));
    }
    
    IEnumerator ResourceTickOver(float waitTime){
        while(true){
           yield return new WaitForSeconds(waitTime);
           foodTotal += foodPtick;
           PowerTotal += PowerPtick;
           HappinessTotal += HappinessPtick;
           MoneyTotal += MoneyPtick;
           PopulationTotal += PopulationPtick;
           Debug.Log("Resources" + "food" + foodTotal + "power" + PowerTotal + "Happiness" + HappinessTotal + "Money" + MoneyTotal + "Population" + PopulationTotal);
       }
    }
    

    第二种方式使用更新:

    void Update(){
        timer += Time.deltaTime;
        if(timer > waitTime){
           timer = 0f;
           foodTotal += foodPtick;
           PowerTotal += PowerPtick;
           HappinessTotal += HappinessPtick;
           MoneyTotal += MoneyPtick;
           PopulationTotal += PopulationPtick;
           Debug.Log("Resources" + "food" + foodTotal + "power" + PowerTotal + "Happiness" + HappinessTotal + "Money" + MoneyTotal + "Population" + PopulationTotal);
       }
    }
    

    InvokeRepeating 的第三种方式:

    void Start(){
        InvokeRepeating("Method", 3f, 3f);
    }
    
    void Method(){
         foodTotal += foodPtick;
         PowerTotal += PowerPtick;
         HappinessTotal += HappinessPtick;
         MoneyTotal += MoneyPtick;
         PopulationTotal += PopulationPtick;
         Debug.Log("Resources" + "food" + foodTotal + "power" + PowerTotal + "Happiness" + HappinessTotal + "Money" + MoneyTotal + "Population" + PopulationTotal);
    }
    

    【讨论】:

      猜你喜欢
      • 2011-10-18
      • 1970-01-01
      • 2013-10-05
      • 1970-01-01
      • 2019-10-27
      • 2020-10-04
      • 1970-01-01
      • 2017-05-06
      • 2012-07-13
      相关资源
      最近更新 更多