【发布时间】:2021-11-12 01:33:28
【问题描述】:
我正在使用 unity 的 Photocapture Object 拍摄带有 hololens 的视频。 我根据官方的示例代码写了代码,但是我在更新函数中改变了过程,以获取多张图片而不是一张。
https://docs.unity3d.com/2018.4/Documentation/Manual/windowsholographic-photocapture.html
但是,当我运行此代码时,它最终会用完 pagefile.sys、内存不足并中止。 我搜索并发现大多数内存泄漏是由纹理 2d 引起的,但是在这段代码中,即使我省略了它们,它们也会发生。此外,在执行代码后,统一分析器不会显示内存使用量的任何逐渐增加。 可能是什么原因?如果您能告诉我,我将不胜感激。
using UnityEngine;
using System;
using System.Linq;
using UnityEngine.XR;
using UnityEngine.Windows.WebCam;
using System.Threading.Tasks;
using UnityEngine.Networking;
using System.Text;
using System.IO;
using System.Net;
using System.Collections;
using System.Collections.Generic;
public class photocap : MonoBehaviour
{
PhotoCapture PhotoCapture = null;
Texture2D targetTexture = null;
Resolution cameraResolution;
Renderer quadRenderer;
float dt = 0;
void Start()
{
cameraResolution = PhotoCapture.SupportedResolutions.OrderByDescending((res) => res.width * res.height).First();
GameObject quad = GameObject.CreatePrimitive(PrimitiveType.Quad);
quad.transform.localScale = new Vector3(0.2f, 0.2f, 0.2f);
quadRenderer = quad.GetComponent<Renderer>() as Renderer;
quadRenderer.material = new Material(Shader.Find("Unlit/Texture"));
quad.transform.parent = this.transform;
quad.transform.localPosition = new Vector3(0.0f, 0.0f, 0.3f);
}
async void StartCapture()
{
PhotoCapture.CreateAsync(false, delegate (PhotoCapture captureObject)
{
PhotoCapture = captureObject;
CameraParameters cameraParameters = new CameraParameters();
cameraParameters.hologramOpacity = 0.0f;
cameraParameters.cameraResolutionWidth = cameraResolution.width;
cameraParameters.cameraResolutionHeight = cameraResolution.height;
cameraParameters.pixelFormat = CapturePixelFormat.BGRA32;
PhotoCapture.StartPhotoModeAsync(cameraParameters, delegate (PhotoCapture.PhotoCaptureResult result)
{
PhotoCapture.TakePhotoAsync(OnCapturedPhotoToMemory);
});
});
}
void OnCapturedPhotoToMemory(PhotoCapture.PhotoCaptureResult result, PhotoCaptureFrame photoCaptureFrame)
{
PhotoCapture.StopPhotoModeAsync(OnStoppedPhotoMode);
}
void OnStoppedPhotoMode(PhotoCapture.PhotoCaptureResult result)
{
PhotoCapture.Dispose();
PhotoCapture = null;
}
void Update()
{
dt += Time.deltaTime;
if (dt > 3)
{
dt = 0.0f;
StartCapture();
}
}
}
【问题讨论】:
标签: c# unity3d out-of-memory hololens