如何在 unity c# 中称呼此 IEnumerator

本文关键字:IEnumerator unity | 更新日期: 2023-09-27 18:36:59

所以我正在尝试通过我的 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);
}

如何在 unity c# 中称呼此 IEnumerator

在"开始"中调用协程后,您似乎没有保持协程处于活动状态。对 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.
        }
    }
}

使用协程的第一种方法:

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

第三种方式与调用重复:

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