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 {
public string deviceName;
WebCamTexture webCameraTexture;
private WebCamDevice[] devices;
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;
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
source
share