我如何在 unity c# 中调用这个 IEnumerator

How do I call this IEnumerator in unity c#

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

这是我正在尝试运行的脚本

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);
}

第一种使用协程的方式:

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);
}

在“开始”中调用协程后,您似乎没有让协程保持活动状态。对 StartCoroutine 的调用将执行一次 return,因此如果没有某种循环,您将只会调用一次协程主体。

在循环中包含 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.

        }
    }
}