feat: webcam memory leak fixed

This commit is contained in:
Michel Roux 2024-01-27 17:54:03 +01:00
parent 1de9e63770
commit 249121e775
3 changed files with 25 additions and 8 deletions

View File

@ -8,11 +8,12 @@ public class CameraManager : MonoBehaviour
public WebCamDevice wDevice; public WebCamDevice wDevice;
public Button freezeButton; public Button freezeButton;
public Button resumeButton; public Button resumeButton;
private Texture2D photo;
// Start is called before the first frame update // Start is called before the first frame update
void Start() void Start()
{ {
wDevice = WebCamTexture.devices.First(x => x.isFrontFacing == false); wDevice = WebCamTexture.devices.FirstOrDefault(x => x.isFrontFacing == false);
wTexture = new WebCamTexture(wDevice.name); wTexture = new WebCamTexture(wDevice.name);
WebcamResume(); WebcamResume();
} }
@ -20,16 +21,14 @@ void Start()
// Update is called once per frame // Update is called once per frame
void Update() void Update()
{ {
Texture2D cropped = CropTexture(wTexture); gameObject.GetComponent<RawImage>().texture = photo ? photo : GetPhoto();
Texture2D rotated = RotateTexture(cropped, !wDevice.isFrontFacing); Resources.UnloadUnusedAssets();
gameObject.GetComponent<RawImage>().texture = rotated;
gameObject.GetComponent<RawImage>().material.mainTexture = rotated;
} }
public void WebcamResume() public void WebcamResume()
{ {
wTexture.Play(); wTexture.Play();
photo = null;
freezeButton.gameObject.SetActive(true); freezeButton.gameObject.SetActive(true);
resumeButton.gameObject.SetActive(false); resumeButton.gameObject.SetActive(false);
} }
@ -50,6 +49,7 @@ public void WebcamChange()
public void WebcamStop() public void WebcamStop()
{ {
photo = GetPhoto();
wTexture.Stop(); wTexture.Stop();
freezeButton.gameObject.SetActive(false); freezeButton.gameObject.SetActive(false);
resumeButton.gameObject.SetActive(true); resumeButton.gameObject.SetActive(true);
@ -76,7 +76,6 @@ Texture2D RotateTexture(Texture2D originalTexture, bool clockwise)
Texture2D rotatedTexture = new(h, w); Texture2D rotatedTexture = new(h, w);
rotatedTexture.SetPixels32(rotated); rotatedTexture.SetPixels32(rotated);
rotatedTexture.Apply();
return rotatedTexture; return rotatedTexture;
} }
@ -100,8 +99,16 @@ Texture2D CropTexture(WebCamTexture originalTexture)
Texture2D croppedTexture = new(size, size); Texture2D croppedTexture = new(size, size);
croppedTexture.SetPixels(originalTexture.GetPixels(x, y, size, size)); croppedTexture.SetPixels(originalTexture.GetPixels(x, y, size, size));
croppedTexture.Apply();
return croppedTexture; return croppedTexture;
} }
Texture2D GetPhoto()
{
Texture2D cropped = CropTexture(wTexture);
Texture2D rotated = RotateTexture(cropped, !wDevice.isFrontFacing);
rotated.Apply();
return rotated;
}
} }

View File

@ -2,6 +2,7 @@
"dependencies": { "dependencies": {
"com.unity.collab-proxy": "2.2.0", "com.unity.collab-proxy": "2.2.0",
"com.unity.feature.development": "1.0.1", "com.unity.feature.development": "1.0.1",
"com.unity.memoryprofiler": "1.1.0",
"com.unity.textmeshpro": "3.0.6", "com.unity.textmeshpro": "3.0.6",
"com.unity.timeline": "1.7.6", "com.unity.timeline": "1.7.6",
"com.unity.toolchain.linux-x86_64": "2.0.6", "com.unity.toolchain.linux-x86_64": "2.0.6",

View File

@ -60,6 +60,15 @@
"dependencies": {}, "dependencies": {},
"url": "https://packages.unity.com" "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": { "com.unity.performance.profile-analyzer": {
"version": "1.2.2", "version": "1.2.2",
"depth": 1, "depth": 1,