繁体   English   中英

如何在统一中调用此IEnumerator c#

[英]How do I call this IEnumerator in unity c#

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

在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.

        }
    }
}

使用协程的第一种方式:

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

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM