Unity3d 协同程序统一-不可预测的行为

Unity3d 协同程序统一-不可预测的行为,unity3d,coroutine,Unity3d,Coroutine,有人能解释我代码的问题吗 我在项目开始时启动了两个合作项目。这些是: loader = StartCoroutine(loadobjectsfromfile); buffer = StartCoroutine(setBufferToSpesificLocation(0)); 简略说明 loadobjectsfromfile从文件中读取obj并将其添加到列表loadedObject。Ant读取更多数据,直到缓冲区已满 IEnumerator loadobjectsfromfile(string

有人能解释我代码的问题吗

我在项目开始时启动了两个合作项目。这些是:

loader = StartCoroutine(loadobjectsfromfile);
buffer = StartCoroutine(setBufferToSpesificLocation(0));
简略说明 loadobjectsfromfile从文件中读取obj并将其添加到
列表loadedObject
。Ant读取更多数据,直到缓冲区已满

 IEnumerator loadobjectsfromfile(string filepath, List<GameObject> objs)
    {       while(...)
            {
                // other code lines
               // ...
                loadedObject.Add(gam);
                while (full_)
                {
                    yield return null;
                }  
                k++;
                yield return null;
            } }
 IEnumerator setBufferToSpesificLocation(int startPoint)
    {

        for ( foo = startPoint; foo < loadedObject.Count; foo++)
        {

            while (full_)
            {
                yield return null;
            }

            put(loadedObject[foo]);

            yield return null;
        }

    }

 void put(GameObject frame)
    {

        buff_[head_] = frame;
        head_ = (head_ + 1) % bufferSize;
        full_ = head_ == tail_;


    }
好吧,如果我启动前两个协同程序,然后调用最后一个协同程序(没有问题。我的代码运行良好。但是我遇到了一个新案例的问题)

loader = StartCoroutine(loadobjectsfromfile);
buffer = StartCoroutine(setBufferToSpesificLocation(0));
StopCoroutine(buffer);
buffer = StartCoroutine(setBufferToSpesificLocation(50)); // 
play = StartCoroutine(displayMesh(buff_));
问题是,在这种情况下,在调用第二个缓冲区协程后,加载程序不工作。当“
(foo=startPoint;foo
”结束时,加载程序工作


我不明白。我想,如果
full\uu
为false,则加载程序和缓冲区都必须工作,但只有缓冲区在工作,加载程序正在等待“缓冲区等待{}循环”

我不知道为什么要启动协同程序,然后立即停止。当您调用StartRoutine时,它将运行该程序,直到达到产量。将该程序运行到第一个产量后,它将运行启动协同程序的下一行

检查此链接,了解事件函数的执行顺序:

这是关于合作的

因此,在您的情况下,事情将按以下顺序发生:

1:开始例行程序

loader = StartCoroutine(loadobjectsfromfile);
2:运行到第一个产量

 IEnumerator loadobjectsfromfile(string filepath, List<GameObject> objs)
    {       while(...)
            {
                // other code lines
               // ...
                loadedObject.Add(gam);
                while (full_)
                {
                    yield return null;
                }  
                k++;
                yield return null; //<<<<<<<<<<<<<<HERE<<<<<<<<<<<<<<<<<<<<
            } }
 IEnumerator setBufferToSpesificLocation(int startPoint)
    {

        for ( foo = startPoint; foo < loadedObject.Count; foo++)
        {

            while (full_)
            {
                yield return null;
            }

            put(loadedObject[foo]);

            yield return null; //<<<<<<<<<<<<<<HERE<<<<<<<<<<<<<<<<<<<<
        }

    }
4:运行到第一个产量

 IEnumerator loadobjectsfromfile(string filepath, List<GameObject> objs)
    {       while(...)
            {
                // other code lines
               // ...
                loadedObject.Add(gam);
                while (full_)
                {
                    yield return null;
                }  
                k++;
                yield return null; //<<<<<<<<<<<<<<HERE<<<<<<<<<<<<<<<<<<<<
            } }
 IEnumerator setBufferToSpesificLocation(int startPoint)
    {

        for ( foo = startPoint; foo < loadedObject.Count; foo++)
        {

            while (full_)
            {
                yield return null;
            }

            put(loadedObject[foo]);

            yield return null; //<<<<<<<<<<<<<<HERE<<<<<<<<<<<<<<<<<<<<
        }

    }
5:再次启动缓冲区,但现在startPoint参数设置为50

buffer = StartCoroutine(setBufferToSpesificLocation(50));
6:运行到第一个产量???不,foo=startPoint,现在是50

IEnumerator setBufferToSpesificLocation(int startPoint)
    {
        //it will skip the loop as foo is 50 and loadedObject.Count is 1 (probabbly)
        for ( foo = startPoint; foo < loadedObject.Count; foo++) 
        {
            //code...
        }

        //WILL END THE COROUTINE
    }
IEnumerator SetBufferToSpecificLocation(int startPoint)
{
//它将跳过循环,因为foo为50,loadedObject.Count为1(可能)
for(foo=startPoint;foo
我只想跳过一些对象,不要将它们添加到我的缓冲区。在我的第二个例子中,loadedObject.Count=200 buffer=[50…149](设置为50)此时已满,所以buffer和loader不起作用。一步后,由于displayMesh协程,full将为0。新的buffer=[51…149]和full再次为1(循环).这就是我的问题,缓冲区在满_uuu为0时工作,但我的加载程序不工作。我的加载对象数仍然是200。它必须是201,然后是202 bla bla。
IEnumerator setBufferToSpesificLocation(int startPoint)
    {
        //it will skip the loop as foo is 50 and loadedObject.Count is 1 (probabbly)
        for ( foo = startPoint; foo < loadedObject.Count; foo++) 
        {
            //code...
        }

        //WILL END THE COROUTINE
    }