c# - 如何在 Windows Phone 上统一减少 WebCameraTexture 使用的内存?

标签 c# unity3d camera windows-phone

我在 unity3d 中为 Windows Phone 平台制作了一个带有两个简单 View 的演示应用程序。在第一个 View 中,我有一个按钮和一个文本,从检查器中我分配给按钮一个事件(单击时)以打开第二个 View 。在这个 View 中,我在面板中有一个原始图像,用于将 mainTexture 分配给 webCamTexture 以在手机上启动相机。

var webCamTexture = new WebCamTexture();
rawImage.material.mainTexture = webCamTexture;
webCamTexture.Play();

在第二个 View 中,我有一个按钮,我可以在其中关闭相机并显示第一个 View (关闭当前 View )webCameraTexture.Stop();

如果我多次这样做,我手机上的 Play() 和 Stop() 内存将如下所示:

enter image description here

我怎样才能清除内存,当我停止相机时,因为有时会给我一个错误“没有足够的存储来完成这个操作”并退出应用程序。

代码开始停止摄像头:

    //call onClick Button (next)
    public void StartMyCamera()
    {
        webCamTexture = new WebCamTexture();
        rawImage.material.mainTexture = webCamTexture;
        webCamTexture.Play();
    }
    //call onClick btn (back - close camera)
    public void StopMyCamera()
    {
        //to stop camera need only this line
        webCamTexture.Stop();
        //----try to clear 
        /*GL.Clear(false, true, Color.clear);
        GC.Collect();
        GC.WaitForPendingFinalizers();
        rawImage.StopAllCoroutines();*/
        //----
    }

Second test

Try to clean Cache

最佳答案

目前您正在播放视频:

var webCamTexture = new WebCamTexture();
rawImage.material.mainTexture = webCamTexture;
webCamTexture.Play();

并用

停止
webCameraTexture.Stop();

这正是您的代码告诉它要做的事情。 new WebCamTexture() 代码行预计在每次调用时分配内存。您应该在 Start() 函数中仅执行一次,然后您可以播放停止 相机没有内存分配。

public RawImage rawImage;
WebCamTexture webCamTexture;

void Start()
{
    intCam(); //Do this once. Only once
}

void intCam()
{
    webCamTexture = new WebCamTexture();
    rawImage.material.mainTexture = webCamTexture;
}

public void StartMyCamera()
{
    webCamTexture.Play();
}

public void StopMyCamera()
{
    //to stop camera need only this line
    webCamTexture.Stop();
}

关于c# - 如何在 Windows Phone 上统一减少 WebCameraTexture 使用的内存?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/39659137/

相关文章:

unity3d - Unity Raycast2D ScreenPointToRay给出错误

c# - AI 在跟随航路点 UNITY 时面临奇怪的方向

c# - 如何通过索引总结 List<int[]> 或锯齿状数组的每个数组?

c# - 即使在处置后,FilesystemWatcher 也会引发

c# - 在音频文件中查找音频样本(频谱图已存在)

c# - 凝视,使用 GoogleVR 在 Unity 中显示工具提示

c# - 类型 'DefaultInlineConstraintResolver' 的内联约束解析器无法解析以下内联约束 : 'apiVersion'

c++ - XIMEA:openCV 2.4.9 无法找到 Ximea 相机,尽管它可以与其他程序一起正常工作

opencv - Unity3d 眼动追踪

c++ - 编写 LookAt 函数