切换包含webcamtexture的场景后Unity3d崩溃

时间:2015-03-01 12:03:55

标签: c# unity3d unityscript

我有两个想要在它们之间切换的场景:场景A和B. 场景A是默认场景场景B是包含3d平面的场景,其默认纹理为webcameTexture,这是它的脚本:

using UnityEngine;
using UnityEngine.UI;
using System.Collections;

public class Camera_panel_script : MonoBehaviour {

// Use this for initialization
public string deviceName;
 WebCamTexture webCameraTexture;
private WebCamDevice[] devices;

// Use this for initialization

IEnumerator Start() {

    yield return Application.RequestUserAuthorization(UserAuthorization.WebCam | UserAuthorization.Microphone);
    if (Application.HasUserAuthorization(UserAuthorization.WebCam | UserAuthorization.Microphone)) {
        WebCamDevice[] devices = WebCamTexture.devices;

        foreach(WebCamDevice cam in devices)
        {
            if(cam.isFrontFacing )
            {    
                webCameraTexture  =    new WebCamTexture(cam.name);
                webCameraTexture.deviceName  = cam.name;
                //if (webCameraTexture != null && webCameraTexture.didUpdateThisFrame) {
                renderer.material.mainTexture = webCameraTexture;
                webCameraTexture.Play();
                //}

                break;
            }
        }
    } else {
    }
}
void Update () {
    if (Input.GetKey (KeyCode.Escape)) {
        if(webCameraTexture!=null && webCameraTexture.isPlaying){
            webCameraTexture.Stop();

        }
        if(!webCameraTexture.isPlaying)
        {
            DestroyObject(gameObject);
            Application.LoadLevel("Game");
        }

    }
}

}

当我再次尝试切换到场景A时,我的应用程序崩溃

if (Input.GetKey (KeyCode.Escape)) {
        if(webCameraTexture!=null && webCameraTexture.isPlaying){
            webCameraTexture.Stop();

        }
        if(!webCameraTexture.isPlaying)
        {
            DestroyObject(gameObject);
            Application.LoadLevel("Game");
        }

    }

我该如何解决这个问题?我搜索了团结论坛,我无法找到适当的解决方案

修改

我改变了我的脚本:

 if (Input.GetKey (KeyCode.Escape)) {
    while (webCameraTexture!=null && webCameraTexture.isPlaying)
        {
            Debug.Log("is still playing");
            webCameraTexture.Stop();
            webCameraTexture=null;
            break;
        }
        Debug.Log("stoped playing");
        Application.LoadLevel("Game");
}

这项工作但切换速度太慢,只需一秒钟再次切换到场景A

2 个答案:

答案 0 :(得分:0)

固定代码(您不应该调用Stop(),只需调用Pause and Change Scene:

if (Input.GetKey (KeyCode.Escape)) 
{
    while (webCameraTexture!=null && webCameraTexture.isPlaying)
    {
        Debug.Log("is still playing");
        webCameraTexture.Pause();
        webCameraTexture=null;
        break;
    }

    Debug.Log("stoped playing");
    Application.LoadLevel("Game");
}

答案 1 :(得分:0)

如果您尝试停止Webcamera并在游戏崩溃的同时更改场景。 在我的情况下,我得到了内存违规。

  

COMM =" UnityMain" reason ="内存违规" SIG = 11'

改善您的解决方案将是一个更好的代码:

if (Input.GetKey (KeyCode.Escape)) 
{
    if (webCameraTexture != null && webCameraTexture.isPlaying)
    {
        Debug.Log("Camera is still playing");
        webCameraTexture.Pause();     

        while (webCameraTexture.isPlaying)
        {
            yield return null;
        }

        Debug.Log("Camera stopped playing");
    }

    webCameraTexture = null;
    Application.LoadLevel("Game");
}

正如您所看到的,我只是在更改场景之前暂停相机。