Hello, Welcome to Microsoft Q&A,
The script of the PhotoCapture method can indeed cause a memory leak.
Texture2D targetTexture = new Texture2D(cameraResolution.width, cameraResolution.height);
You created new Texture2D in each iteration of the while loop. This is very expensive, and it will not be freed by Unity. You only need to create one Texture2D then re-use it. Make the targetTexture variable a global variable then initialize it once in the Start function.
Texture2D targetTexture;
// Start is called before the first frame update
void Start()
{
Resolution cameraResolution = PhotoCapture.SupportedResolutions.OrderByDescending((res) => res.width * res.height).First();
targetTexture = new Texture2D(cameraResolution.width, cameraResolution.height);
Coroutine coro = StartCoroutine(Play());
}
In the OnCapturedPhotoToMemory function, it should look something like this:
void OnCapturedPhotoToMemory(PhotoCapture.PhotoCaptureResult result, PhotoCaptureFrame photoCaptureFrame)
{
if (result.success)
{
photoCaptureFrame.UploadImageDataToTexture(targetTexture);
rawImage.texture = targetTexture;
}
// Clean up
photoCaptureObject.StopPhotoModeAsync(OnStoppedPhotoMode);
}
If the response is helpful, please click "Accept Answer" and upvote it.
Note: Please follow the steps in our documentation to enable e-mail notifications if you want to receive the related email notification for this thread.