I made a demo application 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:
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();*/
//----
}
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();
}
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With