简体   繁体   中英

Unity3d crash after switching a scene containing a webcamtexture

I have two scene that i want to switch between them : Scene A and B Scene A is the default scene Scene B is the scene containing a 3d plane having the default texture as webcameTexture and this is it's 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 again my application crashes

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

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

    }

How should i fix this problem ? i search out the unity forum and i couldn't have a proper solution for that

Edit

i changed my script to this :

 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 switching is too slow ,it took like a second to switch to scene A again

Fixed code (you shouldn't call Stop(), just call 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");
}

If you try to Stop the Webcamera and change the scene at the same time your game will crash. In my case I was getting a memory violation.

comm="UnityMain" reason="memory violation" sig=11'

Improving your solution a better code will be:

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

As you can see I'm only pausing the camera before change the scene.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM