jijie ciprian jijie ciprian - 2 months ago 28
C# Question

How can I reduce memmory used by WebCameraTexture in unity on windows phone?

I made a demo aplication with two simple view in unity3d for windows phone platform.On first view I have a button and a text, from inspector I assign to button one event ( on Click) to open second view. In this view I have a raw image in a panel used to assign mainTexture to webCamTexture for to Start camera on phone.

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


In second View I have a button where I close camera and show first view ( closing current)
webCameraTexture.Stop();


If I do that many times Play() and Stop() memory on my phone looks like:

enter image description here

How can I clear memory, when I stop the camera, because sometimes give me an error "Not enought storage to complete this operation" and exit from application.

Code Start Stop Camera:

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


Second test

Answer

Currently you play the video with:

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

and stop it with

webCameraTexture.Stop();

This is doing exactly what your code is telling it to do. The new WebCamTexture() line of code is expected to allocate memory each time it is called. You are suppose to do this only once in the Start() function and then you can play and stop camera without memory allocation.

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