【问题标题】:How can I reduce memory used by WebCameraTexture in unity on windows phone?如何在 Windows 手机上统一减少 WebCameraTexture 使用的内存?
【发布时间】:2016-09-23 11:00:39
【问题描述】:

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

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

在第二个视图中,我有一个按钮,用于关闭相机并显示第一个视图(关闭当前)webCameraTexture.Stop();

如果我多次这样做,我手机上的 Play() 和 Stop() 内存看起来像:

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

代码开始停止相机:

    //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();*/
        //----
    }

【问题讨论】:

  • 在停止纹理时尝试运行垃圾收集器。
  • 我尝试使用垃圾收集器 GC.Collect();但没有释放内存仍然长大
  • 所以这在这里行不通。您应该继续关注 Unity3d 论坛上的帖子,并可能将其报告为错误。
  • 我不认为这是一个错误。 jijie 感谢这次放代码。我在我的窗户上试过这个,没有问题。我确实认为你做错了什么。你能展示你如何启动和停止相机的完整代码吗?
  • 对不起,我更新了我的帖子@Programmer。

标签: c# unity3d camera windows-phone


【解决方案1】:

目前您使用以下方式播放视频:

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

并用

停止它
webCameraTexture.Stop();

这正是您的代码告诉它做的事情。 new WebCamTexture() 代码行应在每次调用时分配内存。您应该在Start() 函数中只执行一次,然后您可以在没有内存分配的情况下使用playstop 相机。

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

【讨论】:

  • 当我在控制台中运行我的应用程序时出现此错误:NullReferenceException:对象引用未设置为对象的实例。在检查器中,RawImage 附加到脚本。
  • 我发布的这段代码对我有用。双击编辑器中的错误消息,它将显示是哪一行代码导致了它。贴出那行代码。请确保您照原样复制此内容。确保在 Start() 函数中的任何内容之前调用 intCam()。告诉我。
  • 这不是我问的。如果您从控制台双击错误,它应该会打开您的脚本编辑器并显示导致该错误的代码行。无论如何,问题在于您的 StartMyCamera 函数。很可能没有调用 webCamTexture。你确定吗?这应该从Start 函数调用。请将intCam() 移动到Awake() 函数中,让我知道会发生什么。如果这不起作用,你从哪里打电话给StartMyCamera?我想看看你的新代码是什么样子的。请在此处发布pastebin.com
  • 认为StartMyCamera()intCam() 函数之前被调用。顺便说一句,我的答案打字很快。 intCam() 应该是 initCam()。那不应该改变任何事情。你的问题现在解决了吗?
  • 很抱歉回复晚了。这个周末我在构建 Windows 手机时遇到了问题。我收到了一些 SDK 错误。我决定今天重新安装我的操作系统并让你知道测试
【解决方案2】:

“Resources.UnloadUnusedAssets()”对您的问题很有帮助。

public void StopMyCamera()
{
webCamTexture.Stop();
Resources.UnloadUnusedAssets();
}

【讨论】:

    猜你喜欢
    • 2011-09-12
    • 2015-03-03
    • 1970-01-01
    • 2012-02-08
    • 1970-01-01
    • 2019-03-25
    • 2013-05-21
    • 2017-11-13
    相关资源
    最近更新 更多