簡體   English   中英

協程不會停止Unity

[英]Coroutine will not stop Unity

我可以選擇一些按鈕,而360視頻將取決於您選擇的按鈕。 我希望用戶在下一個視頻播放之前,必須在按鈕上進行5秒鍾的射線廣播。

這似乎工作正常,但是當射線不在按鈕上時,我需要協程停止。 當射線不在正確的菜單項上時,我試圖停止協程,但是它仍然繼續。 到目前為止,這是我嘗試過的:

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


    }

此外,自從實施此操作以來,下一個視頻播放之前似乎需要很長時間的停頓,並且似乎很滯后。 有什么辦法可以改善這一點?

因為您是在Update函數中調用協程的,所以您可能會在按鈕上的Raycast的每一幀都調用它。 問題是StopCoroutine僅停止使用給定名稱的第一個協程。 因此,您已經開始的所有其他內容繼續運行。

要解決此問題,除了光線投射,還要放一個布爾值以檢查協程是否已經啟動。

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;

}

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM