我有一个选择按钮和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();
}而且,由于实现了这一点,在下一个视频播放之前似乎有很长时间的停顿,而且似乎相当滞后。有什么办法可以改善这种情况吗?
发布于 2017-11-06 18:59:42
因为您正在更新函数中调用coroutine,所以您可能会调用Raycast在按钮上的每一个帧。问题是,StopCoroutine只停止具有给定名称的第一个协同线。所以你开始的所有其他的都继续跑。
要解决这个问题,除了射线广播之外,还可以使用一个布尔值来检查您的Coroutine是否已经启动。
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;
}https://stackoverflow.com/questions/47142723
复制相似问题