首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >Coroutine不会阻止团结

Coroutine不会阻止团结
EN

Stack Overflow用户
提问于 2017-11-06 17:41:05
回答 1查看 983关注 0票数 0

我有一个选择按钮和360视频将取决于您选择哪个按钮。我希望用户必须在下一个视频播放之前在按钮上进行5秒的光线广播。

这似乎是工作正常,但我需要的协同线停止,当光线不在按钮上。当光线不在正确的菜单项时,我尝试停止协同线,但是它仍然在继续。这就是我迄今尝试过的:

代码语言:javascript
复制
public Coroutine coroutine;

 void Update()
    {

        //create the ray to cast forward
        RaycastHit hit;
        Vector3 origin = transform.position;
        Vector3 direction = transform.forward;
        Ray ray = new Ray(origin, direction);
        Debug.DrawRay(origin, direction * 100, Color.blue);

        if (Physics.Raycast(ray, out hit))
        {
            objectCollided = hit.collider.gameObject.name;
            hasHit = true;

            if (objectCollided == "goForwardCube")
            {
                coroutine = StartCoroutine(WaitAndPrint());
            }
 else if (objectCollided != "goForwardCube")
            {
               StopCoroutine(coroutine);
            }

        }



  IEnumerator WaitAndPrint()
    {

            // suspend execution for 5 seconds
            ButtonControl.forwardCube.GetComponent<MeshRenderer>().material.color = Color.clear;
            forwardText.text = "5";
            yield return new WaitForSeconds(1);
            ButtonControl.forwardCube.GetComponent<MeshRenderer>().material.color = Color.blue;
            forwardText.text = "4";
            yield return new WaitForSeconds(1);
            ButtonControl.forwardCube.GetComponent<MeshRenderer>().material.color = Color.clear;
            forwardText.text = "3";
            yield return new WaitForSeconds(1);
            ButtonControl.forwardCube.GetComponent<MeshRenderer>().material.color = Color.blue;
            forwardText.text = "2";
            yield return new WaitForSeconds(1);
            ButtonControl.forwardCube.GetComponent<MeshRenderer>().material.color = Color.clear;
            forwardText.text = "1";
            yield return new WaitForSeconds(1);
            videoPlayer.url = "Assets/Videos/1(360).mp4";
            ButtonControl.DisableButtons();
            videoPlayer.Play();


    }

而且,由于实现了这一点,在下一个视频播放之前似乎有很长时间的停顿,而且似乎相当滞后。有什么办法可以改善这种情况吗?

EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2017-11-06 18:59:42

因为您正在更新函数中调用coroutine,所以您可能会调用Raycast在按钮上的每一个帧。问题是,StopCoroutine只停止具有给定名称的第一个协同线。所以你开始的所有其他的都继续跑。

要解决这个问题,除了射线广播之外,还可以使用一个布尔值来检查您的Coroutine是否已经启动。

代码语言:javascript
复制
public Coroutine coroutine;
bool alreadyStarted = false;

void Update()
{

    //create the ray to cast forward
    RaycastHit hit;
    Vector3 origin = transform.position;
    Vector3 direction = transform.forward;
    Ray ray = new Ray(origin, direction);
    Debug.DrawRay(origin, direction * 100, Color.blue);

    if (Physics.Raycast(ray, out hit))
    {
        objectCollided = hit.collider.gameObject.name;
        hasHit = true;

        if (objectCollided == "goForwardCube" && !alreadyStarted)
        {
            alreadyStarted = true;
            coroutine = StartCoroutine(WaitAndPrint());
        }
        else if (objectCollided != "goForwardCube")
        {
           alreadyStarted = false;
           StopCoroutine(coroutine);
        }
    }
    else
    {
        alreadyStarted = false;
    }
}


IEnumerator WaitAndPrint()
{

        // suspend execution for 5 seconds
        ButtonControl.forwardCube.GetComponent<MeshRenderer>().material.color = Color.clear;
        forwardText.text = "5";
        yield return new WaitForSeconds(1);
        ButtonControl.forwardCube.GetComponent<MeshRenderer>().material.color = Color.blue;
        forwardText.text = "4";
        yield return new WaitForSeconds(1);
        ButtonControl.forwardCube.GetComponent<MeshRenderer>().material.color = Color.clear;
        forwardText.text = "3";
        yield return new WaitForSeconds(1);
        ButtonControl.forwardCube.GetComponent<MeshRenderer>().material.color = Color.blue;
        forwardText.text = "2";
        yield return new WaitForSeconds(1);
        ButtonControl.forwardCube.GetComponent<MeshRenderer>().material.color = Color.clear;
        forwardText.text = "1";
        yield return new WaitForSeconds(1);
        videoPlayer.url = "Assets/Videos/1(360).mp4";
        ButtonControl.DisableButtons();
        videoPlayer.Play();

        alreadyStarted = false;

}
票数 1
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/47142723

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档