gpt4 book ai didi

c# - 切换包含网络摄像头纹理的场景后 Unity3d 崩溃

转载 作者:行者123 更新时间:2023-11-30 12:58:41 26 4
gpt4 key购买 nike

我有两个场景想在它们之间切换:场景 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

最佳答案

如果您尝试在停止网络摄像头的同时更改场景,您的游戏将会崩溃。就我而言,我遇到了内存冲突。

comm="UnityMain" reason="memory violation" 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");
}

如您所见,我只是在更改场景之前暂停了相机。

关于c# - 切换包含网络摄像头纹理的场景后 Unity3d 崩溃,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/28793306/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com