Beginner Question "Saving a Texture2D"

Sorry fellows, a beginner needs your help.
I just searched in the forum after the following problem but found no solution.

I take a snapshot from the webcam and transfered it to a Texture2D-Variable.

tex.SetPixels(webCam.GetPixels()); // tex is a Texture2D // webCam is WebCamTexture.

And now I want to save this Texture “tex” on my local disc.

I found unter Texture2D. EncodeToPNG() the solution

File.WriteAllBytes(Application.dataPath + “/…/SavedScreen.png”, bytes);

But the code seems not to work. And I could not find the class “File” in the doc.
Can anybody please give me an advice?
Thank you very much.

The code works. “File” is in the MSDN docs.

–Eric

Put this at the top of your C# script:

using System.IO;

Thanks for your fast response.
So at this time, I have only the basic knowledge to work with the scripting code of Unity-JS.
Am I right, that I can save with the “normal” classes of unity ( I am using JS - here “PlayerPrefs”) strings, floats and ints - but no Textures.
(exept the possibility Application.CaptureScreenshot.)

PlayerPrefs is for small amounts of data only. For things like PNG files, use the System.IO.File classes such as WriteAllBytes.

Although, external file access does not work with webplayers for security reasons, so if you have a webplayer as a target platform, that would explain it not working.

–Eric

Thank you Eric - I have to expand my abilities to system IO.