简体   繁体   English

协程仅发射一次

[英]Coroutine only firing once

I'm trying to run a script that will generate a "ramps" upon touch between point a and b. 我正在尝试运行一个脚本,该脚本在点a和b之间接触时会生成“斜坡”。 This code receives a list of where the elements of the ramps should be and then instanciates and places them on the screen. 该代码接收到应将斜坡元素放置在何处的列表,然后实例化它们并将其放置在屏幕上。

However the coroutine is only running once and I can't understand why. 但是协程只运行一次,我不明白为什么。 Can anyone give me some advice? 谁能给我一些建议?

Thank you very much in advance 提前非常感谢你

public IEnumerator CreateRamp(List<Vector3> lP, float angle)
{

    int i = 1;
    while (i <= lP.Count)
    {
        Debug.Log("Iteration " + i + " of " + lP.Count + " position is " + lP[i]);
        GameObject item = Instantiate(Resources.Load("floor")) as GameObject;


        item.transform.position = current_Position;
        item.transform.eulerAngles = new Vector3(0, 0, UnityEngine.Random.Range(0f, 360f));

        item.GetComponent<Ramp>().strtPos = item.transform.position;
        item.GetComponent<Ramp>().strtRot = item.transform.eulerAngles;
        item.GetComponent<Ramp>().strtScale = new Vector3(0.4f, 0.4f, 1);

        item.GetComponent<Ramp>().tgtRot = new Vector3(0, 0, angle);
        item.GetComponent<Ramp>().tgtPos = lP[i-1];
        i += 1;
        yield return new WaitForSeconds(0.2f);
    }
}

I suspect your condition i <= lP.Count is true only once. 我怀疑您的条件i <= lP.Count仅是一次。 (Maybe lP.Count == 1 , I think). (也许我认为lP.Count == 1 )。

The way co-routine works is that, the code inside the CreateRamp function is executed across multiple frames. 协同例程的工作方式是,跨多个帧执行CreateRamp函数中的代码。

When you StartCoroutine(CreateRamp(...)) , it is immediately run until it hits yield statement. 当您StartCoroutine(CreateRamp(...)) ,它将立即运行,直到命中yield语句。 It will wait there for 0.2 seconds and will be run again from the statement right after the yield. 它将在那里等待0.2秒,并在收益率之后立即从语句再次运行。

In the second execution, it evaluates the condition i <= lP.Count again and see that it is False => it jumps out of the loop and because it hits the end of the function, that co-routine will be stopped, no more execution in the future. 在第二次执行中,它再次评估条件i <= lP.Count并发现它为False =>它跳出了循环,并且由于它到达了函数的末尾,因此该联合例程将被停止,不再将来执行。

Since this function is an IEnumerable it should be treated by other code as a list of Ramp objects. 由于此函数是IEnumerable,因此其他代码应将其视为Ramp对象的列表。 I suspect (there isn't enough of your code to know), that the way you are calling this function is incorrect. 我怀疑(您的代码不足以了解),您调用此函数的方式不正确。

On a side note, with your yield returning a waitforX, It would be better in the long term to either perform the wait outside of this function (where you are calling it from) or at the very least add the wait period as a parameter to the function. 附带一提,从收益中返回一个waitforX,从长远来看,最好在此函数之外(从中调用它)执行等待,或者至少将等待时间作为参数添加到功能。 Hard coded values like that will end up biting you later on, especially if your game's code-base grows. 这样的硬编码值最终会在以后给您带来困扰,尤其是当您的游戏的代码库不断增长时。 I recommend it be exposed as a setting on your GameObject, so it can be tweaked from the editor. 我建议将其作为GameObject上的设置公开,因此可以从编辑器中进行调整。

One other thing, How do you destroy these Ramp objects when you are done with them? 另一件事,当用完这些斜坡对象后,如何销毁它们? It might be good to consider storing references to them as you create them so you can destroy them later. 创建它们时最好考虑存储对它们的引用,以便以后可以销毁它们。

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

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