2017-11-06 16 views
0

私はボタンの選択肢を持っており、360ビデオは選択するボタンによって異なります。私は、次のビデオが再生される前に、ユーザーがボタンで5秒間レイキャストをしなければならないようにします。コルーチンはUnityを停止しません

これは問題なく動作しているようですが、光線がボタンにないときにはコルーチンを停止する必要があります。光線が正しいメニュー項目にないときにコルーチンを停止しようとしましたが、それはまだ続きます。これは私がこれまでにしようとしているものです。また

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


    } 

、これを実装しているので、そこに次のビデオを再生する前に、長い休止のようですし、それは非常にラグようです。これを改善する方法はありますか?

答えて

1

更新機能でコルーチンを呼び出すので、あなたの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; 

} 
+0

注:「alreadyStarted = false;」と入力することもできます。 "if(Physics.Raycast(ray、out hit))"の 'else'ステートメントでは、 –

関連する問題