From 249121e7753584dfb846f273e3d83db5aa03a294 Mon Sep 17 00:00:00 2001 From: Michel Roux Date: Sat, 27 Jan 2024 17:54:03 +0100 Subject: [PATCH] feat: webcam memory leak fixed --- Assets/Scripts/CameraManager.cs | 23 +++++++++++++++-------- Packages/manifest.json | 1 + Packages/packages-lock.json | 9 +++++++++ 3 files changed, 25 insertions(+), 8 deletions(-) diff --git a/Assets/Scripts/CameraManager.cs b/Assets/Scripts/CameraManager.cs index ac1db37..7651941 100644 --- a/Assets/Scripts/CameraManager.cs +++ b/Assets/Scripts/CameraManager.cs @@ -8,11 +8,12 @@ public class CameraManager : MonoBehaviour public WebCamDevice wDevice; public Button freezeButton; public Button resumeButton; + private Texture2D photo; // Start is called before the first frame update void Start() { - wDevice = WebCamTexture.devices.First(x => x.isFrontFacing == false); + wDevice = WebCamTexture.devices.FirstOrDefault(x => x.isFrontFacing == false); wTexture = new WebCamTexture(wDevice.name); WebcamResume(); } @@ -20,16 +21,14 @@ void Start() // Update is called once per frame void Update() { - Texture2D cropped = CropTexture(wTexture); - Texture2D rotated = RotateTexture(cropped, !wDevice.isFrontFacing); - - gameObject.GetComponent().texture = rotated; - gameObject.GetComponent().material.mainTexture = rotated; + gameObject.GetComponent().texture = photo ? photo : GetPhoto(); + Resources.UnloadUnusedAssets(); } public void WebcamResume() { wTexture.Play(); + photo = null; freezeButton.gameObject.SetActive(true); resumeButton.gameObject.SetActive(false); } @@ -50,6 +49,7 @@ public void WebcamChange() public void WebcamStop() { + photo = GetPhoto(); wTexture.Stop(); freezeButton.gameObject.SetActive(false); resumeButton.gameObject.SetActive(true); @@ -76,7 +76,6 @@ Texture2D RotateTexture(Texture2D originalTexture, bool clockwise) Texture2D rotatedTexture = new(h, w); rotatedTexture.SetPixels32(rotated); - rotatedTexture.Apply(); return rotatedTexture; } @@ -100,8 +99,16 @@ Texture2D CropTexture(WebCamTexture originalTexture) Texture2D croppedTexture = new(size, size); croppedTexture.SetPixels(originalTexture.GetPixels(x, y, size, size)); - croppedTexture.Apply(); return croppedTexture; } + + Texture2D GetPhoto() + { + Texture2D cropped = CropTexture(wTexture); + Texture2D rotated = RotateTexture(cropped, !wDevice.isFrontFacing); + rotated.Apply(); + + return rotated; + } } diff --git a/Packages/manifest.json b/Packages/manifest.json index e2a4c60..91b486a 100644 --- a/Packages/manifest.json +++ b/Packages/manifest.json @@ -2,6 +2,7 @@ "dependencies": { "com.unity.collab-proxy": "2.2.0", "com.unity.feature.development": "1.0.1", + "com.unity.memoryprofiler": "1.1.0", "com.unity.textmeshpro": "3.0.6", "com.unity.timeline": "1.7.6", "com.unity.toolchain.linux-x86_64": "2.0.6", diff --git a/Packages/packages-lock.json b/Packages/packages-lock.json index 5fbe1a9..4ce32a3 100644 --- a/Packages/packages-lock.json +++ b/Packages/packages-lock.json @@ -60,6 +60,15 @@ "dependencies": {}, "url": "https://packages.unity.com" }, + "com.unity.memoryprofiler": { + "version": "1.1.0", + "depth": 0, + "source": "registry", + "dependencies": { + "com.unity.editorcoroutines": "1.0.0" + }, + "url": "https://packages.unity.com" + }, "com.unity.performance.profile-analyzer": { "version": "1.2.2", "depth": 1,