Unity3d crash after switching scene containing webcam

I have two scenes that I want to switch between: scene A and B Scene A is the default scene. Scene B is a scene containing a 3d plane that has a default texture, like webcameTexture, and this is it script:

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");
        }

    }
}

}

when I try to switch to scene A, my application crashes again

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

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

    }

How do I fix this problem? I was looking for a forum of unity, and I did not have a suitable solution for this

Edit

i changed my script to the following:

 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");
}

this work, but the switch is too slow, it took a second to switch back to scene A

+4
source share
2 answers

( Stop(), Pause 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");
}
0

- , . .

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");
}

, , .

0

All Articles